-
-
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
Dataclass "init" method should expect the type from a custom descriptor's "set" method #13856
Comments
@erictraut fyi, I migrated your issue from pyright here |
FYI, the behavior of |
I think this confusion goes a bit deeper than from __future__ import annotations
from typing import reveal_type, TYPE_CHECKING
from attr import define
class DecoGet:
pass
class Deco:
def __set__(self, oself: object, value: Deco) -> None:
...
def __get__(self, oself: object, owner: None | type[object]=None) -> DecoGet:
return DecoGet()
@define
class AttrBlub:
a: Deco
b: Deco = Deco()
from dataclasses import dataclass
@dataclass
class DataBlub:
a: Deco
b: Deco = Deco()
reveal_type(AttrBlub(Deco()).a)
reveal_type(AttrBlub(Deco()).b)
reveal_type(DataBlub(Deco()).a)
reveal_type(DataBlub(Deco()).b)
reveal_type(AttrBlub.a)
reveal_type(AttrBlub.b)
try:
reveal_type(DataBlub.a) # AttributeError at runtime
except:
print("skipped")
reveal_type(DataBlub.b) Side-by-side, type-check and output looks like this (emojis added to indicate agreement / disagreement between type-time and runtime):
Updated to include annotations about which one's which, which shows that the problem is with attrs' annotations more than it is with dataclasses, although "attribute error" vs. descriptor-type is not great. |
As I was playing with this I discovered a more specific problem in this neighborhood with Callable #14869 |
Mypy 1.1.1 has been released which includes a non-compliant pep-681 implementation that fails with SQLAlchemy's :class:`.MappedAsDataclass` and similar features. In order to work around this issue until Mypy is able to release a fix, as well as to support other typing tools which may have non-compliant pep-681 implementations, document a workaround class for :class:`.MappedAsDataclass`. Including this class as well as a decorator was considered, but overall this is an issue with typing tools that they will have to resolve and I'm not ready to set up for this issue going on long term. There's also no good solution for the decorator version since you have to have an ``__init__`` method indicated somewhere. References: python/mypy#13856 Fixes: #9467 Change-Id: I1be6abea7f7fc72883c14ab2447edad937d0c23f
Mypy 1.1.1 has been released which includes a non-compliant pep-681 implementation that fails with SQLAlchemy's :class:`.MappedAsDataclass` and similar features. In order to work around this issue until Mypy is able to release a fix, as well as to support other typing tools which may have non-compliant pep-681 implementations, document a workaround class for :class:`.MappedAsDataclass`. Including this class as well as a decorator was considered, but overall this is an issue with typing tools that they will have to resolve and I'm not ready to set up for this issue going on long term. There's also no good solution for the decorator version since you have to have an ``__init__`` method indicated somewhere. References: python/mypy#13856 Fixes: sqlalchemy#9467 Change-Id: I1be6abea7f7fc72883c14ab2447edad937d0c23f
This already works with pyright due to: microsoft/pyright#3245 but mypy does not yet support this: python/mypy#13856 See also: python/mypy#14868
This already works with pyright due to: microsoft/pyright#3245 but mypy does not yet support this: python/mypy#13856 See also: python/mypy#14868
Just tested out with |
This issue is copied, mostly verbatim, from here: microsoft/pyright#3245.
If a
@dataclass
field is initialized with a descriptor as a default value, mypy should use the value type of the__set__
method for the descriptor when determining the type of the corresponding parameter within the synthesized__init__
method.Types are also evaluated incorrectly in this case.
To Reproduce
Run mypy against the above script.
Expected Behavior
No Errors
Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: