Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: duck typing assert

Thread Tools

RE: duck typing assert

Andriy Kornatskyy
Posts: n/a


Thank you for the comments.

There is definitely a room for improvement, however there are limits. Oneof them is related to decorator that replaces decorated method arguments with something like *args, **kwargs. Here is an example.

def x():
*** def decorate(m):
******* def x(*args, **kwargs):
*********** pass
******* return x
*** return decorate

class Foo(object):

*** @x
*** def bar(self, a):
******* pass

Another one challenge is related to inheritance, or even more complex case: multiple inheritance. I do not believe there is a need to scan whole hierarchy, better apply checks separately, the assert will be more readable.

class IFoo(object):
*** def foo(self):
******* pass

class IBar(IFoo):
*** def bar(self):
******* pass

class Bar(IBar):
*** def foo(self):
******* pass

*** def bar(self):
******* pass

assert looks(Bar).like(IBar)
assert looks(Bar).like(IFoo)

I agree, special methods __?__ should not be considered private... but what is right: know which one are special (how about some custom? e.g. __ctx__) or enforce check for it by explicitly asking for such check?

# 1
assert looks(Foo).like(IFoo, notice=['__len__'])
# 2
assert looks(Foo, notice=['__len__']).like(IFoo)

I believe if is decorated as property, it must remain property, otherwise you need exclude it from checks, thus this way you pay codereviewer attention to that.


Andriy Kornatskyy

> From: Removed)
> Date: Thu, 8 Nov 2012 11:34:45 -0700
> Subject: Re: duck typing assert
> To: (E-Mail Removed)
> On Thu, Nov 8, 2012 at 10:30 AM, Andriy Kornatskyy
> <(E-Mail Removed)> wrote:
> >
> > People who come from strongly typed languages that offer interfaces often are confused by lack of one in Python. Python, being dynamic typing programming language, follows duck typing principal. It can as simple as this:
> >
> > assert looks(Foo).like(IFoo)
> >
> > The post below shows how programmer can assert duck typing between two Python classes:
> >
> >
> >
> > Comments or suggestions are welcome.

> Overall, it looks potentially useful to me. Looking over the
> wheezy.core.introspection source, it appears to ignore special method
> names. For example, if you do:
> class IFoo(object):
> def __len__(self):
> pass
> class Foo(object):
> pass
> assert looks(Foo).like(IFoo)
> I would expect this to fail, but judging from the code it would not,
> as it ignores all names starting with '_'. That makes some sense for
> private names (but on the other hand, why would you declare private
> names in an interface unless you want to enforce their presence?), but
> names like __len__ should not be considered private.
> Another comment I have is on properties, and descriptors in general.
> Does this allow something declared as a property to be implemented as
> a simple class attribute, and vice versa? Or can something declared
> as a property be implemented with some custom descriptor class? I
> think the answer is "no" to both, as the check it performs is
> "t2.__class__ is not t.__class__". I assert though that in general
> the type of a descriptor (that is not a simple class attribute) is not
> as important in duck testing as its API -- and all descriptors have
> basically the same API of __get__, __set__, and __delete__.
> --

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: duck typing assert Ian Kelly Python 0 11-08-2012 06:34 PM
duck duck go peter NZ Computing 1 01-09-2011 05:19 AM
assert(x) and '#define ASSERT(x) assert(x)' Alex Vinokur C Programming 5 11-25-2004 08:48 PM
RE: remove assert statement (Was: Re: PEP new assert idiom) Robert Brewer Python 1 11-07-2004 06:53 PM