0

I'd like a builtin type that doesn't require the numpy dependency, but will allow me to express the fact that a variable is a real number. That is, an integer, a float, builtin or any of the fancy numpy ones (numpy.int16, numpy.float32, etc.).

Now, numbers.Real is almost right since instance and subclass checks work:

from numbers import Real

assert isinstance(10.0, Real)
assert issubclass(type(10.0), Real)
assert isinstance(np.int16(4), Real)
assert not isinstance(2 + 3j, Real)

Yet the following code:

import random
import numbers


def foo(x: numbers.Real, y: numbers.Real):
    return random.uniform(x, y)

makes the linter complain that Expected type 'float', got 'Real' instead.

thorwhalen
  • 1,920
  • 14
  • 26
  • 1
    Related: https://stackoverflow.com/q/63894187/3001761, https://stackoverflow.com/q/50928592/3001761 – jonrsharpe Jul 06 '22 at 15:47
  • 1
    It looks like [PEP](https://peps.python.org/pep-0484/#the-numeric-tower) says to just use `float` for the type hinting. – blackbrandt Jul 06 '22 at 15:49

0 Answers0