-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Erase stray typevars in functools.partial generic #18954
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
base: master
Are you sure you want to change the base?
Erase stray typevars in functools.partial generic #18954
Conversation
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.
The change itself makes sense to me but I'm a bit surprised by the tests
|
||
# We should not leak stray typevars that aren't in scope: | ||
use_int_callable(partial(func_b, b="")) | ||
use_func_callable(partial(func_b, b="")) |
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.
Why does this pass? I assume the erased return type is Union[int, str]
and not Never
...
Also could you add some reveal_type
like use_func_callable(reveal_type(partial(func_b, b="")))
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.
The erased return type is Any
. erase_typevars
replaces them with Any
(special_form
), not with upper bounds.
Adding reveal_type
is a good idea, let's show the inferred generics explicitly. I don't even need no, let's keep both - subtype checks may do something differentuse_*_callable
then...
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 think you should still have the use_*_callable
to ensure things still work, at least in a few spots -- printing correctly doesn't necessarily mean the logic is right, e.g. maybe the plugin could mess up passing the partials to the use_*_callable
functions (?).
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.
And Any
is really what we want. Replacing tvars with their upper bounds will cause another bunch of false positives whenever smth like def fn[T: str | int](x: T) -> T
(hope I didn't mess up this ugly syntax) is used with partial.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #18953. Fixes #15215. Refs #17461.
When the function passed to
partial
is generic and has generic params in the return type, we must erase them, otherwise they become orphan and cannot be used later. This only applies topartial[...]
generic param and not to the underlying "exact" callable stored internally as the latter remains generic.The ultimate fix would be to implement #17620 so that we stop caring about
partial[...]
generic param, but this should improve usability (but causes false negatives).