Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gvanrossum opened this issue Nov 1, 2016 · 3 comments
Closed

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

gvanrossum opened this issue Nov 1, 2016 · 3 comments

Comments

@gvanrossum
Copy link
Member

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

@JukkaL
Copy link
Contributor

JukkaL commented Nov 1, 2016

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?

@gvanrossum
Copy link
Member Author

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: ...

@srittau
Copy link
Collaborator

srittau commented Oct 21, 2018

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants