8000 Type alias for typings.Annotated · Issue #18925 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Type alias for typings.Annotated #18925
Closed as not planned
Closed as not planned
@vitaminSP

Description

@vitaminSP

Bug Report

I am trying to create a type alias for typings.Annotated to improve code readability, but mypy fails during static type checking. In more details, I have the following code:

from typing import Annotated
import torch
import numpy

# This works
a: Annotated[torch.Tensor | numpy.ndarray, 4] | None = None  

# This fails, I expect it to work
type T[*args] = Annotated[torch.Tensor | numpy.ndarray, *args] 
b : T[4] | None = None  

Mypy fails during static checking, but only for the second type annotation. I expect instead that both type annotations would behave the same way. This is the error: error: Invalid type: try using Literal[4] instead? [valid-type].

To Reproduce

See above.

Expected Behavior

See above.

Actual Behavior

See above.

Your Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Mypy command-line flags: None, just the file name
  • Mypy configuration options from mypy.ini (and other config files): No other configs
  • Python version used: 3.12.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0