Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

https://img.shields.io/travis/adamchainz/kwargs-only/master.svg https://img.shields.io/pypi/v/kwargs-only.svg https://img.shields.io/badge/code%20style-black-000000.svg

Unmaintained: I’m no longer maintaining this package because it’s for Python 2 compatibility and all other projects I’ve used it on are now Python 3 only. If you want to continue maintenance please contact me.


A decorator to make a function accept keyword arguments only, on both Python 2 and 3.

If you are using only Python 3, you don’t need this, you can just do:

def myfunction(*, foo=1, bar=2):
    pass

Why?

If you are making a library (that still supports Python 2), you might want to make all functions in its API take keyword arguments only, for a couple of reasons:

  • To avoid user confusion, e.g. if you take x and y arguments as coordinates, it’s easy to forget which way round to pass them.

  • To make your API easier to extend - you’ll know no callers rely on the positional argument ordering, so can refactor this to make sense.

Installation

Use pip:

pip install kwargs-only

Python 2.7 and 3.4 to 3.8 supported.

Usage

Import the decorator and apply it to a function:

from kwargs_only import kwargs_only

@kwargs_only
def myfunction(foo=1, bar=2):
    pass

Then calling the function with positional arguments will cause it to fail with TypeError:

>>> myfunction(1, 2)
...
TypeError: myfunction should only be called with keyword args

The decorator detects methods and classmethods, by allowing for the first argument to be a positional one if its name is self or cls. kwargs_only should be applied to the function before classmethod is. For example:

class MyClass:

    @classmethod
    @kwargs_only
    def my_class_method(cls, foo=1):
        pass

    @kwargs_only
    def my_instance_method(self, bar=1):
        pass

That’s about all there is to it! Enjoy!

History

1.1.1 (2019-12-21)

  • Update PyPI development status as inactive. This package is no longer maintained since I have copied it into my mone Python 2 compatible project. I recommend you do the same going forwards - and get off Python 2!

1.1.0 (2019-11-15)

  • Support Python 3.8.

1.0.2 (2019-10-28)

  • Update PyPI development status as active. This package is maintained again since I have started using it on a Python 2 compatible project :)

1.0.1 (2019-02-07)

  • Update PyPI development status as inactive. This package is no longer maintained, see README.rst.

1.0.0 (2017-06-18)

  • First release on PyPI, featuring kwargs_only decorator.

Release files for kwargs-only 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kwargs-only 1.1.1
File Size Uploaded
kwargs-only-1.1.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kwargs-only 1.1.1
File Interpreter ABI Platform
kwargs_only-1.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 8.3 kB

Release files / kwargs-only-1.1.1.tar.gz

Download URL kwargs-only-1.1.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
eed949edb0f09c461aa12027bfa019cbc9105091768a13f625f3039f042a36cd
BLAKE2b-256 checksum
How to use checksums
b96b5412355d88016001d34ba8696a2ba14c7e79dab5ad83b02dd799259c86ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

Release files / kwargs_only-1.1.1-py2.py3-none-any.whl

Download URL kwargs_only-1.1.1-py2.py3-none-any.whl
Size 3.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a19facb8c21ad9cc079b63710791dd745643588a9985396c48268368dbce535e
BLAKE2b-256 checksum
How to use checksums
bdaaf6df0d1275ebe9dacd3d542d56aa2111825f92644962eb0918810dbc7238
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page