Skip to content

Generic NamedTuples and TypedDict #7719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
crusaderky opened this issue Oct 15, 2019 · 1 comment
Closed

Generic NamedTuples and TypedDict #7719

crusaderky opened this issue Oct 15, 2019 · 1 comment

Comments

@crusaderky
Copy link
Contributor

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")
@ilevkivskyi
Copy link
Member

Generic named tuples and typed dicts are not supported, see feature-request issues:

The bug currently is that these are not flagged as errors, see #6200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants