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.1.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.1-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wizedispatcher-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 0938bf09eaea109b6a0b7cc99cdd01d02e5696efe9186cbfdcf655927588099a
MD5 3eed0d7e89b4f6e0c025098302d7c5bb
BLAKE2b-256 0aa00d770bfc7919cbea46bca4ff84377a609580726807798fcb533796b853ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wizedispatcher-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f792c1452f34ed9ee1b7f15cc074a25fe3b0975f1f5d82b30f748e9153591f93
MD5 a9b917ecbbf9da63da5f0f9d11649273
BLAKE2b-256 286df064a3ea3a13167aef9920d2a47352ecb856378249f5b464a76f42b9edb7

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