This module provides a decorator for indicating named arguments are keyword-only arguments.
kwonly
To decorate a function with kwonly, any keyword-only arguments should appear as positional arguments in the signature, after any arguments that should be accepted positionally and before the varargs arguments if one is present. Pass the 0-based index of the first keyword-only argument and all positional arguments at or after that index will be treated as keyword-only arguments.
>>> from kwonly import kwonly >>> @kwonly(0) ... def foo(a, b, c): ... print a, b, c ... >>> foo(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() takes exactly 0 arguments (3 given) >>> foo(c=3, a=1, b=2) 1 2 3 >>> @kwonly(2) ... def foo(a, b, c, d=4): ... print a, b, c, d ... >>> foo(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: foo() takes exactly 2 arguments (3 given) >>> foo(1, 2, c=3) 1 2 3 4 >>> foo(1, 2, d=5, c=3) 1 2 3 5 >>> @kwonly(0) ... def foo(a, b, c, *args): ... print a, b, c, args ... >>> foo(4, 5, 6, a=1, b=2, c=3) 1 2 3 (4, 5, 6)
kwonly is of course not a signature-preserving decorator. The argspec will include the non-keyword-only arguments and their default values and the varargs argument if one is given. If no keyword arguments are given, the argspec will have the keywords set to restricted_kwargs, otherwise it won’t be touched. The keyword-only arguments won’t appear in the argspec at all.
NoDefault
Because the keyword-only arguments are declared as positional arguments and must appear after all actual positional arguments, the presence of default arguments in the positional arguments can interfere with the ability to declare required keyword arguments. In such a case, the NoDefault value can be used to indicate that there isn’t actually a default argument supplied for the function, so if no value is passed in the call a TypeError will be raised.
>>> from kwonly import kwonly, NoDefault >>> @kwonly(2) ... def foo(a, b=2, c=NoDefault, d=4): ... print a, b, c, d ... >>> foo(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 3, in foo TypeError: foo needs keyword-only argument c >>> foo(1, c=3) 1 2 3 4
Python versions
This module enables keyword-only arguments in python 2.x, but also works in python 3. This allows modules to be written for both lines of python without sacrificing the utility of keyword-only arguments or having to maintain two separate versions. The python 3 declaration syntax for keyword-only arguments can’t be used, but python 3 will get the full benefit of keyword-arguments in that the signature of the decorated function will match the function that it logically models.
>>> from kwonly import kwonly, NoDefault
>>> @kwonly(2)
... def foo(a, b=2, c=NoDefault, d=4, *args, **kwargs):
... print(a, b, c, d, args, kwargs)
...
>>> foo(1, 2, 5, 6, c=3, g=7, h=8)
1 2 3 4 (5, 6) {'h': 8, 'g': 7}
>>> foo(1, c=3, g=7, h=8)
1 2 3 4 () {'h': 8, 'g': 7}
>>> def bar(a, b=2, *args, c, d=4, **kwargs):
... print(a, b, c, d, args, kwargs)
...
>>> bar(1, 2, 5, 6, c=3, g=7, h=8)
1 2 3 4 (5, 6) {'h': 8, 'g': 7}
>>> bar(1, c=3, g=7, h=8)
1 2 3 4 () {'h': 8, 'g': 7}
>>> import inspect
>>> inspect.getfullargspec(foo)
FullArgSpec(args=['a', 'b'], varargs='args', varkw='kwargs', defaults=(2,), kwonlyargs=['c', 'd'], kwonlydefaults={'d': 4}, annotations={})
>>> inspect.getfullargspec(bar)
FullArgSpec(args=['a', 'b'], varargs='args', varkw='kwargs', defaults=(2,), kwonlyargs=['c', 'd'], kwonlydefaults={'d': 4}, annotations={})
Metadata
Release files for kwonly 1.0.3
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 |
|---|---|---|---|---|
| kwonly-1.0.3.tar.gz | 15.8 kB | Without provenance | Details for kwonly-1.0.3.tar.gz |
Release files / kwonly-1.0.3.tar.gz
| Download URL | kwonly-1.0.3.tar.gz |
|---|---|
| Size | 15.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38da406255dcf5de2cbb73cfc4674dc831e39a48a0e3045d5aab063b38ffba91
|
|
BLAKE2b-256 checksum How to use checksums |
134ae5a2437b1461880d04acd46099afa6fd8e58850e9b4a26cd3a692694362b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for kwonly 1.0.3
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.