Skip to content

How do you implement type-checking with methods for classes that inherit from NamedTuple? #2954

@knowsuchagency

Description

@knowsuchagency

Could it be related to the bug mentioned in #1987 ?

class A:
    def __init__(self, x: int) -> None:
        self.x = x

    # mypy seems perfectly happy with this
    def __add__(self, other: 'A') -> 'A':
        return type(self)(self.x + other.x)

    def __str__(self) -> str:
        return f'A(x={self.x})'


class B(NamedTuple('B', [('x', int)])):

    # error: Argument 1 of "__add__" incompatible with supertype "tuple"
    def __add__(self, other: 'B') -> 'B':
        return type(self)(self.x + other.x)

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