Skip to content

TypeAlias does not work when attribute-accessed from the typing_extensions package #12179

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
bzoracler opened this issue Feb 15, 2022 · 1 comment · Fixed by #12180
Closed
Labels
bug mypy got something wrong

Comments

@bzoracler
Copy link
Contributor

Bug Report

TypeAlias does not trigger errors in mypy if accessed from the typing_extensions package (rather than being imported directly).

To Reproduce

import typing as tp
import typing_extensions as tpx
from typing_extensions import TypeAlias

ASDF: TypeAlias = tp.Literal["A", "S", "D", "F"]
JKL: tpx.TypeAlias = tp.Literal["J", "K", "L"]

Int: tpx.TypeAlias = int
Str: TypeAlias = str

Expected Behavior

Everything below should throw errors in mypy:

G: ASDF = "G"  # error: Incompatible types in assignment (expression has type "Literal['G']", variable has type "Union[Literal['A'], Literal['S'], Literal['D'], Literal['F']]")
H: JKL = "H"  # error: Incompatible types in assignment (expression has type "Literal['H']", variable has type "Union[Literal['J'], Literal['K'], Literal['L']]")
n: Int = "9"  # error: Incompatible types in assignment (expression has type "str", variable has type "int")
s: Str = 9  # error: Incompatible types in assignment (expression has type "int", variable has type "str")

Actual Behavior

The aliases JKL and Int, typed using tpx.TypeAlias, do not trigger errors with mypy:

G: ASDF = "G"  # error: Incompatible types in assignment (expression has type "Literal['G']", variable has type "Union[Literal['A'], Literal['S'], Literal['D'], Literal['F']]")
H: JKL = "H"  # Expected error here; got none
n: Int = "9"  # Expected error here; got none
s: Str = 9  # error: Incompatible types in assignment (expression has type "int", variable has type "str")

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
    [mypy]
    plugins = numpy.typing.mypy_plugin
    ignore_missing_imports = True
    
  • Python version used: 3.8.12
  • Operating system and version: Elementary OS 6.1 Jólnir (Based on Ubuntu 20.04 LTS)
@bzoracler bzoracler added the bug mypy got something wrong label Feb 15, 2022
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 15, 2022

Yikes, this is pretty bad. Thanks for the report. I think the issue isn't about typing_extensions vs typing (which tests cover), but rather the import alias. That is, tp.TypeAlias fails as well. Looks easy to fix, though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants