-
-
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
Support PEP 681 (dataclass_transform) #14293
Comments
It would be really nice to support |
Hm? Python 3.11 has already been released. |
@thomkeh Thanks for catching my mistake, edited 😄 |
Just to keep this issue up to date: mypy 1.0 shipped with extremely rudimentary support for dataclass_transform decorator with no parameters passed. mypy master has increasingly better support for PEP 681. |
@wesleywright Looks like this complete on master? If so, I'll close this issue. Please feel free to tick this off the Python 3.11 issue. |
@NeilGirdhar Yes, as far as I'm aware we're feature complete on master now. |
Does the implementation support taking into account overloads to determine whether a field will be present in the The example is from this section of the PEP: https://peps.python.org/pep-0681/#field-specifier-parameters |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Re-reading the PEP, it looks like the use of overloads for field specifiers isn't quite fully implemented. We currently have this behaviour on mypy master: import typing
from typing import overload, Optional, Any, Callable, Literal, Type, TypeVar
# Library code (within type stub or inline)
# In this library, passing a resolver means that init must be False,
# and the overload with Literal[False] enforces that.
@overload
def model_field(
*,
default: Optional[Any] = ...,
resolver: Callable[[], Any],
init: Literal[False] = False,
) -> Any: ...
@overload
def model_field(
*,
default: Optional[Any] = ...,
resolver: None = None,
init: bool = True,
) -> Any: ...
def model_field(
*,
default: Optional[Any] = ...,
resolver: Optional[Callable[[], Any]] = ...,
init: bool = ...,
) -> Any: ...
_T = TypeVar("_T")
@typing.dataclass_transform(
kw_only_default=True,
field_specifiers=(model_field, ))
def create_model(
*,
init: bool = True,
) -> Callable[[Type[_T]], Type[_T]]: ...
# Code that imports this library:
@create_model(init=True)
class CustomerModel:
id: int = model_field(resolver=lambda : 0)
name: str
cm = CustomerModel(name="John") Mypy output:
The second error is a false positive: mypy should be able to infer that there's no "id" argument in the |
Opened #14870 to support the implicit defaults for the |
@tmke8 are you aware of any specific use cases for such overloads in the wild? |
@wesleywright no, I just read about them in the PEP |
You could check with the pydantic and SQLAlchemy folks; they seem to be heavy users of |
You can see more context about the field specifier overloads in microsoft/pyright#1782 cc @patrick91 |
More specific link: microsoft/pyright#1782 (comment) |
I suspect that full overload resolution in the dataclass plugin would require some changes to the plugin system, since during the main dataclass transform pass types aren't fully set up yet, and subtype checks can't be reliably used. I wonder if it would be sufficient to do "lightweight" overload resolution using only the argument counts and names and some simple type rules (e.g. matching A possible more general approach would to postpone the determination of the Yet another idea would be to generate the |
This also updates all dependencies and fixes the minor fallout of four MyPy typecheck issues (all instances of no support yet for @dataclass_transform: python/mypy#14293) and one Sphinx issue with the custom directive systems use of markdown.
This also updates all dependencies and fixes the minor fallout of four MyPy typecheck issues (all instances of no support yet for @dataclass_transform: python/mypy#14293) and one Sphinx issue with the custom directive systems use of markdown.
This also updates all dependencies and fixes the minor fallout of four MyPy typecheck issues (all instances of no support yet for @dataclass_transform: python/mypy#14293) and one Sphinx issue with the custom directive system's use of markdown.
I believe this is complete now. |
PEP 681 introduces a new decorator function in the
typing
module nameddataclass_transform
. This was added to Python 3.11.See #5383 for a related issue that this would solve. See #12840 for MyPy's Python 3.11 issues.
The text was updated successfully, but these errors were encountered: