-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Flatten TypeAliasType when it is aliased as a Union #8146
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
Conversation
IIUC, this change only handles a single level of nesting? A single level is all I currently need so I'm not complaining, but, for example, the following still fails to typecheck for me. from typing import Union
T1 = int
T2 = Union[T1, float]
T3 = Union[T2, complex]
T4 = Union[T3, int]
def foo(a: T4, b: T4) -> T4:
return a + b |
|
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.
Thanks! This fix looks reasonable to me (at least for now). I previously mentioned potential of such problems because of the new type alias representation. Maybe at some point I will run a session of "fuzzy testing": will run all tests where unions are randomly replaced with nested aliased unions.
Please add tests and I think I will merge this.
Resolves #8125
@appleby has some really good thoughts on the issue and I tried some of them.
The main problem is not about flattening unions inside variants since the following code generates no error
The problem, however, is because when using
TypeAliasType
to alias a Union, theTypeAliasType
will not get flattened, so this PR fixes this.I haven't added testcase yet, since no proper fixture file seems to support the add ops in the test code of #8125 , so lots of
error: Unsupported left operand type for +
will be reported (running mypy using command line will pass the test code)cc @msullivan