Skip to main content

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

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

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

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-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.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.

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

kwargs-only-1.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

kwargs_only-1.1.0-py2.py3-none-any.whl (3.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file kwargs-only-1.1.0.tar.gz.

File metadata

  • Download URL: kwargs-only-1.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for kwargs-only-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d8179b663c88b53c686a501e88cb3f4e7a15aa3a5ca9cde4171d646449fe54b6
MD5 15f8b54e119a76edaf03a418076c5ff8
BLAKE2b-256 7f574490c29df06dba059bf16f22ea81671f3c2b1ce0dd37811533f79b11b691

See more details on using hashes here.

File details

Details for the file kwargs_only-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: kwargs_only-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for kwargs_only-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 59cecf1f1a3e7dc340476a4fca80c8bec06336860d381b6728ede6f15637ee20
MD5 e612be3fcb27606ca942a407067ccb40
BLAKE2b-256 ac84e7a76cb5ca876a3190990e0e53c1629ab51227abf7965027149358c25959

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