Skip to main content

Runtime dispatch with decorator-based overload registration.

Project description

Tree Interval Logo

WizeDispatcher

A lightweight, version-robust Python runtime dispatch library with powerful overload registration and type-based selection

Try it on Replit

✨ Features

  • 🎯 Overload Registration via Decorators
    Register multiple implementations for the same function, method, or property setter, with keyword or positional type constraints.

  • 📝 Type Hint Overrides
    Overloads can specify types in the decorator to override or partially use type hints from the function signature.

  • Partial Type Specification
    Missing type constraints in an overload are automatically filled from the fallback/default implementation.

  • 📊 Weighted Specificity Scoring
    Runtime match scoring system:

    • 3 → Exact type match
    • 2 → Instance match
    • 1Any annotation match
    • 0 → Wildcard match (unspecified param)
    • -1 → No match
  • 🛠 Full Typing Support
    Union, Optional, Literal, generic containers (list[int], tuple[int, ...]), and callable detection.

  • 📦 Method & Property Support
    Works with instance methods, @classmethod, @staticmethod, and property setters.

  • 🚀 Fast Cached Dispatch
    Caches previous matches to speed up repeated calls.

  • 🧩 Varargs & Kwargs Handling
    Fully supports *args and **kwargs in overloads, resolving them according to parameter order.

  • 🐍 Version Robust
    Works consistently across Python 3.8+ with no dependencies.

🚀 Quick Start

from wizedispatcher import dispatch

# Fallback
def greet(name: object) -> str:
    return f"Hello, {name}!"

# Keyword constraint
@dispatch.greet(name=str)
def _(name: str) -> str:
    return f"Hello, {name}, nice to meet you."

# Positional constraint
@dispatch.greet(str, int)
def _(name, age) -> str:
    return f"{name} is {age} years old"

print(greet("Alice"))   # Hello, Alice, nice to meet you.
print(greet("Bob", 30)) # Bob is 30 years old

📊 Weight-Based Evaluation

WizeDispatcherevaluates overloads with a specificity weight system:

Weight Meaning
3 Exact type match
2 Instance match
1 Any annotation match
0 Wildcard (unspecified param)
-1 No match (discarded)

Example:
If two overloads match, the one with the higher total weight is chosen.

🧩 Partial Type Specification

# Default function defines all parameters
def process(a: int, b: str, c: float) -> str:
    return "default"

# Overload defines only 'a', inherits 'b' and 'c' types from default
@dispatch.process(a=str)
def _(a: str, b, c) -> str:
    return f"a is str, b is {type(b)}, c is {type(c)}"

🛠 Methods & Properties

class Converter:
    @property
    def value(self) -> int:
        return self._value

    @value.setter
    def value(self, val: object) -> None:
        self._value = val  # fallback setter

    @dispatch.value(value=int)
    def _(self, value: int) -> None:
        self._value = value * 10

    @dispatch.value(value=str)
    def _(self, value: str) -> None:
        self._value = int(value)

c = Converter()
c.value = 3
print(c.value)  # 30
c.value = "7"
print(c.value)  # 7

📦 Installation

pip install wizedispatcher

📚 Documentation

  • Wiki: Complete documentation in /wizedispatcher_wiki
  • Examples: Ready-to-run demos in /demo

📝 License

This project is licensed under the MIT License - see the LICENSE file.

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

wizedispatcher-0.1.2.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wizedispatcher-0.1.2-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file wizedispatcher-0.1.2.tar.gz.

File metadata

  • Download URL: wizedispatcher-0.1.2.tar.gz
  • Upload date:
  • Size: 36.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for wizedispatcher-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9747ff87b8fc7ac368ceb30f2e8f5522faf7fbe4f4a8251a58bcacdfbc530340
MD5 d28162c6428e1b1cdebab82bc085325e
BLAKE2b-256 95af8e9a0297435f9ae2b25e6606792039c00f4df7c8c0556e647cc0bc4d17e2

See more details on using hashes here.

File details

Details for the file wizedispatcher-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: wizedispatcher-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for wizedispatcher-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 25b780ddab4b28cc4710eafee4f1b034bf82d4d5e79b867b699f8fed786ca28b
MD5 03f8e8d411a28c9c3ba1e9ffa7d65409
BLAKE2b-256 ecc2cad9e5ef4f29748fc2a5591aa10f0e17ea06a9a1f13fc63794c48efe1381

See more details on using hashes here.

Supported by

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