multiarg-dispatch 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- multiarg_dispatch-0.1.0/.github/CODEOWNERS +2 -0
- multiarg_dispatch-0.1.0/.github/workflows/cd.yml +32 -0
- multiarg_dispatch-0.1.0/.github/workflows/ci.yml +58 -0
- multiarg_dispatch-0.1.0/.gitignore +174 -0
- multiarg_dispatch-0.1.0/.vscode/settings.json +27 -0
- multiarg_dispatch-0.1.0/CHANGELOG.md +18 -0
- multiarg_dispatch-0.1.0/LICENSE +21 -0
- multiarg_dispatch-0.1.0/PKG-INFO +109 -0
- multiarg_dispatch-0.1.0/README.md +85 -0
- multiarg_dispatch-0.1.0/multiarg_dispatch/__init__.py +18 -0
- multiarg_dispatch-0.1.0/multiarg_dispatch/exceptions.py +34 -0
- multiarg_dispatch-0.1.0/multiarg_dispatch/main.py +145 -0
- multiarg_dispatch-0.1.0/multiarg_dispatch/main.pyi +16 -0
- multiarg_dispatch-0.1.0/multiarg_dispatch/py.typed +0 -0
- multiarg_dispatch-0.1.0/pyproject.toml +53 -0
- multiarg_dispatch-0.1.0/tests/__init__.py +0 -0
- multiarg_dispatch-0.1.0/tests/test_multidispatch.py +243 -0
- multiarg_dispatch-0.1.0/uv.lock +249 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
publish:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout code
|
|
14
|
+
uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Install uv
|
|
17
|
+
uses: astral-sh/setup-uv@v4
|
|
18
|
+
with:
|
|
19
|
+
enable-cache: true
|
|
20
|
+
|
|
21
|
+
- name: Set up Python
|
|
22
|
+
uses: actions/setup-python@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: '3.x'
|
|
25
|
+
|
|
26
|
+
- name: Build package
|
|
27
|
+
run: uv build
|
|
28
|
+
|
|
29
|
+
- name: Publish to PyPI
|
|
30
|
+
env:
|
|
31
|
+
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
32
|
+
run: uv publish --token $UV_PUBLISH_TOKEN
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
lint:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Set up Python 3.14
|
|
16
|
+
uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.14"
|
|
19
|
+
|
|
20
|
+
- name: Install uv
|
|
21
|
+
uses: astral-sh/setup-uv@v4
|
|
22
|
+
with:
|
|
23
|
+
enable-cache: true
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
- name: Install dependencies
|
|
27
|
+
run: uv sync --frozen --dev
|
|
28
|
+
|
|
29
|
+
- name: Run ruff
|
|
30
|
+
run: uv run ruff check multiarg_dispatch/
|
|
31
|
+
|
|
32
|
+
- name: Run mypy
|
|
33
|
+
run: uv run mypy multiarg_dispatch/
|
|
34
|
+
|
|
35
|
+
test:
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
strategy:
|
|
38
|
+
matrix:
|
|
39
|
+
python-version: ["3.11", "3.12", "3.13", "3.14"]
|
|
40
|
+
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/checkout@v4
|
|
43
|
+
|
|
44
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
45
|
+
uses: actions/setup-python@v5
|
|
46
|
+
with:
|
|
47
|
+
python-version: ${{ matrix.python-version }}
|
|
48
|
+
|
|
49
|
+
- name: Install uv
|
|
50
|
+
uses: astral-sh/setup-uv@v4
|
|
51
|
+
with:
|
|
52
|
+
enable-cache: true
|
|
53
|
+
|
|
54
|
+
- name: Install dependencies
|
|
55
|
+
run: uv sync --frozen --dev
|
|
56
|
+
|
|
57
|
+
- name: Run pytest
|
|
58
|
+
run: uv run pytest tests/ -v
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# PyPI configuration file
|
|
174
|
+
.pypirc
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"editor.formatOnPaste": false,
|
|
3
|
+
"editor.formatOnSave": true,
|
|
4
|
+
"editor.formatOnType": false,
|
|
5
|
+
"files.trimTrailingWhitespace": true,
|
|
6
|
+
"editor.codeActionsOnSave": {
|
|
7
|
+
"source.fixAll": "explicit"
|
|
8
|
+
},
|
|
9
|
+
"[python]": {
|
|
10
|
+
"editor.codeActionsOnSave": {
|
|
11
|
+
"source.organizeImports": "explicit"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"ruff.lineLength": 88,
|
|
15
|
+
"ruff.lint.select": [
|
|
16
|
+
"E",
|
|
17
|
+
"F",
|
|
18
|
+
"I"
|
|
19
|
+
],
|
|
20
|
+
"ruff.lint.ignore": [
|
|
21
|
+
"F401"
|
|
22
|
+
],
|
|
23
|
+
"isort.args": [
|
|
24
|
+
"--profile",
|
|
25
|
+
"black"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-03-03
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Initial release
|
|
12
|
+
- Multi-argument dispatch functionality based on type hints for all arguments
|
|
13
|
+
- Support for union types in type hints
|
|
14
|
+
- Type checking enforced at registration
|
|
15
|
+
- Warning system for arguments with default values
|
|
16
|
+
- Fully compatible with Python 3.11+
|
|
17
|
+
|
|
18
|
+
[0.1.0]: https://github.com/gblokker/multiarg-dispatch/releases/tag/v0.1.0
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Goof Blokker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: multiarg-dispatch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A replacement function for single dispatch that allows dispatching based on type hints for all arguments.
|
|
5
|
+
Project-URL: Homepage, https://github.com/gblokker/multiarg-dispatch
|
|
6
|
+
Project-URL: Repository, https://github.com/gblokker/multiarg-dispatch
|
|
7
|
+
Project-URL: Issues, https://github.com/gblokker/multiarg-dispatch/issues
|
|
8
|
+
Author-email: Goof Blokker <goofb@live.nl>
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: dispatch,functools,generic functions,multidispatch,type hints
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# multiarg-dispatch
|
|
26
|
+
|
|
27
|
+
`multiarg-dispatch` is a Python package that extends the functionality of `functools.singledispatch` to allow dispatching based on **type hints for all arguments**, not just the first one. It provides a simple decorator to define generic functions with multiple implementations depending on argument types.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
- Dispatch functions based on the types of **all arguments**, including keyword arguments.
|
|
32
|
+
- Supports **union types** in type hints.
|
|
33
|
+
- Raises a warning if arguments have default values (since defaults are not considered during dispatch).
|
|
34
|
+
- Type checking enforced at registration: all parameters must have type hints.
|
|
35
|
+
- Fully compatible with Python >3.11.
|
|
36
|
+
- **Note** that the registry uses strong references, so for garbage collection do not forget to delete the function that uses multidispatch.
|
|
37
|
+
- **Note** that local classes cannot be used as type hints, since these are accessible globally to retrieve as type hint.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install from PyPI:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install multiarg-dispatch
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Or with uv:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
uv add multiarg-dispatch
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from multiarg_dispatch import multidispatch, DispatchWarning
|
|
57
|
+
|
|
58
|
+
@multidispatch
|
|
59
|
+
def test_func(a, b=None):
|
|
60
|
+
return "default"
|
|
61
|
+
|
|
62
|
+
@test_func.register
|
|
63
|
+
def _(a: int, b: str = "default") -> str:
|
|
64
|
+
return f"int:{a},str:{b}"
|
|
65
|
+
|
|
66
|
+
@test_func.register
|
|
67
|
+
def _(a: str, b: list = []) -> str:
|
|
68
|
+
if b is None:
|
|
69
|
+
b = []
|
|
70
|
+
return f"str:{a},list:{b}"
|
|
71
|
+
|
|
72
|
+
@test_func.register
|
|
73
|
+
def _(a: float, b: str | list = "default") -> str:
|
|
74
|
+
return f"float:{a},union:{b}"
|
|
75
|
+
|
|
76
|
+
print(test_func(10)) # int:10,str:default
|
|
77
|
+
print(test_func("hello", [])) # str:hello,list:[]
|
|
78
|
+
print(test_func(3.14, "extra")) # float:3.14,union:extra
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## API
|
|
82
|
+
|
|
83
|
+
### `multidispatch(func)`
|
|
84
|
+
|
|
85
|
+
Decorator to make a function multi-dispatch capable.
|
|
86
|
+
|
|
87
|
+
* `register(func)`: Register a new implementation based on type hints.
|
|
88
|
+
* `dispatch(cls)`: Retrieve the implementation for given types.
|
|
89
|
+
* `registry`: Read-only view of all registered implementations.
|
|
90
|
+
|
|
91
|
+
### `DispatchWarning`
|
|
92
|
+
|
|
93
|
+
Warning raised when dispatching might be affected by defaults.
|
|
94
|
+
|
|
95
|
+
## Contributing
|
|
96
|
+
|
|
97
|
+
Contributions are welcome! Please follow these guidelines:
|
|
98
|
+
|
|
99
|
+
1. Fork the repository.
|
|
100
|
+
2. Create a feature branch (`git checkout -b feature/my-feature`).
|
|
101
|
+
3. Make your changes.
|
|
102
|
+
4. Ensure all tests pass (`pytest` recommended).
|
|
103
|
+
5. Open a Pull Request with a clear description of your changes.
|
|
104
|
+
|
|
105
|
+
Please follow [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guidelines and include type hints where appropriate.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# multiarg-dispatch
|
|
2
|
+
|
|
3
|
+
`multiarg-dispatch` is a Python package that extends the functionality of `functools.singledispatch` to allow dispatching based on **type hints for all arguments**, not just the first one. It provides a simple decorator to define generic functions with multiple implementations depending on argument types.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Dispatch functions based on the types of **all arguments**, including keyword arguments.
|
|
8
|
+
- Supports **union types** in type hints.
|
|
9
|
+
- Raises a warning if arguments have default values (since defaults are not considered during dispatch).
|
|
10
|
+
- Type checking enforced at registration: all parameters must have type hints.
|
|
11
|
+
- Fully compatible with Python >3.11.
|
|
12
|
+
- **Note** that the registry uses strong references, so for garbage collection do not forget to delete the function that uses multidispatch.
|
|
13
|
+
- **Note** that local classes cannot be used as type hints, since these are accessible globally to retrieve as type hint.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Install from PyPI:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install multiarg-dispatch
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or with uv:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
uv add multiarg-dispatch
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from multiarg_dispatch import multidispatch, DispatchWarning
|
|
33
|
+
|
|
34
|
+
@multidispatch
|
|
35
|
+
def test_func(a, b=None):
|
|
36
|
+
return "default"
|
|
37
|
+
|
|
38
|
+
@test_func.register
|
|
39
|
+
def _(a: int, b: str = "default") -> str:
|
|
40
|
+
return f"int:{a},str:{b}"
|
|
41
|
+
|
|
42
|
+
@test_func.register
|
|
43
|
+
def _(a: str, b: list = []) -> str:
|
|
44
|
+
if b is None:
|
|
45
|
+
b = []
|
|
46
|
+
return f"str:{a},list:{b}"
|
|
47
|
+
|
|
48
|
+
@test_func.register
|
|
49
|
+
def _(a: float, b: str | list = "default") -> str:
|
|
50
|
+
return f"float:{a},union:{b}"
|
|
51
|
+
|
|
52
|
+
print(test_func(10)) # int:10,str:default
|
|
53
|
+
print(test_func("hello", [])) # str:hello,list:[]
|
|
54
|
+
print(test_func(3.14, "extra")) # float:3.14,union:extra
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## API
|
|
58
|
+
|
|
59
|
+
### `multidispatch(func)`
|
|
60
|
+
|
|
61
|
+
Decorator to make a function multi-dispatch capable.
|
|
62
|
+
|
|
63
|
+
* `register(func)`: Register a new implementation based on type hints.
|
|
64
|
+
* `dispatch(cls)`: Retrieve the implementation for given types.
|
|
65
|
+
* `registry`: Read-only view of all registered implementations.
|
|
66
|
+
|
|
67
|
+
### `DispatchWarning`
|
|
68
|
+
|
|
69
|
+
Warning raised when dispatching might be affected by defaults.
|
|
70
|
+
|
|
71
|
+
## Contributing
|
|
72
|
+
|
|
73
|
+
Contributions are welcome! Please follow these guidelines:
|
|
74
|
+
|
|
75
|
+
1. Fork the repository.
|
|
76
|
+
2. Create a feature branch (`git checkout -b feature/my-feature`).
|
|
77
|
+
3. Make your changes.
|
|
78
|
+
4. Ensure all tests pass (`pytest` recommended).
|
|
79
|
+
5. Open a Pull Request with a clear description of your changes.
|
|
80
|
+
|
|
81
|
+
Please follow [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guidelines and include type hints where appropriate.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from .exceptions import (
|
|
2
|
+
InvalidTypeAnnotationError,
|
|
3
|
+
MissingTypeAnnotationError,
|
|
4
|
+
MultiArgDispatchError,
|
|
5
|
+
RegistrationError,
|
|
6
|
+
)
|
|
7
|
+
from .main import DispatchWarning, multidispatch
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"multidispatch",
|
|
13
|
+
"DispatchWarning",
|
|
14
|
+
"MultiArgDispatchError",
|
|
15
|
+
"MissingTypeAnnotationError",
|
|
16
|
+
"InvalidTypeAnnotationError",
|
|
17
|
+
"RegistrationError",
|
|
18
|
+
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Custom exceptions for multiarg-dispatch."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MultiArgDispatchError(Exception):
|
|
5
|
+
"""Base exception for all multiarg-dispatch errors."""
|
|
6
|
+
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MissingTypeAnnotationError(MultiArgDispatchError, TypeError):
|
|
11
|
+
"""Raised when a function parameter lacks a required type annotation."""
|
|
12
|
+
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class InvalidTypeAnnotationError(MultiArgDispatchError, TypeError):
|
|
17
|
+
"""Raised when a type annotation is not valid for dispatch.
|
|
18
|
+
|
|
19
|
+
Valid dispatch types are:
|
|
20
|
+
- Regular classes (type objects)
|
|
21
|
+
- Union types where all members are classes
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class RegistrationError(MultiArgDispatchError, TypeError):
|
|
28
|
+
"""Raised when function registration fails.
|
|
29
|
+
|
|
30
|
+
This typically occurs when registering a function with a different
|
|
31
|
+
number of parameters than the base function.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
pass
|