Skip to content

Stubs for e.g. int.__add__ need to be more sophisticated #649

Closed

Description

@gvanrossum

See python/mypy#1684. Looks like we need something like

_TI = TypeVar('_TI', bound=int)
class int(...):
    ...
    def __and__(self, n: _TI) -> _TI: ...
    ...
    def __rand__(self, n: _TI) -> _TI: ...

and ditto for | and ^.o

Activity

  1. JukkaL commented on Nov 1, 2016

    @JukkaL
    Contributor

    I don't think that the example is correct:

    >>> class A(int): pass
    >>> type(A(1) & 1)
    <class 'int'>
    >>> type(1 & A(1))
    <class 'int'>
    

    Maybe we just need to add a bunch of operator method overrides to the stubs for bool?

  2. gvanrossum commented on Nov 11, 2016

    @gvanrossum
    MemberAuthor

    Hm, mypy doesn't like the overloads. This works but makes me feel dirty:

    class bool(int, SupportsInt, SupportsFloat):
        def __init__(self, o: object = ...) -> None: ...
        @overload  # type: ignore
        def __and__(self, n: bool) -> bool: ...
        @overload
        def __and__(self, n: int) -> int: ...
  3. srittau commented on Oct 21, 2018

    @srittau
    Collaborator

    Is this still an issue? At least the code from the original bug report type checks fine with 0.641:

    a = True
    a &= True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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