Skip to content

Base class function signature compatibility checks overly strict #2619

Closed
@ambv

Description

@ambv

Actual example of this code can be found in Python 3.6's enum with Flag and IntFlag.

Minimal repro::

from typing import TypeVar

_T = TypeVar('_T')


class Base:
    def __or__(self: _T, other: _T) -> _T: ...
    def __and__(self: _T, other: _T) -> _T: ...
    def __xor__(self: _T, other: _T) -> _T: ...


class Other(int, Base):  # <-- errors here
    def __or__(self: _T, other: Union[int, _T]) -> _T: ...  # type: ignore
    def __and__(self: _T, other: Union[int, _T]) -> _T: ...  # type: ignore
    def __xor__(self: _T, other: Union[int, _T]) -> _T: ...  # type: ignore

Expected behavior: no errors.
Actual behavior:

f:11: error: Definition of "__and__" in base class "int" is incompatible with definition in base class "Base"
f:11: error: Definition of "__or__" in base class "int" is incompatible with definition in base class "Base"
f:11: error: Definition of "__xor__" in base class "int" is incompatible with definition in base class "Base"

The errors would be fine if Other didn't redefine the methods itself. In this case the incompatibility in base classes is irrelevant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions