Please use int and float instead of numbers.Integral/Real
See original GitHub issuePEP 484 intentionally recommends using int/float instead of the numeric ABCs, and mypy doesn’t like them at all:
from numbers import Integral, Real
def f(i: Integral, x: Real) -> Real:
return i * x
f(1, 3.14)
gives these errors:
__tmp__.py:4: error: Argument 1 to "f" has incompatible type "int"; expected "Integral"
__tmp__.py:4: error: Argument 2 to "f" has incompatible type "float"; expected "Real"
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top Results From Across the Web
python - How to accept the input of both int and float types?
number = input("Enter a number: ") nb = None for cast in (int, float): try: nb = cast(number) print(cast) break except ValueError: pass....
Read more >8606 (floats in exponent do not propagate) - Sage Trac
Note the result is an integer, not a float! ... However we would then need a specific method to coerce an integral real...
Read more >Integer and float numbers - Learn Python 3 - Snakify
The division operator / for integers gives a floating-point real number (an ... we read a line with input() and then cast a...
Read more >Combinatorial Functions (GNU Emacs Calc Manual)
If the number is an integer-valued float, the result is a floating-point approximation. If the number is a non-integral real number, the generalized ......
Read more >Integers Floats / Examples / Processing.org
Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m not sure I have an example in mind that isn’t solved by some kind of “casting” before use. (Even Python-Future’s
int
subclasseslong
.)Perhaps I am being (overly) nervous as a “purist”? Other than PEP 484, I can’t find any official suggestion that PEP 3141 should be avoided or treated as deprecated. True, its use is probably rare, or an afterthought, but it just feels dirty that type checking should ever be incompatible with ABCs. But maybe I’ll just up my dose of Sertraline and move on. 😏
(Referencing python/mypy#2636 for background.)
Thanks @gvanrossum.