-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Implementation of PEP 673 (typing.Self) #11666
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
Conversation
|
Did you do this off an old checkout? There's a ton of merge conflicts. The PEP itself suggested implementing Self by desugaring into a bound TypeVar, which might be easier since it can (hopefully) be done fully in the semanal phase. |
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, very excited for this feature! 👍
Going to leave some minor early comments 🙂
Co-authored-by: Nikita Sobolev <[email protected]>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I am not completely sure it is due to the implementation, but in the piece of code bellow mypy cannot correctly understand the type of an object that uses this commit's implementation of
The contravariant and covariant issue is separate and I believe it is a problem with master too, by following the prompt it just leads to pyright complaining that T and K should be invariant and then for both it leads to "Cannot use a contravariant type variable as return type", so I assume they should be invariant. |
# Conflicts: # mypy/constraints.py # mypy/semanal.py # mypy/subtypes.py # mypy/typeanal.py
This comment has been minimized.
This comment has been minimized.
|
I'd like a bit of advice on how to be substitute Self into a function signature, should I use a TypeVistor subclass or should I just have a function to do it? |
mypy/typeshed/stdlib/typing.pyi
Outdated
|
|
||
| if sys.version_info >= (3, 11): | ||
| # Self is also a (non-subscriptable) special form. | ||
| Self: object = ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't change this directly, it is just copied from typeshed
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I'm not sure, but it seems that the documentation says nothing about
That's also weird because the documentation doesn't promise that either: "The return value of Perhaps things have changed since the docs were written, but it seems that all you can promise a priori is: It's not even guaranteed that the return value has the same type. I think MyPy should automatically apply this basic assumption, and if the user wants to add additional guarantees, they should be free to do so, e.g., |
|
For what it's worth, pyright seems to allow EDIT: but not in static methods: |
|
Hi @erikkemperman now that my exams are over would you be ok with me co-authoring you on this as I try to push it over the finish line? |
|
@Gobot1234 That’d be wonderful, actually — I got a bit stuck to be honest, and have been too busy with work stuff to work on this. For what it’s worth, here is my branch: https://github.com/erikkemperman/mypy/commits/gobot-master This is basically your PR, merged with the upstream master branch of end of May, plus some some small fixes to make some tests pass. At that point I had the feeling that it should be possible to implement this PEP in a somewhat “smaller” approach, i.e. the desugaring strategy you also mentioned, but I did not get very far trying that for lack of time. I also was slightly confused about some edge cases like I still think it’s a bit odd that mypy handles this method differently if a user explicitly decorates it as static, but that should probably be a separate issue. |
Gobot master
# Conflicts: # mypy/meet.py # mypy/typeops.py
This comment has been minimized.
This comment has been minimized.
|
Looks like the interaction with class methods is messed up. When |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
steam.py
I'm able to fix these locally I can push these changes at some point later in the week. ✅ fixed
These are concerning but I'm not entirely what's causing them,
First error is catching a valid type error not sure what the second is on about
Is a type error on my end.
This error is a genieuly baffling one I'm yet to wrap my head round. These functions (
It seems like this case needs a bit of special casing for the compatiblity checker. discord.py
Something very funky is going on here considering (for or) they are
Something here isn't being bound correctly. ✅ fixed |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
For the works. I also haven't been able to reproduce the tornado errors with a small snippet |
|
Diff from mypy_primer, showing the effect of this PR on open source code: |
Ref #12840 Fixes #11871 Fixes #14089 This is an alternative implementation to two existing PRs: #11666, #13133. This PR treats `typing.Self` as pure syntactic sugar, and transforms it into a type variable early during semantic analyzis. This way we can re-use all the existing machinery and handled edge cases for self-types. The only new thing is self-type for _attributes_ (as proposed in the PEP). This required handling in several places, since attribute access is duplicated in several places (see #7724), plus special forms (like NamedTuples and TypedDicts) and dataclasses plugin require additional care, since they use attribute annotations in special ways. I don't copy all the existing tests for "old style" self-types, but only some common use cases, possible error conditions, and relevant new edge cases, such as e.g. special forms mentioned above, and implicit type variable binding for callable types.
|
Superseded by #14041 |
Description
An initial implementation of typing.Self type checking for PEP 673.
Couple of questions:
Test Plan
I've tested the use cases from the PEP individually and it worked but unit test wise I'm not entirely sure what to do should I basically just copy all of https://github.com/python/mypy/blob/master/test-data/unit/check-selftype.test and add some extra stuff for recursive classes and staticmethods?