Skip to main content

TypeDispatch is a Python utility for registering and dispatching functions based on object types and predicates, supporting inheritance through method resolution order (MRO).

Project description

typedispatch

typedispatch is a Python library that provides a flexible and powerful mechanism for registering functions based on type resolution. It allows you to associate types with functions and dynamically look up functions based on the type of an object, including handling inheritance hierarchies and predicate-based dispatch.

Features

  • Register functions for specific types
  • Automatic type resolution based on inheritance
  • Decorator syntax for easy registration
  • Predicate-based function dispatch
  • Supports multiple inheritance

Installation

pip install typedispatch

Usage

Basic Registration

You can register functions directly with the register method:

from typedispatch import TypeDispatch, TypeDispatchError

typedispatch = TypeDispatch()
typedispatch.register(int, lambda x: f"Processing integer: {x}")

print(typedispatch.lookup(10))  # Output: Processing integer: 10

Using the Decorator

The library provides a convenient decorator for registering functions with specific types:

from typedispatch import TypeDispatch

typedispatch = TypeDispatch()

@typedispatch.register_decorator(int)
def handle_integer(x):
    return f"Handling integer: {x}"

@typedispatch.register_decorator(str)
def handle_string(x):
    return f"Handling string: {x}"

print(typedispatch.lookup(42))        # Output: Handling integer: 42
print(typedispatch.lookup("hello"))   # Output: Handling string: hello

Inheritance-based Dispatch

It automatically handles inheritance hierarchies:

typedispatch.register(list, lambda x: f"List of length {len(x)}")

class MyList(list):
    pass

print(typedispatch.lookup(MyList([1, 2, 3])))  # Output: List of length 3

Predicate-based Dispatch

You can use predicates to further refine the dispatch logic:

@typedispatch.register_decorator(int, a=True)
def handle_a_ints(x):
    return f"A integer {x}"

@typedispatch.register_decorator(int, b=True)
def handle_b_ints(x):
    return f"B integer {x}"

class MyInt(int):
    pass

@typedispatch.register_decorator(MyInt, a=True)
def handle_a_myints(x):
    return f"A MyInt {x}"

print(typedispatch.lookup(2, a=True))          # Output: A integer 2
print(typedispatch.lookup(2, b=True))          # Output: B integer 2
print(typedispatch.lookup(MyInt(2), a=True))   # Output: A MyInt 2
print(typedispatch.lookup(MyInt(2), b=True))   # Output: B integer 2

Error Handling

When no matching function is found, a TypeDispatchError is raised:

try:
    typedispatch.lookup(3.14)  # Assuming no function registered for float
except TypeDispatchError as e:
    print(f"Error: {e}")  # Output: Error: No function found for type <class 'float'> or its superclasses.

API Reference

TypeDispatch

  • register(obj_type, func, **predicate): Register a function for a specific type with optional predicates.
  • register_decorator(obj_type, **predicate): Decorator for registering functions.
  • lookup(obj, **predicate): Find and execute the function associated with the object's type and predicates.
  • is_registered(obj_type): Check if a type or any of its superclasses is registered.

Exceptions

  • TypeDispatchError: Raised when no matching function is found or when an invalid type is registered.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typedispatch-0.5.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

typedispatch-0.5.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file typedispatch-0.5.0.tar.gz.

File metadata

  • Download URL: typedispatch-0.5.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for typedispatch-0.5.0.tar.gz
Algorithm Hash digest
SHA256 af9c83d6ec030eb539cbfea14fdfb5f2841f227b0e89b3b105d83ff0c3c48623
MD5 cba191af042a2b72661e30b76bfa4857
BLAKE2b-256 eb20c82ad5df907c064e2a5bab9c86cd3db3835751b8eec4f1aab40956947de3

See more details on using hashes here.

File details

Details for the file typedispatch-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typedispatch-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63ba8f00604bf2f1454b46fab287bf786074b82b2c2c60a644f1f90c8b14cd52
MD5 3209120aa13540ac68be3a8e1e8f2203
BLAKE2b-256 3289e82bd4c48cd1373a86faa8e4ddfbc0a6ee6f1cc41b9902cbc4087b18109e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page