Skip to content

Unbound parameters of type aliases silently default to Any #3386

@jkleint

Description

@jkleint

This may just be my misunderstanding, but it seems potentially undesirable, so I am reporting it as a bug. As of mypy 0.511, this typechecks just fine:

from typing import TypeVar, Tuple

V = TypeVar('V', bound=int)
E = Tuple[V, V]

def outgoing(edge: E) -> V:         # Note unparameterized E
    reveal_type(edge)               # Revealed type is 'Tuple[Any, Any]'
    return edge[1]

print(outgoing((1, 'a')))

It looks like declaring a variable of type E ought to be the same as declaring it of type Tuple[V, V], but in fact it becomes Tuple[Any, Any]. The issue is that the alias itself requires a type parameter (E[V]). This is a mistake that is both easy to make and silently removes type safety. Unfortunately, the PEP and docs are not very clear about aliases needing parameters.

I'm not sure if there is a use case for missing alias parameters defaulting to Any, but it seems like type safety would be greatly improved it produced a typechecking error instead. It would really be convenient if mypy could try to bind missing type alias parameters to bound parameters of the same name, which would improve both usability and type safety. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions