-
-
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
Fix meta level on member access #2809
Conversation
test-data/unit/lib-stub/typing.pyi
Outdated
class Mapping(Generic[T, U]): pass | ||
class Mapping(Generic[T, U]): | ||
@overload | ||
def get(self, k: T) -> Optional[U]: pass |
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.
Can you move this to Dict
in fixture/dict.pyi
? We try to keep the typing.pyi
stub pretty minimal.
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.
OK, sure, I moved this to dict.pyi
in a new commit.
@@ -990,6 +990,13 @@ def analyze_ordinary_member_access(self, e: MemberExpr, | |||
e.name, original_type, e, is_lvalue, False, False, | |||
self.named_type, self.not_ready_callback, self.msg, | |||
original_type=original_type, chk=self.chk) | |||
if isinstance(member_type, CallableType): | |||
for v in member_type.variables: | |||
v.id.meta_level = 0 |
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.
If we just switch the meta level, I think that we can get an id conflict between type variables in the 0 level if we happen to be in a generic function which has some additional type variables in scope. Not sure what's the best way to work around this. What about serializing the meta levels of type variables as well?
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 can get an id conflict between type variables in the 0 level if we happen to be in a generic function which has some additional type variables in scope
I was also thinking about such possibility, but I didn't manage to trigger this error so I thought maybe I was wrong. Now you are also saying this and I am worried again. I need to think more about this tomorrow, maybe there is a way to exclude such possibility.
What about serializing the meta levels of type variables as well?
I think that serialazing type variables with meta_level == 1
is not a good idea. As I understand, this means that they are in process of being inferred, while serialazing is used for completely analysed modules, or am I wrong?
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.
Actually, your original approach actually seems to be fine, since we are should always freshen the type variables before using them in type inference, so I think that there can be no conflict.
@gvanrossum What does the new label |
Sorry, the "blocked" label means that the PR is waiting for someone or something not on the core mypy team -- typically waiting for the author to push more commits (e.g. response to code review or a rebase) or when it's waiting for another issue to be fixed, or perhaps on a difficult decision. This is a signal to the core team to reduce priority reviewing. We're still experimenting with this workflow -- we don't have a way to signal exactly what it's waiting for, nor do we have a bot yet that automatically unblocks when the anticipated event happens. (Nor have we consistently applied it yet to the mypy repo -- I did do a pass over typeshed.) Sorry to scare you! I've removed the label now that you've responded to the code review. |
@gvanrossum |
Do you have a suggestion for a better color? The current color was randomly
picked for my by GitHub...
|
White text on blue background might be better (the PR is "cold", waits for a new input). |
I think that this is actually good now and my concerns weren't valid. Thanks for the PR! |
@JukkaL |
Hmm I just noticed that there are several calls to |
OK, I will take a look at those and will make an additional PR if necessary. |
I investigated this, and it looks like wee need to do this everywhere, one can still trigger the crash in serialize with from typing import TypeVar
T = TypeVar('T')
class C:
def m(self, x: T) -> T:
return x
class D(C):
def __init__(self) -> None:
self.d = super().m I will make a PR to fix this soon. |
OK, here is the PR I promised #2847 |
Fixes #2804
The problem is that
analize_member_access
callsfreshen_function_type_vars
if the member is a function (i.e. method). This is necessary to not mix the type variables during type inference of generic methods inside generic functions. However, if the method does not participate in type inference, type variables are left inmeta_level = 1
state. This causes the error, since the types could not be serialized in the middle of type inference.In this PR I propose to restore
meta_level = 0
on member access, this is safe, since anyway,check_call
always callsfreshen_function_type_vars
on callee (for generic functions, generic methods, etc).