Skip to content

Semantic analyzers do not correctly handle qualified type aliases #6667

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

Closed
Michael0x2a opened this issue Apr 12, 2019 · 3 comments
Closed

Semantic analyzers do not correctly handle qualified type aliases #6667

Michael0x2a opened this issue Apr 12, 2019 · 3 comments
Labels
bug mypy got something wrong priority-0-high

Comments

@Michael0x2a
Copy link
Collaborator

Michael0x2a commented Apr 12, 2019

Consider the following program:

# test.py
class Outer:
    class Inner: pass

Alias = Outer
x: Alias.Inner
reveal_type(x)

Mypy thinks that the type of x is test.Outer instead of test.Outer.Inner. We get this behavior with both the new and old semantic analyzers.


It seems the root cause has to do with the lookup_qualified function -- when the loop encounters a SymbolNode of type TypeAlias, it effectively stops processing the parts variable, ignoring any subsequent information in that list. So, we drop everything to the right of the first type alias.

There's actually a TODO about this in the code, though it points out the issue for only Var and FuncDef. The new semantic analyzer's lookup_qualified uses the same algorithm + has the same TODO.

In order to fix this correctly, we'll maybe need to either transfer the unhandled parts back to the caller, or move the type alias expansion logic from typeanal into this function? I briefly experimented with both approaches, but they both seemed very clunky.

Probably the right thing to do is to just fix this as a part of the long-awaited lookup-functions-refactor (#4157).

(Hopefully this isn't a dupe -- I'm mostly filing this so I can link to it in a PR I'm preparing.)

Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Apr 12, 2019
This pull request adds in basic support for enum literals. Basically,
you can construct them and use them as types, but not much else.

(I originally said I was going to wait until the new semantic analyzer
work was finished, but I'm bored.)

Some notes:

1. I plan on submitting incremental and fine-grained-mode tests in
   a future PR.

2. I added two copies of each test -- one using the old semantic
   analyzer and one using the new one. Let me know if we prefer not
   doing this and if I should pick one over the other.

3. I wanted to add support for aliases to enums, but ran into some
   difficulty doing so. See python#6667
   for some analysis on the root cause.
@ilevkivskyi
Copy link
Member

This is not a duplicate, and this is indeed about the TODO you mention. I am not sure if there is any "graceful" fix for this. Ideally, the fix should take care of all currently missing node cases (including FuncDef, Var, TypeAlias, maybe also PlaceholderNode if necessary).

I would also defer this until the fix of #4157

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-0-high labels Apr 12, 2019
Michael0x2a added a commit that referenced this issue Apr 12, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
This pull request adds in basic support for enum literals. Basically,
you can construct them and use them as types, but not much else.

Some notes:

1. I plan on submitting incremental and fine-grained-mode tests in
   a future PR.

2. I wanted to add support for aliases to enums, but ran into some
   difficulty doing so. See #6667
   for some analysis on the root cause.
@ilevkivskyi
Copy link
Member

Interestingly on master this now gives:

test.py:5: error: Name 'Alias.Inner' is not defined
test.py:6: note: Revealed type is 'Any'

Which is probably a bit less bad.

@ilevkivskyi
Copy link
Member

This was fixed by #8014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high
Projects
None yet
Development

No branches or pull requests

2 participants