Skip to content

type statement + Annotated == Invalid type alias #17751

Closed
@nernst

Description

@nernst

Bug Report

Using the type statement with an annotated type results in a invalid type alias error.

To Reproduce

annotated.py:

from typing import Annotated, TypeAlias


class MyAnnotation:
    pass


# Works:
# MyInt: TypeAlias = Annotated[int, MyAnnotation()]

# Causes Invalid type alias: expression is not a valid type  [valid-type]
type MyInt = Annotated[int, MyAnnotation()]


class MyModel:
    value: MyInt

Expected Behavior

I expect the type statement to behave the same as the commented-out manual type alias that works.

Actual Behavior

annotated.py:8: error: Invalid type alias: expression is not a valid type  [valid-type]
    type MyInt = Annotated[int, MyAnnotation()]
                 ^
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.2
  • Mypy command-line flags: mypy annotated.py
  • Mypy configuration options from mypy.ini (and other config files):
    from pyproject.toml:
[tool.mypy]
python_version = "3.12"

# set mypy_path and explicit_package_bases, otherwise mypy gets confused about package names
# due to the use of namespace packages.
mypy_path = "src"
explicit_package_bases = true

pretty = true
disallow_any_unimported = true
disallow_any_expr = false
disallow_any_explicit = false
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_decorators = true
implicit_optional = true
strict_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
enable_incomplete_feature = ["NewGenericSyntax"]
  • Python version used: 3.12.2

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions