AndreaCensi / contracts

PyContracts is a Python package that allows to declare constraints on function parameters and return values. Contracts can be specified using Python3 annotations, or inside a docstring. PyContracts supports a basic type system, variables binding, arithmetic constraints, and has several specialized contracts and an extension API.
http://andreacensi.github.io/contracts/
Other
398 stars 62 forks source link

unhandled exception in check_contract #89

Open HitLuca opened 4 years ago

HitLuca commented 4 years ago

when trying to add a @contract decorator to a function that has parametrized tuples as hints (e.g. Tuple[int, float]), breaks contracts with the error TypeError: Parameterized Tuple cannot be used with isinstance().

The first thing would be to return a better error instead of that obscure one, like "cannot add contracts to a function that has parametrized tuples as type hints" or similar.

Second, this issue may be fixed by adding a check in check_contract of CheckType for the values of self.types: if a parametrized tuple is used in the comparison, the value of self.types.__args__ will be a tuple of types composing it ('__args__': (<class 'int'>, <class 'float'>). Knowing that one could loop through every input element in the tuple and compare its type with the expected one