Skip to content
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

Unpacking a NamedTuple subclass uses superclass' types #8663

Open
brettcs opened this issue Apr 12, 2020 · 0 comments
Open

Unpacking a NamedTuple subclass uses superclass' types #8663

brettcs opened this issue Apr 12, 2020 · 0 comments
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-named-tuple

Comments

@brettcs
Copy link

brettcs commented Apr 12, 2020

If you subclass an existing NamedTuple to provide more specific type declarations for some of the fields, and then unpack an instance of that NamedTuple, mypy 0.770 gets its type information from the superclass, not the subclass. Minimal reproduction:

from typing import NamedTuple, Optional

class BaseInventory(NamedTuple):
    number: Optional[int]
    name: str


class Inventory(BaseInventory):
    number: int


item = Inventory(42, 'parrot')
item.number % 10  # mypy allows this
number, _ = item
number % 10  # mypy does not allow this

The last line reports:

error: Unsupported operand types for % ("None" and "int")  [operator]
note: Left operand is of type "Optional[int]"

I expected no error, since accessing the field by name works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-named-tuple
Projects
None yet
Development

No branches or pull requests

2 participants