deadpixi / contracts

An implementation of contracts for Python.
GNU Lesser General Public License v3.0
342 stars 18 forks source link

Bypass class methods in invariant conditions #6

Closed Bernardstanislas closed 6 years ago

Bernardstanislas commented 6 years ago

Context

I have classes with classmethod methods, using invariants on such classes fails since it cannot get a proper self instance.

Suggestion

I suggest such class methods are bypassed by the invariant conditions.

@deadpixi what do you think? :)

deadpixi commented 6 years ago

Works for me. Merged...and fixed to only deal with class methods and work on both Python 2 and 3. :)

deadpixi commented 6 years ago

@Bernardstanislas Please check the latest master and see if it works for you. Thanks.

Bernardstanislas commented 6 years ago

@deadpixi it does not work, I think you made a mistake refactoring the check function: https://github.com/deadpixi/contracts/commit/e38ea7b2e1c7abe53de63301df3a6d4689e352d4#diff-29bd5250e9cbe38f796792663ea86090R499

if ismethod(f):
    return True # Python 2

should be

if ismethod(f):
    return False # Python 2

since you don't want to check class methods

deadpixi commented 6 years ago

@Bernardstanislas Try it now, please. Should work much better, and should be easier to read.

The problem is that "ismethod" is defined differently in Python 2 and 3.