Skip to main content

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

Project description

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 only using Python 3, you don’t need this, you can just do:

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


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.


Use pip:

pip install kwargs-only

Tested on Python 2.7 and Python 3.7.


Import the decorator and apply it to a function:

from kwargs_only import kwargs_only

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

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:

    def my_class_method(cls, foo=1):

    def my_instance_method(self, bar=1):

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


Pending Release

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.

Filename, size & hash SHA256 hash help File type Python version Upload date
kwargs_only-1.0.1-py2.py3-none-any.whl (3.7 kB) Copy SHA256 hash SHA256 Wheel py2.py3
kwargs-only-1.0.1.tar.gz (4.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page