question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please use int and float instead of numbers.Integral/Real

See original GitHub issue

PEP 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
positacommented, Jan 10, 2017

If you really believe that Stone has important uses for numpy numbers in annotations then maybe we need to talk …

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 subclasses long.)

Personally I think PEP 3141 is a dead end. Very few people import the numbers module to use with isinstance() or issubclass() and even fewer use it to implement their own numeric types; and very few people writing numeric code care whether their code works for numbers that aren’t instances of the concrete classes they themselves use regularly.

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

0reactions
zero323commented, Feb 8, 2017

Thanks @gvanrossum.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found