-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy has implemented pep-681 incorrectly and does not intepret descriptors correctly #14868
Comments
I've updated the description to include a typing-only example, illustrating how this spec is meant to work per the authors of pep-681 |
it looks like mypy has this issue already at #13856. why was mypy 1.1.1 released with this feature knowing it was broken? Is there a way I can tell my users to disable mypy's pep-681 support until this is fixed? |
Just a thought, but does the sqlalchemy repo have any examples that are type checked, which might have been caught by mypy_primer (or whichever step it is) in a PR? As more projects migrate to sqlalchemy 2, these breakages should be caught earlier, but if sqlalchemy has some typed examples, that could probably help ensure they are checked. I'll try and make sure some of the aio-libs examples use |
we have a whole directory of files that are type checked as part of CI, we didnt add mappedasdataclass becuase mypy has not supported it: https://github.com/sqlalchemy/sqlalchemy/tree/main/test/ext/mypy/plain_files |
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
that's exciting, will check it out in a few |
looks good, will have a patch ready for this when 1.2 is released, thanks! |
Infer `__init__` argument types from the signatures of descriptor `__set__` methods, if present. We can't (easily) perform full type inference in a plugin, so we cheat and use a simplified implementation that should still cover most use cases. Here we assume that `__set__` is not decorated or overloaded, in particular. Fixes #14868.
Infer `__init__` argument types from the signatures of descriptor `__set__` methods, if present. We can't (easily) perform full type inference in a plugin, so we cheat and use a simplified implementation that should still cover most use cases. Here we assume that `__set__` is not decorated or overloaded, in particular. Fixes #14868.
Currently using the PR for test. Change-Id: Idc4c475587f5151ef79046d24ca3ac274c2cb2ca References: python/mypy#14868
Hi -
Now that mypy has pep-681 support, they are making a mistake that we spent a lot of time with the pyright people working out.
This SQLAlchemy mapping and usage is valid:
however Mypy reports:
this is not correct.
Mapped[]
is a descriptor, so the appropriate value that should be set is a string, not a descriptor object.Here's a complete example using only typing as an import
We had very involved discussions about this here: microsoft/pyright#2958 where the authors of pep-681 decided only to add a note about this here: https://peps.python.org/pep-0681/#descriptor-typed-field-support
While I was disappointed they would not add this language to the spec explicitly, they state it in that paragraph:
That is, Python dataclasses when a descriptor is used as the left hand type, the type of the actual attribute passed to init must be the contained datatype, not the descriptor type.
We at SQLAlchemy spent weeks back and forth with the pep-681 authors on this and I am a little bit terrified that because this language wasn't included, Mypy did it exactly the wrong way.
I am very hopeful that the Mypy authors can correct this behavior else we will have to get our users to disable pep-681 support for mypy.
The text was updated successfully, but these errors were encountered: