Skip to main content

ducktypes

Duck typing support library

Rationale

Allows working with objects depending on their current behaviour, not their type or their registered (ABC-like) interface.

Quick example:

from ducktypes.ducktype import Duck

class MyObject(object):
    def some_method(self, a, b):
        pass
    def another_method(self, x):
        pass


class AnotherObject(object):
    def some_method(self, a, b):
        pass
    def another_method(self, a):
        pass

class AnotherDifferentObject(object):
    def some_method(self, a, b, c):
        pass
    def another_method(self, a):
        pass

duck = Duck(MyObject())

print "AnotherObject instances implement MyObject interface: ", duck.maybe_implemented_by(AnotherObject())
print "AnotherDifferentObject instances implement MyObject interface:", duck.maybe_implemented_by(AnotherDifferentObject())

Output:

AnotherObject instances implement MyObject interface:  True
AnotherDifferentObject instances implement MyObject interface: False

Gotchas

  • positional arguments are supposed to be called as positional arguments, and keyword arguments are supposed to be called as positional arguments. If your client code calls keyword arguments as positional ones, signature matching may fail.

  • proxy-everything methods, i.e. (*args, **kwargs)-like signature, will of course deceive our signature detection system.

Todo

  • __getattr__ / __getattribute__ overriding functions

  • subclasses exposing special methods with different signature

  • C-declared functions that don’t expose their signature.

  • implement optional strict checking.

Release files for ducktypes 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ducktypes 0.2.5
File Size Uploaded
ducktypes-0.2.5.zip 16.4 kB Details

Release files / ducktypes-0.2.5.zip

Download URL ducktypes-0.2.5.zip
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a07ea1849da4328c4e18c2f4bf6c89334dd1eb0d563963d057522da675a9a33b
BLAKE2b-256 checksum
How to use checksums
ae3567a8271be75e0171376cc674ead3b08953292fb7f8f6df21259c201ae415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.5 This release

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

0.2.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page