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.
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
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 errorTypeError: 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
ofCheckType
for the values ofself.types
: if a parametrized tuple is used in the comparison, the value ofself.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