How to typecheck real numbers, and type-safely compare them to ints

292 Views Asked by At

I have a function that inputs a real number. In practice it might be an int or a float. and I want it to check if it is >0

For example

def f(x):
   return x > 0

Now I want that function to be type checked. Since I wanted x to be any real number, I naturally wrote:

from numbers import Real

def f(x: Real) -> bool:
   return x > 0

But problem, according to pyright:

Operator ">" not supported for types "Real" and "Literal[0]"
  Operator ">" not supported for types "Real" and "Literal[0]"
[Pyright: reportGeneralTypeIssues]

Here come my two questions:

  • Why is the comparison between Real and int not defined? Is it an abstract-base-classes-related problem?
  • What type should I use instead? Do I have no choice but to use Union[int, float] (which I find a bit ugly)?
0

There are 0 best solutions below