how to assert that method accepts specific types

D

Darren Dale

I would like to assert that a method accepts certain types. I have a
short example that works:

from functools import wraps

def accepts(*types):
def check_accepts(f):
@wraps(f)
def new_f(self, other):
assert isinstance(other, types), \
"arg %r does not match %s" % (other, types)
return f(self, other)
return new_f
return check_accepts

class Test(object):

@accepts(int)
def check(self, obj):
print obj

t = Test()
t.check(1)

but now I want Test.check to accept an instance of Test as well. Does
anyone know how this can be accomplished? The following class
definition for Test raises a NameError:

class Test(object):

@accepts(int, Test)
def check(self, obj):
print obj

Thanks,
Darren
 
C

Chris Rebert

I would like to assert that a method accepts certain types. I have a
short example that works:

from functools import wraps

def accepts(*types):
def check_accepts(f):
@wraps(f)
def new_f(self, other):
assert isinstance(other, types), \
"arg %r does not match %s" % (other, types)
return f(self, other)
return new_f
return check_accepts

class Test(object):

@accepts(int)
def check(self, obj):
print obj

t = Test()
t.check(1)

but now I want Test.check to accept an instance of Test as well. Does
anyone know how this can be accomplished? The following class
definition for Test raises a NameError:

class Test(object):

@accepts(int, Test)
def check(self, obj):
print obj

You're going to have to either inject it after the class definition
somehow, or give the class name as a string and use eval() or similar.
The class object doesn't exist until the entire class body has
finished executing, so you can't refer to the class within its own
body.

Cheers,
Chris
 
D

Darren Dale

You're going to have to either inject it after the class definition
somehow, or give the class name as a string and use eval() or similar.
The class object doesn't exist until the entire class body has
finished executing, so you can't refer to the class within its own
body.

Thats too bad, thanks for clarifying.
 
T

Terry Reedy

Darren said:
I would like to assert that a method accepts certain types. I have a
short example that works:

from functools import wraps

def accepts(*types):
def check_accepts(f):
@wraps(f)
def new_f(self, other):
assert isinstance(other, types), \
"arg %r does not match %s" % (other, types)
return f(self, other)
return new_f
return check_accepts

class Test(object):

@accepts(int)
def check(self, obj):
print obj

t = Test()
t.check(1)

but now I want Test.check to accept an instance of Test as well. Does
anyone know how this can be accomplished? The following class
definition for Test raises a NameError:

class Test(object):

@accepts(int, Test)
def check(self, obj):
print obj

Because Test does not exist at the time the function is compiled.
Remove '@accepts...' and put Test.check = accepts(int, Test)(Test.check)
after the class definition and it should work.

tjr
 
R

Rhodri James

I would like to assert that a method accepts certain types. I have a
short example that works:

from functools import wraps

def accepts(*types):
def check_accepts(f):
@wraps(f)
def new_f(self, other):
assert isinstance(other, types), \
"arg %r does not match %s" % (other, types)
return f(self, other)
return new_f
return check_accepts

class Test(object):

@accepts(int)
def check(self, obj):
print obj

t = Test()
t.check(1)

but now I want Test.check to accept an instance of Test as well. Does
anyone know how this can be accomplished? The following class
definition for Test raises a NameError:

class Test(object):

@accepts(int, Test)
def check(self, obj):
print obj

An icky but relatively clear way to get around this is to gratuitously
subclass Test:

class AcceptableTest(object):
pass

class Test(AcceptableTest):
@accepts(int, AcceptableTest)
def check(self, obj):
print obj
 
A

Aahz

class Test(object):
@accepts(int)
def check(self, obj):
print obj

t = Test()
t.check(1)

but now I want Test.check to accept an instance of Test as well. Does
anyone know how this can be accomplished? The following class
definition for Test raises a NameError:

class Test(object):
@accepts(int, Test)
def check(self, obj):
print obj

Are you using Python 2.6 or later? You could probably write a tricky
class decorator that re-wraps all wrapped methods....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top