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

Added support for attribute contracts #7

Closed braingram closed 11 years ago

braingram commented 11 years ago

I added some support for attribute contract statements (example below).

Example

import contracts

class A:
    a = 1
    b = 2

a = A()

def test(c):
    print "Testing %s" % c
    print "\t%s" % contracts.check(c, a)
    print "\tPassed"

test('object')
test('object(a:int)')
test('object(a:int;b:int)')
test('object(a:int;b:int,>1)')
AndreaCensi commented 11 years ago

Hi Brett,

I think something like this was needed. "object" seems a bit generic though. How about "attr", like "'attr(a:int;b:int)", or "hasattr"? Or do you have any other suggestion?

braingram commented 11 years ago

I agree that object is a bit generic. I like the 'attr' keyword instead. Renamed, and updated commit.

Excellent module too!

AndreaCensi commented 11 years ago

OK I merged and added some tests.

I didn't allow "attr" by itself.