-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 interacts badly with @property even with the type: ignore
workaround
#9937
Labels
bug
mypy got something wrong
topic-descriptors
Properties, class vs. instance attributes
topic-overloads
Comments
jraygauthier
changed the title
@overload interect badly with @property even with the
@overload interacts badly with @property even with the Jan 22, 2021
type: ignore
workaroundtype: ignore
workaround
AlexWaygood
added
topic-overloads
topic-descriptors
Properties, class vs. instance attributes
labels
Apr 1, 2022
Slightly shorter repro: from __future__ import annotations
from typing import overload, Generic, TypeVar
T = TypeVar('T')
class C(Generic[T]):
@overload # type:ignore[misc]
@property
def f(self: C[int]) -> int: raise NotImplementedError
@overload # type:ignore[misc]
@property
def f(self: C[str]) -> str: raise NotImplementedError
@property # type:ignore[misc]
def f(self): raise NotImplementedError
c: C[str] = C()
_ = c.f # E: Invalid self argument "C[str]" to attribute function "f" with type "Callable[[C[int]], int]" Playground: https://mypy-play.net/?mypy=latest&python=3.11&gist=15958eb7a2388321bc5561aeebffefd7 |
Here's a workaround as suggested here: microsoft/pyright#3071 (comment) from __future__ import annotations
from typing import overload, Generic, TypeVar, Any
T = TypeVar("T")
class _fDescriptor:
@overload
def __get__(self, instance: C[int], owner: Any) -> int:
...
@overload
def __get__(self, instance: C[str], owner: Any) -> str:
...
def __get__(self, instance: object, owner: Any) -> int | str:
raise NotImplementedError
class C(Generic[T]):
f: _fDescriptor
c: C[str] = C()
_ = c.f
reveal_type(c.f)
ci: C[int] = C()
reveal_type(ci.f) This will reveal the types correctly. |
Bump! I've just encountered the same issue. I'm not really into creating a custom descriptor. |
1 task
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-descriptors
Properties, class vs. instance attributes
topic-overloads
Assuming a
issue.py
module:This occurs when I attempt to overload a property. It however does not occurs for the same overload on a plain method.
issue.py
is:Your Environment
mypy.ini
(and other config files):$ uname -a Linux rgauthier-precision 5.4.79 #1-NixOS SMP Sun Nov 22 09:14:12 UTC 2020 x86_64 GNU/Linux
The text was updated successfully, but these errors were encountered: