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

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

If you are only using 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

Tested on Python 2.7 and Python 3.6.

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

Pending Release

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.0.0.tar.gz (3.8 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.0.0-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: kwargs-only-1.0.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for kwargs-only-1.0.0.tar.gz
Algorithm Hash digest
SHA256 39729a657dbb61abdadfcef2cc97f12b765c4a6766a7f27b991c3245329f8f1f
MD5 86b9fefdc2f26491f444f0a35c102285
BLAKE2b-256 1cb15367cfea75294be910bca5be7309e005d39ff65cc17c4b0d482e556b2e82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kwargs_only-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2ac51677ae07e8e78137eb9af53b6acc7d66d77490d039f87842762bf5a2e913
MD5 fefb7c75f3947571203e1160f16bcfa6
BLAKE2b-256 219f4415b310802eae064607ce2e762dc452220424dbdcf680346f796203bd58

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