Skip to content

divmod not returning correct types #10849

@Dr-Irv

Description

@Dr-Irv

Describe the bug
If creating overloads for __divmod__() that should be applied with divmod, the wrong types are returned when using divmod

Code or Screenshots
File divmod.pyi :

from typing import overload

class LHS:
    @overload
    def __divmod__(self, other: RHS) -> tuple[RHS, RHS]: ...  # type: ignore[overload-overlap]
    @overload
    def __divmod__(self, other: object) -> tuple[LHS, LHS]: ...

class RHS:
    @overload
    def __divmod__(self, other: LHS) -> tuple[LHS, LHS]: ...  # type: ignore[overload-overlap]
    @overload
    def __divmod__(self, other: object) -> tuple[RHS, RHS]: ...

File divmodtst.py :

from divmod import LHS, RHS

lhs = LHS()
rhs = RHS()

reveal_type(divmod(lhs, rhs))
reveal_type(lhs.__divmod__(rhs))

pyright divmodtst.py has this:

divmodtst.py:1:6 - warning: Import "divmod" could not be resolved from source (reportMissingModuleSource)
divmodtst.py:6:13 - information: Type of "divmod(lhs, rhs)" is "tuple[RHS, RHS] | tuple[LHS, LHS]"
  c:\Code\Misc\pytypes\divmodtst.py:7:13 - information: Type of "lhs.__divmod__(rhs)" is "tuple[RHS, RHS]"

mypy divmodtst.py has this:

divmodtst.py:6: note: Revealed type is "tuple[divmod.RHS, divmod.RHS]"
divmodtst.py:7: note: Revealed type is "tuple[divmod.RHS, divmod.RHS]"

Note that the results for pyright for calling divmod vs. __divmod__() are different. I think they should be the same.

VS Code extension or command-line

pyright command linen version 1.1.404, python 3.11

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