Skip to content

Recursive sequence type of Sequence[NotAString] should not accept str #18184

Closed as not planned
@jorenham

Description

@jorenham

With at least mypy 1.13.0, the false negative can be reproduced with:

from collections.abc import Sequence
from typing import TypeAlias

class Scalar: ...

Vector: TypeAlias = Sequence[Scalar]
Tensor: TypeAlias = Vector | Sequence["Tensor"]

accepted_vector: Vector = [Scalar()]     # true negative
rejected_vector: Vector = "duck"         # true positive: ducks aren't vectors

accepted_tensor: Tensor = [[[Scalar()]]] # true negative
rejected_tensor1: Tensor = object()      # true positive: objects aren't tensors
rejected_tensor2: Tensor = "duck"        # false negative: ducks are tensors...?

originally posted in #731 (comment)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions