Skip to main content

A Simple pure python library to emulate type overloading

Project description

No AI used during developpement, other than this readme

pyOverloading

A lightweight Python library that brings true function overloading to Python using type hints.

Unlike functools.singledispatch, pyOverloading supports multiple arguments and deep inspection of generic types (e.g., distinguishing between a list[int] and a list[str]).

✨ Features

  • Multi-argument dispatch: Overload functions based on the types of all arguments.
  • Deep Type Inspection: Recursive check for nested types in lists, dicts, sets, and tuples.
  • Union & Any Support: Full compatibility with typing.Union and typing.Any.
  • Decorator-based: No boilerplate, just use @overload.

🚀 Installation

From your project root:

pip install .

💡 Usage & Examples

Basic Overloading

The most common use case: same function name, different scalar types.

from pyOverloading.Overloading import overload

@overload
def process(x: int):
    return f"Processing integer: {x}"

@overload
def process(x: str):
    return f"Processing string: {x}"

print(process(10))    # Output: Processing integer: 10
print(process("hi"))  # Output: Processing string: hi

Advanced: Deep Generic Inspection

pyOverloading goes beyond the surface. It can differentiate between collections based on their content.

from pyOverloading.Overloading import overload
from typing import List

@overload
def handle_data(data: List[int]):
    return sum(data)

@overload
def handle_data(data: List[str]):
    return "-".join(data)

print(handle_data([1, 2, 3]))      # Output: 6
print(handle_data(["a", "b"]))    # Output: a-b

Complex Types (Unions & Dicts)

You can handle complex data structures seamlessly.

from pyOverloading.Overloading import overload
from typing import Union, Dict

@overload
def update_config(conf: Dict[str, int]):
    print("Updating numeric configuration")

@overload
def update_config(val: int | float):
    print(f"Updating single scale value: {val}")

update_config({"timeout": 30}) # Matches Dict[str, int]
update_config(1.5)             # Matches Union[int, float]

🛠 How it works

  1. Registration: When you decorate a function with @overload, it's added to a global registry keyed by its module and qualified name.
  2. Signature Mapping: The library extracts the inspect.signature to map specific types to the correct function implementation.
  3. Runtime Dispatch: When called, the wrapper analyzes the types of the passed arguments (recursively for containers) and executes the best match.

⚠️ Notes

  • Performance: Deep type checking (like list[int]) involves iterating over collection elements. For high-performance loops, prefer specific function names.
  • Type Hints Required: Arguments without type hints are treated as object (matches anything).

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

pyoverloading-0.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

pyoverloading-0.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pyoverloading-0.0.1.tar.gz.

File metadata

  • Download URL: pyoverloading-0.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pyoverloading-0.0.1.tar.gz
Algorithm Hash digest
SHA256 376a5f74f9ed8e1ff6530239b2b0452cacb49ac4c4ca3f9002a478297cd0f694
MD5 ce83abb6b80f598efacc111ee59dc984
BLAKE2b-256 e90c0f098394dc03e9e239c3ef1812e72f5d81b5c56313ece56112b4d56f3b68

See more details on using hashes here.

File details

Details for the file pyoverloading-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyoverloading-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pyoverloading-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cbd3f9640ede373c4354461f79f2c801b11311a6bc1d8a4567f5ee707d9ed3b2
MD5 f390157dede5d637d1dc533c77d568cd
BLAKE2b-256 ebbb2b14a9784b8c73bbf7b18d8f215e0a746eb49e107b5f8822967a530c13ea

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