Skip to content

Generic NamedTuples and TypedDict #7719

Closed
@crusaderky

Description

@crusaderky

mypy 0.730 doesn't work correctly with classes that descend both from NamedTuple or TypedDict and from Generic. dataclasses instead work fine.

from dataclasses import dataclass
from typing import Generic, NamedTuple, TypeVar
from typing_extensions import TypedDict


T = TypeVar("T")

@dataclass
class A(Generic[T]):
    x: T


class B(NamedTuple, Generic[T]):
    x: T


class C(TypedDict, Generic[T]):
    x: T


"foo" + A(x=1).x
"foo" + B(x=1).x
"foo" + C(x=1)["x"]

mypy output:

21: error: Unsupported operand types for + ("str" and "int")
22: error: Unsupported operand types for + ("str" and "T")
22: error: Argument "x" to "B" has incompatible type "int"; expected "T"
23: error: Unsupported operand types for + ("str" and "T")
23: error: Incompatible types (expression has type "int", TypedDict item "x" has type "T")

Expected output:

21: error: Unsupported operand types for + ("str" and "int")
22: error: Unsupported operand types for + ("str" and "int")
23: error: Unsupported operand types for + ("str" and "int")

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