This release is a pre-release and may not be stable for production use.
simulate-argument-binding
A pure Python library to simulate Python's internal argument binding given function signature details, default values, and user-provided arguments/keywords. Useful for code analysis, code generation, and implementing custom dispatch or validation logic.
Installation
pip install simulate-argument-binding
Examples
from __future__ import print_function
from simulate_argument_binding import simulate_argument_binding
# 1. All Required Arguments Provided
binding = simulate_argument_binding(
posonlyargs=['x'],
args=['y'],
vararg=None,
kwonlyargs=['z'],
varkwarg=None,
defaults={},
provided_args=[3, 4],
provided_kwargs={'z': 5}
)
assert list(binding.items()) == [('x', 3), ('y', 4), ('z', 5)]
# 2. Default Values and Overriding
binding = simulate_argument_binding(
posonlyargs=['a'],
args=['b', 'c'],
vararg=None,
kwonlyargs=['d'],
varkwarg=None,
defaults={'b': 2, 'c': 3, 'd': 4},
provided_args=[9, 8], # a=9, b=8 overrides default
provided_kwargs={}
)
assert list(binding.items()) == [('a', 9), ('b', 8), ('c', 3), ('d', 4)]
# 3. Var-positional (`*args`) and Var-keyword (`**kwargs`)
binding = simulate_argument_binding(
posonlyargs=[],
args=['x'],
vararg='args',
kwonlyargs=[],
varkwarg='kwargs',
defaults={},
provided_args=[1, 2, 3, 4],
provided_kwargs={'foo': 10, 'bar': 20}
)
assert list(binding.items()) == [
('x', 1),
('args', (2, 3, 4)),
('kwargs', {'foo': 10, 'bar': 20})
]
# 4. Keyword-only Arguments
binding = simulate_argument_binding(
posonlyargs=[],
args=[],
vararg=None,
kwonlyargs=['k1', 'k2'],
varkwarg=None,
defaults={'k2': 5},
provided_args=[],
provided_kwargs={'k1': 1}
)
assert list(binding.items()) == [
('k1', 1),
('k2', 5)
]
# 5. Detect Missing Required Arguments
try:
simulate_argument_binding(
posonlyargs=['a'],
args=['b'],
vararg=None,
kwonlyargs=[],
varkwarg=None,
defaults={},
provided_args=[],
provided_kwargs={}
)
except TypeError as ex:
assert "Missing positional-only arguments" in str(ex) or "Missing arguments" in str(ex)
else:
assert False, "Should have raised TypeError"
# 6. Detect Duplicate Assignment
try:
simulate_argument_binding(
posonlyargs=[],
args=['x'],
vararg=None,
kwonlyargs=[],
varkwarg=None,
defaults={},
provided_args=[1],
provided_kwargs={'x': 2}
)
except TypeError as ex:
assert "multiple values for argument 'x'" in str(ex)
else:
assert False, "Should have raised TypeError"
# 7. Detect Extra Positional/Keyword Args when Not Allowed
try:
simulate_argument_binding(
posonlyargs=[],
args=['a'],
vararg=None,
kwonlyargs=[],
varkwarg=None,
defaults={},
provided_args=[1, 2],
provided_kwargs={}
)
except TypeError as ex:
assert "extra positional arguments" in str(ex)
else:
assert False, "Should have raised TypeError"
try:
simulate_argument_binding(
posonlyargs=['a'],
args=[],
vararg=None,
kwonlyargs=[],
varkwarg=None,
defaults={},
provided_args=[5],
provided_kwargs={'oops': 1}
)
except TypeError as ex:
assert "extra keyword arguments" in str(ex)
else:
assert False, "Should have raised TypeError"
# 8. Advanced: Mixing All Kinds
binding = simulate_argument_binding(
posonlyargs=['a'],
args=['b'],
vararg='c',
kwonlyargs=['d'],
varkwarg='e',
defaults={'b': 2, 'd': 3},
provided_args=[1, 99, 7, 8, 9], # a=1, b=99, *c=(7,8,9)
provided_kwargs={'d': 42, 'extra1': 'x', 'extra2': 'y'}
)
assert list(binding.items()) == [
('a', 1),
('b', 99),
('c', (7, 8, 9)),
('d', 42),
('e', {'extra1': 'x', 'extra2': 'y'})
]
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Metadata
Release files for simulate-argument-binding 0.1.0a0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simulate_argument_binding-0.1.0a0.tar.gz | 4.4 kB | Without provenance | Details for simulate_argument_binding-0.1.0a0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simulate_argument_binding-0.1.0a0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for simulate_argument_binding-0.1.0a0-py2.py3-none-any.whl |
Total release size: 9.2 kB
Release files / simulate_argument_binding-0.1.0a0.tar.gz
| Download URL | simulate_argument_binding-0.1.0a0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6a48adc6d53b38c2c8a9ad7961bcb4299dbe8551d3bd9cff46cbc7eb4017aaae
|
|
BLAKE2b-256 checksum How to use checksums |
c6c04cbf47a431b1aa3c5decbd77ca58785723f70ba42cdfedfde0248ea07d41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.9
|
Release files / simulate_argument_binding-0.1.0a0-py2.py3-none-any.whl
| Download URL | simulate_argument_binding-0.1.0a0-py2.py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
f6a3b07b95929ae64e4d6166a8c766950d37812b79f5c1672be26d4d4c330e90
|
|
BLAKE2b-256 checksum How to use checksums |
f986b924bb1f69ee403b27b947ea96184358588c4060b808ee21fcc9bcfff5ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.9
|
Security for simulate-argument-binding 0.1.0a0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.