Skip to content
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

new union syntax doesn't work on Literals #11426

Closed
DetachHead opened this issue Nov 1, 2021 · 3 comments · Fixed by #14008
Closed

new union syntax doesn't work on Literals #11426

DetachHead opened this issue Nov 1, 2021 · 3 comments · Fixed by #14008
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator) topic-type-alias TypeAlias and other type alias issues

Comments

@DetachHead
Copy link
Contributor

from __future__ import annotations

from typing import Literal

Foo = Literal[1, 2]

Bar = Foo | Literal[3] # error: Unsupported left operand type for | ("object")

https://mypy-play.net/?mypy=latest&python=3.10&gist=3a429dfa3e9e3b692aac25887027ab69

@DetachHead DetachHead added the bug mypy got something wrong label Nov 1, 2021
@sobolevn
Copy link
Member

sobolevn commented Nov 1, 2021

This also happens without __future__ import, python3.9:

from typing import Literal

Foo = Literal[1, 2]
Bar = Foo | Literal[3]
# error: Invalid type alias: expression is not a valid type
# error: Unsupported left operand type for | ("object")

Will fix it right now, thanks!

@sobolevn
Copy link
Member

sobolevn commented Nov 1, 2021

On python3.10 it generates a single error:

» mypy out/ex.py --show-traceback
out/ex.py:4: error: Unsupported left operand type for | ("object")

@JelleZijlstra
Copy link
Member

On 3.9 without the import the error is correct since | doesn't work at runtime in 3.9. But it should not error in 3.10 or with the future import on, or in stringified annotations.

@JelleZijlstra JelleZijlstra added the topic-pep-604 PEP 604 (union | operator) label Mar 19, 2022
@AlexWaygood AlexWaygood added the topic-type-alias TypeAlias and other type alias issues label Mar 24, 2022
JukkaL added a commit that referenced this issue Nov 4, 2022
Fix Python 3.10 `|` union syntax in type aliases, when one of
the operands is a type alias or a type with an overloaded `__init__`.

Fixes #12368. Fixes #12005. Fixes #11426.
JukkaL added a commit that referenced this issue Nov 7, 2022
Fix Python 3.10 `|` union syntax in type aliases, when one of
the operands is a type alias or a type with an overloaded `__init__`.

We can now infer `typing._SpecialForm` for type aliases in a runtime 
context.

Also create a bunch of minimal test-only stubs for stdlib modules 
to fix some test failures caused by the missing `typing._SpecialForm`
in the default test stubs. This is generally what we want in any case, 
since using typeshed stubs with minimal builtins/typing stubs can
result in unpredictable behavior and slow tests.

Fixes #12368. Fixes #12005. Fixes #11426.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-604 PEP 604 (union | operator) topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants