How to resolve incompatible type Optional type conflict?

87 Views Asked by At

Hi anyone know how to resolve this error in py3? Getting error message like:

"int" has incompatible type "Union[float, int, str, None]"; expected "Union[str, bytes, SupportsInt, SupportsIndex, SupportsTrunc]

My type is an optional value Union[float, int, str, None]. I would like to cast it when it's an int int(VALUE). Union is from typing module

1

There are 1 best solutions below

0
On

Solution found in mypy giving error as incompatible type "Optional[int]"; expected "Union[SupportsFloat, str, bytes, bytearray]

Example

  value = 10 // Type is Union[float, str, int, None], an optional value. Directly use it int(value) will trigger the error message.
  assert isinstance(value, int)
  assert int(value) == 10

Similar to float and str or for the value in the list [int(t) for t in list if isinstance(t, int)]