-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Comments
I'm guessing that the code for aliases just isn't robust enough. Also, running this code gets
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, @ilevkivskyi Thoughts? |
I noticed that aliases work normally only when they are global (this is explicitly visible in Concerning the missing I don't have an immediate idea how to fix this, but probably this should not be too difficult. |
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):
Strangely, at least to me, if the annotation of |
Removing the annotation on func disables all checks inside it. |
Current mypy only allows global Type Aliases; see: python/mypy#3145
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. |
Actually, I just re-read that discussion and it looks like the example Guido mentioned deals with class objects (like
@gvanrossum @JukkaL what do you think? |
@ilevkivskyi Agreed that aliases to subscripted types should be allowed everywhere. Only simple aliases to class objects (without subscripts) are sometimes problematic. |
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.
python/mypy#3145 (imported from commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
python/mypy#3145 (imported from commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
python/mypy#3145 (imported from commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
python/mypy#3145 (imported from zulip/zulip commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
python/mypy#3145 (imported from zulip/zulip commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
python/mypy#3145 (imported from zulip/zulip commit 6ac6d6ff5d3b230a7cfcb479a2967662bcdf7973)
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])
.The text was updated successfully, but these errors were encountered: