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

Boolean values are allowed to be returned when int required. #53

Open CodyKochmann opened 7 years ago

CodyKochmann commented 7 years ago

This small example should be all that is needed to explain the problem.

from contracts import contract

@contract(url=str, returns=int)
def validate_url(url=""):
    """ returns True if the input is a valid url """
    return False

print validate_url("google.com")
# False
asmodehn commented 7 years ago

FYI booleans are integer in python :

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> isinstance(True, int)
True
>>> False == 0
True
>>> True == 1
True

So I dont think much can be done here...

CodyKochmann commented 7 years ago

Would it be crazy to say if booleans specifically are being specified this would be used for the test instead?

type(True) == bool

Its a little less flexible but being that design by contract is a little more strict than the general python, I feel like its appropriate.