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

int is not compatable with float #11145

Closed
KotlinIsland opened this issue Sep 19, 2021 · 7 comments
Closed

int is not compatable with float #11145

KotlinIsland opened this issue Sep 19, 2021 · 7 comments
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Sep 19, 2021

https://mypy.readthedocs.io/en/stable/duck_type_compatibility.html

int is duck type compatible with float

No it's not:

f: float = 1
f.is_integer()
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Sep 19, 2021
@JelleZijlstra
Copy link
Member

Probably the best solution is to add the is_integer() method to int. This is not the right issue tracker for that, though.

@KotlinIsland
Copy link
Contributor Author

for method in dir(float):
    try:
        getattr(int, method)
    except AttributeError as e:
        print(e)
type object 'int' has no attribute '__getformat__'
type object 'int' has no attribute '__set_format__'
type object 'int' has no attribute 'fromhex'
type object 'int' has no attribute 'hex'
type object 'int' has no attribute 'is_integer'

@KotlinIsland
Copy link
Contributor Author

bytearray and memoryview are duck type compatible with bytes.

for method in dir(bytes):
    try:
        getattr(memoryview, method)
    except AttributeError as e:
        print(e)
type object 'memoryview' has no attribute '__add__'
type object 'memoryview' has no attribute '__contains__'
type object 'memoryview' has no attribute '__getnewargs__'
type object 'memoryview' has no attribute '__mod__'
type object 'memoryview' has no attribute '__mul__'
type object 'memoryview' has no attribute '__rmod__'
type object 'memoryview' has no attribute '__rmul__'
type object 'memoryview' has no attribute 'capitalize'
type object 'memoryview' has no attribute 'center'
type object 'memoryview' has no attribute 'count'
type object 'memoryview' has no attribute 'decode'
type object 'memoryview' has no attribute 'endswith'
type object 'memoryview' has no attribute 'expandtabs'
type object 'memoryview' has no attribute 'find'
type object 'memoryview' has no attribute 'fromhex'
type object 'memoryview' has no attribute 'index'
type object 'memoryview' has no attribute 'isalnum'
type object 'memoryview' has no attribute 'isalpha'
type object 'memoryview' has no attribute 'isascii'
type object 'memoryview' has no attribute 'isdigit'
type object 'memoryview' has no attribute 'islower'
type object 'memoryview' has no attribute 'isspace'
type object 'memoryview' has no attribute 'istitle'
type object 'memoryview' has no attribute 'isupper'
type object 'memoryview' has no attribute 'join'
type object 'memoryview' has no attribute 'ljust'
type object 'memoryview' has no attribute 'lower'
type object 'memoryview' has no attribute 'lstrip'
type object 'memoryview' has no attribute 'maketrans'
type object 'memoryview' has no attribute 'partition'
type object 'memoryview' has no attribute 'removeprefix'
type object 'memoryview' has no attribute 'removesuffix'
type object 'memoryview' has no attribute 'replace'
type object 'memoryview' has no attribute 'rfind'
type object 'memoryview' has no attribute 'rindex'
type object 'memoryview' has no attribute 'rjust'
type object 'memoryview' has no attribute 'rpartition'
type object 'memoryview' has no attribute 'rsplit'
type object 'memoryview' has no attribute 'rstrip'
type object 'memoryview' has no attribute 'split'
type object 'memoryview' has no attribute 'splitlines'
type object 'memoryview' has no attribute 'startswith'
type object 'memoryview' has no attribute 'strip'
type object 'memoryview' has no attribute 'swapcase'
type object 'memoryview' has no attribute 'title'
type object 'memoryview' has no attribute 'translate'
type object 'memoryview' has no attribute 'upper'
type object 'memoryview' has no attribute 'zfill'

@A5rocks
Copy link
Contributor

A5rocks commented Sep 20, 2021

FYI int being accepted as float at least is specified by PEP 484: https://www.python.org/dev/peps/pep-0484/#the-numeric-tower .

@KotlinIsland
Copy link
Contributor Author

I think this functionality is absurd, nowhere else is duck typing used. It seems extremely inconsistent.

@sobolevn
Copy link
Member

sobolevn commented Sep 20, 2021

Related #3195 and #8363

@hauntsaninja
Copy link
Collaborator

typing-sig is a better place for this discussion (or bugs.python.org if the solution is to add new methods to builtins). mypy can't make changes to behaviour that is specified by PEPs.

The bool stuff is separate, since bool actually is a subtype of int at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

5 participants