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

Check type on annotated class #78

Closed Delja closed 4 years ago

Delja commented 5 years ago
def singleton(classe):
    instances = {}
    def get_instance():
        if classe not in instances:
            instances[classe] = classe()
        return instances[classe]
    return get_instance

@singleton
class Annotated: pass

@contract(x=Annotated)
def test(x):
    return x

This code return "I want either a string or a type, not Instance of function: <function singleton..get_instance [...]". This is normal considering that the Annotated call is replaced by singleton (Annotated), but in my case view that the purpose of this annotation is to have a single instance. It would be convenient to be able to use the type of return of the decorator as a type of verification contract.