-
-
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
Allow fancy self-types #7860
Allow fancy self-types #7860
Conversation
Few extra comments:
|
(Also note that we can count #3625 as fixed only if remove all checks at the definition site). |
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.
Maybe you'll want to get a second opinion from Jukka/Sully, but FWIW these changes all look fine to me: the overall approach seems sound and nicely minimal, and my comments were mostly nits and smaller suggestions.
Co-Authored-By: Michael Lee <michael.lee.0x2a@gmail.com>
Co-Authored-By: Michael Lee <michael.lee.0x2a@gmail.com>
Co-Authored-By: Michael Lee <michael.lee.0x2a@gmail.com>
Co-Authored-By: Michael Lee <michael.lee.0x2a@gmail.com>
…kyi/mypy into fix-bind-self-direction
I changed my mind about this. With a tiny extra fix this issue will be now also closed by this PR. |
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.
This is a really exciting improvement. I love when so much falls out of one fix.
@@ -561,6 +561,140 @@ with ``Union[int, slice]`` and ``Union[T, Sequence]``. | |||
to returning ``Any`` only if the input arguments also contain ``Any``. | |||
|
|||
|
|||
.. _advanced_self: | |||
|
|||
Advanced uses of self-types |
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.
Is it worth documenting the use in __init__
?
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.
I noticed it is mostly useful for typeshed stubs, but didn't find this patter in user code, I will add a short sentence at the end, where I discuss overloads.
mypy/typeops.py
Outdated
# (info). For example | ||
# We first need to record all non-trivial (explicit) self types in __init__, | ||
# since they will not be available after we bind them. Note, we use explicit | ||
# self-types only in the defining class, similar to __new__ (see below). |
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.
__new__
can use explicit returns in subclasses, but kind of as a technicality, since it only works using bound type vars, I think.
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.
Yes, I will make the comment more clear.
""" | ||
if isinstance(typ, TypeType): | ||
return supported_self_type(typ.item) | ||
return (isinstance(typ, TypeVarType) or |
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.
This doesn't seem to check that there is an instance upper bound? Should there be?
Also my inclination would be to throw parens around the and clause.
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.
This doesn't seem to check that there is an instance upper bound? Should there be?
I would say it is safe to keep it as is. We can restrict this later if we will find problems.
Also my inclination would be to throw parens around the and clause.
OK.
Fixes #3625
Fixes #5305
Fixes #5320
Fixes #5868
Fixes #7191
Fixes #7778
Fixes python/typing#680
So, lately I was noticing many issues that would be fixed by (partially) moving the check for self-type from definition site to call site.
This morning I found that we actually have such function
check_self_arg()
that is applied at call site, but it is almost not used. After more reading of the code I found that all the patterns for self-types that I wanted to support should either already work, or work with minimal modifications. Finally, I discovered that the root cause of many of the problems is the fact thatbind_self()
uses wrong direction for type inference! All these years it expected actual argument type to be supertype of the formal one.After fixing this bug, it turned out it was easy to support following patterns for explicit self-types:
__init__
for generic classesThis PR cuts few corners, but it is ready for review (I left some TODOs). Note I also add some docs, I am not sure this is really needed, but probably good to have.