Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overload + decorator to transform callable into class protocol w/ attribute does not see the attribute #18715

Open
jakkdl opened this issue Feb 21, 2025 · 0 comments
Labels
bug mypy got something wrong topic-overloads

Comments

@jakkdl
Copy link

jakkdl commented Feb 21, 2025

Bug Report
Trying to combine function attributes (a la #2087) where we add the attribute with a decorator fails to apply the decorator and the attribute is dropped on the ground.

To Reproduce

from __future__ import annotations
from typing import overload, Callable, TypeVar, Protocol
from typing_extensions import ParamSpec, reveal_type

P = ParamSpec("P")
R = TypeVar("R", covariant=True)

class WithExtra(Protocol[P, R]):
    extra: str
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...

def add_extra(func: Callable[P, R]) -> WithExtra[P, R]:
    func.extra = "added"  # type: ignore
    return func  # type: ignore

@overload
@add_extra
def foo(x: int) -> str: ...

@overload
@add_extra
def foo(x: str) -> str: ...

@add_extra
def foo(x: int | str) -> str:
    return str(x)

assert foo.extra  # type: ignore[attr-defined]
reveal_type(foo)  # Revealed type is "Overload(def (x: builtins.int) -> builtins.str, def (x: builtins.str) -> builtins.str)"

This works in pyright.
I feel like it should be possible to work around it with a callable class, but I've yet to figure out the proper incantation.

Real-life use: https://github.com/pytest-dev/pytest/blob/b0caf3d7adc45f773177424593431869fd2f82d8/src/_pytest/python_api.py#L1025

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: python 3.13.1
@jakkdl jakkdl added the bug mypy got something wrong label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overloads
Projects
None yet
Development

No branches or pull requests

2 participants