1

I get the following traceback that mypy throws:

dust/core/validator/_validators.py:504: error: Unsupported operand types for < ("str" and "ValidateProperty")
dust/core/validator/_validators.py:504: error: Unsupported operand types for < ("float" and "ValidateProperty")
dust/core/validator/_validators.py:504: error: Unsupported operand types for < ("float" and "str")
dust/core/validator/_validators.py:504: error: Unsupported operand types for < ("str" and "float")

for the code that goes something like following.

class Value(ValidateProperty):
    def __init__(min_value: VALUE_RANGE=None, max_value: VALUE_RANGE=None):
        if min_value is not None and max_value is not None:
            if max_value < min_value:
                raise ValueError(f"max_value can not be less than min_value")

where VALUE_RANGE is

VALUE_RANGE = typing.Union[FLOAT,STRING, None]

such that:

FLOAT = typing.Union[float, "ValidateProperty", None]
STRING = typing.Union[str, "ValidateProperty", None]

and ValidateProperty is an ABC class with __get__ and __set__ methods.

how to ensure that mypy gets that comapred values are always of same types ?

qw4r9cal
  • 11
  • 2
  • check the type() before comparison? – Ruli Sep 28 '20 at 13:04
  • @Ruli I tried it it can work but the cases can increase and it becomes overwork then, any other typing hint workaround ? – qw4r9cal Sep 28 '20 at 13:07
  • instead you can create a list of types and check if the types are in that list, just an idea – Ruli Sep 28 '20 at 14:03
  • I think you probably want to use [generics](https://mypy.readthedocs.io/en/stable/generics.html#generic-functions). In particular, see [typevars with value restrictions](https://mypy.readthedocs.io/en/stable/generics.html#type-variables-with-value-restriction). – Michael0x2a Sep 28 '20 at 17:13

0 Answers0