[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light
/ mypy Public
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

Flatten TypeAliasType when it is aliased as a Union #8146

Merged
merged 4 commits into from Dec 20, 2019
Merged

Flatten TypeAliasType when it is aliased as a Union #8146

merged 4 commits into from Dec 20, 2019

Conversation

Copy link
Collaborator

TH3CHARLie commented Dec 14, 2019

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

from typing import Union

T1 = Union[int, float]
T2 = Union[Union[Union[int, float], float], Union[float, complex], complex]

def foo(a: T2, b: T2) -> T2:
    return a + b

The problem, however, is because when using TypeAliasType to alias a Union, the TypeAliasType 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)

  • add testcase

cc @msullivan

Copy link

appleby commented Dec 15, 2019

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

Copy link
Collaborator Author

TH3CHARLie commented Dec 15, 2019

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

Yeah you're right, I'll fix it up
I've patched it up in 0a12cfd. Notice that flatten_nested_unions is already used for UnionType.__init__, in order to prevent a double-flatten, I add a boolean flag to control this and therefore breaks no backward compilability.

TH3CHARLie changed the title Flatten TypeAliasType when it is aliased as a Union [WIP]Flatten TypeAliasType when it is aliased as a Union Dec 15, 2019
TH3CHARLie changed the title [WIP]Flatten TypeAliasType when it is aliased as a Union Flatten TypeAliasType when it is aliased as a Union Dec 16, 2019
Copy link

appleby commented Dec 19, 2019

I can't comment on the suitability of the fix, but I can confirm that the changes here resolve both the toy minimal example in #8125, and also the Real World type checking issue I was having that prompted me to open #8125 in the first place.

Copy link
Member

ilevkivskyi left a comment

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.

mypy/types.py Outdated Show resolved Hide resolved
mypy/types.py Outdated Show resolved Hide resolved
ilevkivskyi merged commit fe8309a into python:master Dec 20, 2019
TH3CHARLie deleted the flatten-nested-unions branch Dec 20, 2019
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.

Binary arithmetic on nested Unions of numeric types fails to typecheck
3 participants