Skip to content
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 the logic in active self-type calculation for current scope #7235

Merged
merged 2 commits into from
Jul 18, 2019

Conversation

ilevkivskyi
Copy link
Member

Fixes #5846

The fix for crash is kind of straightforward. Previously active_self_type() always returned None at method scope (sic!)

While working on this I discovered couple other problems:

  • Area around checking LSP is generally problematic (see also Overriding definition such as type alias in a subclass causes a crash #5425 that I tried to fix earlier but failed). In particular, the are two ways to get current TypeInfo, one is from lvalue.node.info or defn.info, another is using active_self_type() (this is rather abusing it IMO). I don't try to fix this here (i.e. switch to always using one way) because this is a relatively large refactoring.
  • Currently in type checker we defer nested functions instead of top-level ones. I believe this is not by design and is rather caused by absence of docstrings and unintuitive method names in CheckerScope class. Namely, there "top function" stays for "top of stack function", not "top-level function". I don't try to fix this here either because this is conceptually a big change.

@ilevkivskyi ilevkivskyi requested a review from JukkaL July 18, 2019 11:19
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's strange that this hasn't been causing more trouble. I assume that this only triggers in very specific cases that are pretty rare.


class C(B):
def __init__(self) -> None:
self.x: Any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the type is incompatible with the base class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I am not super happy with the error message, because it says:

Incompatible types in assignment (expression has type "int", base class "B" defined the type as "str")

while the initial type is T (it shows the mapped/expanded type). It is however not easy to improve, so I think it is OK.

@ilevkivskyi ilevkivskyi merged commit 0b59a10 into python:master Jul 18, 2019
@ilevkivskyi ilevkivskyi deleted the fix-active-self branch July 18, 2019 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Internal Error when overriding a generic variable in __init__
2 participants