-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A descriptor for contract attributes #30
base: master
Are you sure you want to change the base?
Conversation
…s to __contracts__
Hi Yaniv, I imagine that this depends on other libraries implementing attributes---I a. On Tuesday, November 18, 2014, Yaniv Ben-Ami [email protected]
(sent from mobile device, please pardon typos and brevity) |
Thanks! I guess we could go with something like "verify", "guard", "watch" or "attach". It would be nice to have it sound like an original python keyword. No, I used just functools and types, both from the standard library. |
On Tue, Nov 18, 2014 at 8:07 PM, Yaniv Ben-Ami [email protected]
Sorry let me reword my comment: I meant, if there is a popular library thanks for the contribution! A. Andrea Censi | LIDS / MIT | http://censi.mit.edu |
BTW, I learned how to do descriptors from Chris Beaumont's excellent writeup (which I found via google). Chris - I changed your design pattern a bit to hold the data in a dict on the instances, rather than on the descriptors. I hope it is as valid, but it would be good if you review. |
@ChrisBeaumont you are the expert with descriptors. What do you think of this implementation? |
This looks good to me! This is very similar to how Traits works (http://code.enthought.com/projects/traits/) I have a few style comments that I'll leave inline. |
""" | ||
class_name=function_.__class__.__name__ | ||
function_=function_.__call__ | ||
function_.__dict__['__name__']=class_name +'.__call__' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to not use function_.__name__ = class_name +'.__call__'
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason it generates an error:
AttributeError: 'instancemethod' object has no attribute 'name'
@AndreaCensi regarding Traits and what to name these: IPython has a "traitlets" module they use, whose API is similar to traits: http://ipython.org/ipython-doc/dev/api/generated/IPython.utils.traitlets.html The problem is that both of these libraries are more "MyPy"-style, in that they have many special-purpose classes like I personally like |
OK. For my own project I need functions, so I'll use my own fork until the arrow notation is ready. But for the pull request I scaled back function support. It's a trivial descriptor now, and should be fairly robust. I also renamed to Attribute. |
I tried to merge this, but the two changes you made to main.py (one involving using partial functions) make tests fail in nonobvious ways. I'm working on this branch: https://github.com/AndreaCensi/contracts/tree/yaniv256-ContractAttribute Run |
Hi Andrea,
I wrote a descriptor for enforcing contracts. From the doc string:
ContractAttribute is a descriptor for object attributes that
enforces a contract check on whatever object or function the
attribute is set to. For a function, the function would not be
passed the self argument (because that breaks encapsulation -
you can still actively pass self if you want). Setting up an
attribute like this is useful when an API that is expressed as
an object requires a client function to work with. In such
cases you want to both communicate expectations with regards
to the needed function and verify that they are met.
Usage example:
Output: