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

Unsupported operand types for > ("int" and "Number") #8566

Closed
jwodder opened this issue Mar 20, 2020 · 2 comments
Closed

Unsupported operand types for > ("int" and "Number") #8566

jwodder opened this issue Mar 20, 2020 · 2 comments
Labels

Comments

@jwodder
Copy link

jwodder commented Mar 20, 2020

  • Are you reporting a bug, or opening a feature request? Bug
  • Please insert below the code you are checking with mypy.

Sample 1 (mypy_error.py):

from numbers import Number

def is_very_large(x: Number):
    return x > 9000

Sample 2 (mypy_error2.py):

from numbers import Number

def is_foo(data: dict):
    x = data["x"]
    if isinstance(x, Number) and x < 9000:
        return True
    else:
        return False

Note: Running mypy on Sample 2 succeeds if the dict annotation is removed from data or if the first two lines of the function are replaced with def is_foo(x):.

  • What is the actual behavior/output?
mypy_error2.py:5: error: Unsupported operand types for > ("int" and "Number")
mypy_error.py:4: error: Unsupported operand types for < ("int" and "Number")
Found 2 errors in 2 files (checked 2 source files)

Note that the comparison operators shown in the error messages are the opposite of what appears in the code!

  • What is the behavior/output you expect? No errors, as int and Number should be fully comparable
  • What are the versions of mypy and Python you are using? mypy 0.770, Python 3.7.6
    Do you see the same issue after installing mypy from Git master? Yes (commit 11c6888)
  • What are the mypy flags you are using? None
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 20, 2020

This is false, int and Number are not fully comparable:

 λ cat test51.py  
from numbers import Number

def is_very_large(x: Number) -> None:
    assert isinstance(x, Number)
    return x > 9000

is_very_large(complex(1, -1))
λ python3 test51.py
Traceback (most recent call last):
  File "test51.py", line 7, in <module>
    is_very_large(complex(1, -1))
  File "test51.py", line 5, in is_very_large
    return x > 9000
TypeError: '>' not supported between instances of 'complex' and 'int'

The issues disappear when removing annotations because of the first point in https://mypy.readthedocs.io/en/stable/common_issues.html#no-errors-reported-for-obviously-wrong-code

You might want Real instead? This is documented here: https://docs.python.org/3/library/numbers.html#numbers.Real

@jwodder
Copy link
Author

jwodder commented Mar 20, 2020

My mistake.

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

No branches or pull requests

3 participants