[Python] mypy linter error (valid-type) with pydantic's Annotated pattern in a generic

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    Why would the following give a linter error in the second case but not the first:

    # OK:
    type MyAnnotatedType = Annotated[int | None, Field(strict=True)]
    # Error: Invalid type alias: expression is not a valid type mypy(valid-type)
    type MyAnnotatedSeqType = Sequence[Annotated[int | None, Field(strict=True)]]
    # OK again:
    type MyAnnotatedSeqTypeOK = Sequence[MyAnnotatedType]


    Is this a limitation specified in PEP 593 somewhere, or more of a simplification in mypy to avoid more complex type inference, possibly requiring handling metadata at runtime?

    Continue reading...

Compartilhe esta Página