Skip to content

false positive with divmod and overloaded __divmod__ #9835

@jorenham

Description

@jorenham
from typing import Self, overload

class A:
    @overload
    def __divmod__(self, x: Self, /) -> tuple[Self, Self]: ...
    @overload
    def __divmod__(self, x: int, /) -> tuple[int, int]: ...
    def __divmod__(self, x: Self | int, /) -> tuple[Self, Self] | tuple[int, int]:
        return (self, self) if isinstance(x, A) else (x, x)

reveal_type(divmod(A(), 1))

reports two errors:

No overloads for "divmod" match the provided arguments  (reportCallIssue)
Argument of type "Literal[1]" cannot be assigned to parameter "y" of type "SupportsRDivMod[_T_contra@divmod, _T_co@divmod]" in function "divmod"
  "Literal[1]" is incompatible with protocol "SupportsRDivMod[A, _T_co@divmod]"
    "__rdivmod__" is an incompatible type
      Type "(value: int, /) -> tuple[int, int]" is not assignable to type "(other: _T_contra@SupportsRDivMod, /) -> _T_co@SupportsRDivMod"
        Parameter 1: type "_T_contra@SupportsRDivMod" is incompatible with type "int"
          "A" is not assignable to "int"  (reportArgumentType)

pyright playground


mypy behaves correctly, and reports:

revealed type is "tuple[builtins.int, builtins.int]"

mypy playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions