Skip to content

Qualified alias names are mishandled #3145

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
pkch opened this issue Apr 8, 2017 · 7 comments · Fixed by #4000
Closed

Qualified alias names are mishandled #3145

pkch opened this issue Apr 8, 2017 · 7 comments · Fixed by #4000
Assignees
Labels
bug mypy got something wrong priority-1-normal

Comments

@pkch
Copy link
Contributor

pkch commented Apr 8, 2017

T = TypeVar('T')

class X(Generic[T]):
    A = X[T]
    def f(self, x: int = 1) -> X[T]: ...
    a: X[T]
    b: A = a # ok
    def g(self) -> None:
        a: X[T]
        b: X.A = a # ok


def g(x: X[T]) -> None:
    p: X[T] = x.f() # ok
    q: X.A = x.f() # fail

I think it should pass, unless qualified aliases are not allowed (in which case it should fail in X.g()). In addition, the error is confusing: Incompatible types in assignment (expression has type m.X[T], variable has type m.X[T]).

@gvanrossum
Copy link
Member

I'm guessing that the code for aliases just isn't robust enough.

Also, running this code gets

    A = X[T]
NameError: name 'X' is not defined

and attempting to fix that using string quotes elicits other errors. But I'm guessing you've boiled this down from a more complicated real use case.

Finally even if all that was taken care of, A would still mean X[Any] because a generic alias requires using [<something>] to fix the type parameter, and without that defaults to [Any].

@ilevkivskyi Thoughts?

@ilevkivskyi
Copy link
Member

I noticed that aliases work normally only when they are global (this is explicitly visible in semananl.py, for example here also see this comment), when I was working on generic aliases. I didn't fix it at that time, I don't remember why.

Concerning the missing Any substitution: this may be because of interference with analize_member_access (most probably I didn't consider this case because I was only thinking about globally defined aliases).

I don't have an immediate idea how to fix this, but probably this should not be too difficult.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal labels Apr 9, 2017
@neiljp
Copy link

neiljp commented Aug 5, 2017

I've just seen the simple case of 'only global aliases work' in porting the zulip mypy to use 0.521, where the following did not seemingly generate an error previously (reduced example):

from typing import List, Dict, Any
def func():
    # type: () -> None
    L = List[Dict[str, Any]]
    def nested(x):
    # type: (L) -> None
        pass

Strangely, at least to me, if the annotation of func is removed, then mypy succeeds, whereas with it, it currently generates: error: Invalid type "L".

@gvanrossum
Copy link
Member

Removing the annotation on func disables all checks inside it.

neiljp added a commit to neiljp/zulip that referenced this issue Aug 5, 2017
Current mypy only allows global Type Aliases; see:
python/mypy#3145
@ilevkivskyi
Copy link
Member

Currently, prohibiting type aliases at function scope is more a deliberate choice, rather than a limitation. One is still allowed to define type aliases at class scope, but only subscripted, i.e.:

class A: ...
class C:
    x = A  # this is an ordinary member with type 'Type[A]'
    y = Dict[str, T]  # this is a type alias

One can always make a variable instead of an alias by adding an explicit annotation. In #3524 I proposed to apply the same rule at a function scope, but Guido said that this would require many explicit annotations in some internal code bases, since class valued variables are used much more often than aliases at a function scope.

@ilevkivskyi
Copy link
Member

Actually, I just re-read that discussion and it looks like the example Guido mentioned deals with class objects (like int), not types (like List[int]), so that I think that we can allow aliases at function scopes, if the alias is subscripted (i.e. exactly the same as we do at class scopes now). This will:

  • simplify the rules (no subtle difference between class and function scopes)
  • solve the Zulip's use case
  • cause no additional errors for situations where classes are used as values (not aliases).

@gvanrossum @JukkaL what do you think?

@ilevkivskyi ilevkivskyi self-assigned this Aug 7, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented Aug 8, 2017

@ilevkivskyi Agreed that aliases to subscripted types should be allowed everywhere. Only simple aliases to class objects (without subscripts) are sometimes problematic.

JukkaL pushed a commit that referenced this issue Oct 10, 2017
Fixes #3145.

This PR allows subscripted type aliases at function scope, as discussed in 
#3145. This will simplify the rules and have other pluses (apart from fixing 
the actual issue).

In addition, I prohibit reuse of bound type variables to define generic 
aliases. Situations like this were always ambiguous.
amanagr pushed a commit to amanagr/zulint that referenced this issue Jun 27, 2019
python/mypy#3145

(imported from commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
amanagr pushed a commit to amanagr/zulint that referenced this issue Jun 27, 2019
python/mypy#3145

(imported from commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
amanagr pushed a commit to amanagr/zulint that referenced this issue Jun 27, 2019
python/mypy#3145

(imported from commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
amanagr pushed a commit to amanagr/zulint that referenced this issue Jun 28, 2019
python/mypy#3145

(imported from zulip/zulip commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
amanagr pushed a commit to amanagr/zulint that referenced this issue Jul 1, 2019
python/mypy#3145

(imported from zulip/zulip commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
amanagr pushed a commit to amanagr/zulint that referenced this issue Jul 10, 2019
python/mypy#3145

(imported from zulip/zulip commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
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-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants