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

dataclass_transform alias does not work with python version 3.11 #3912

Closed
RobertCraigie opened this issue Sep 5, 2022 · 2 comments
Closed
Labels
as designed Not a bug, working as intended

Comments

@RobertCraigie
Copy link
Contributor

Describe the bug

Pyright does not use the given alias for the synthesised __init__ method if pythonVersion is set to 3.11.

To Reproduce

from pydantic import BaseModel, Field

class Model(BaseModel):
    prettyName: str = Field(alias='name')

print(Model(name='foo'))
reveal_type(Model.__init__)

With this pyproject.toml file:

[tool.pyright]
pythonVersion = "3.11"
typeCheckingMode = "strict"

And the third party library used, pydantic==1.10.2.

This is what pyright outputs:

test.py
  test.py:8:13 - error: No parameter named "name" (reportGeneralTypeIssues)
  test.py:9:13 - information: Type of "Model.__init__" is "(self: Model, *, prettyName: str = Field(alias='name')) -> None"
1 error, 0 warnings, 1 information 

It should be noted that I also encountered this error with pythonVersion being left unset in the config file.

Expected behavior

Pyright should not error and the revealed type should be:

(self: Model, *, name: str) -> None

VS Code extension or command-line

Command-line, v.1.1.269.

@erictraut erictraut added the bug Something isn't working label Sep 6, 2022
@erictraut
Copy link
Collaborator

This is a bug in pydantic's use of dataclass_transform. Prior to the finalization of PEP 681, we changed the name of one of the parameters from field_descriptors to field_specifiers. This was based on feedback. Pydantic is still using the old (obsolete) name in its code.

@dataclass_transform(kw_only_default=True, field_descriptors=(Field, FieldInfo))

It needs to be updated to match the PEP. Please file a bug with the pydantic maintainers. In the meantime, you can make the change manually in your copy of pydantic sources if you want this to work.

I'll also see if there's a short-term workaround that I can make in pyright to help people who are using the current pydantic version.

@erictraut
Copy link
Collaborator

The maintainer of pydantic confirmed that this will be fixed in a dot release within the next several days. pydantic/pydantic#4500

@erictraut erictraut added as designed Not a bug, working as intended and removed bug Something isn't working labels Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants