RETIRED, further work has moved to Debian project infrastructure
Go to file
Morgan Fainberg 15db6f8555 Update requirements.txt for pbr versions to match
Match PBR versions between setup.py and requirements.txt
2016-06-26 13:58:23 -07:00
doc Merge branch 'jamielennox-readme' 2016-01-13 17:21:27 -08:00
positional Fix to preserve argspec 2016-04-15 11:20:47 -05:00
.coveragerc Add tests and support files. 2016-01-12 21:36:03 -08:00
.gitignore Stop ignoring doc/source/api 2016-01-13 17:03:49 -08:00
.testr.conf Add tests and support files. 2016-01-12 21:36:03 -08:00
.travis.yml Add tests and support files. 2016-01-12 21:36:03 -08:00
LICENSE Remove copied copyright 2016-01-14 10:15:12 +11:00
README.rst Fix the python3 demo in README 2016-04-20 23:35:49 +08:00
requirements.txt Update requirements.txt for pbr versions to match 2016-06-26 13:58:23 -07:00
setup.cfg Add Sphinx Documentation 2016-01-13 16:32:15 -08:00
setup.py Remove references to gate 2016-01-14 10:17:39 +11:00
test-requirements.txt Merge pull request #4 from jamielennox/no-gate 2016-01-13 16:35:54 -08:00
tox.ini Add Sphinx Documentation 2016-01-13 16:32:15 -08:00

positional

A decorator which enforces only some args may be passed positionally.

PyPi

Build Status

Documentation Status

The Basics

positional provides a decorator which enforces only some args may be passed positionally. The idea and some of the code was taken from the oauth2 client of the google-api client.

The decorator makes it easy to support Python 3 style key-word only parameters. For example, in Python 3 it is possible to write:

>>> def fn(pos1, *, kwonly1, kwonly2=None):
...     ...

All named parameters after * must be a keyword:

>>> fn(10, 'kw1', 'kw2')  # Raises exception.
>>> fn(10, kwonly1='kw1', kwonly2='kw2')  # Ok.

To replicate this behaviour with the positional decorator you simply specify how many arguments may be passed positionally.

First to import the decorator we typically use:

>> from positional import positional

Replicating the Example above:

>>> @positional(1)
... fn(pos1, kwonly1=None, kwonly2=None):
...     ...

If no default value is provided to a keyword argument, it becomes a required keyword argument:

>>> @positional(0)
... def fn(required_kw):
...     ...

This must be called with the keyword parameter:

>>> fn() # Raises exception
>>> fn(10) # Raises Exception
>>> fn(required_kw=10) # OK

When defining instance or class methods always remember that in python the first positional argument passed is the instance; you will need to account for self and `cls`:

>>> class MyClass(object):
...
...     @positional(2)
...     def my_method(self, pos1, kwonly1=None):
...         ...
...
...     @classmethod
...     @positional(2)
...     def my_method(cls, pos1, kwonly1=None):
...         ...

If you would prefer not to account for self and cls you can use the method and classmethod helpers which do not consider the initial positional argument. So the following class is exactly the same as the one above:

>>> class MyClass(object):
...
...     @positional.method(1)
...     def my_method(self, pos1, kwonly1=None):
...         ...
...
...     @positional.classmethod(1)
...     def my_method(cls, pos1, kwonly1=None):
...         ...

If a value isn't provided to the decorator then it will enforce that every variable without a default value will be required to be a kwarg:

>>> @positional()
... def fn(pos1, kwonly1=None):
...     ...
...
>>> fn(10)  # Ok.
>>> fn(10, 20)  # Raises exception.
>>> fn(10, kwonly1=20)  # Ok.

This behaviour will work with the positional.method and positional.classmethod helper functions as well:

>>> class MyClass(object):
...
...    @positional.classmethod()
...    def my_method(cls, pos1, kwonly1=None):
...        ...
...
>>> MyClass.my_method(10)  # Ok.
>>> MyClass.my_method(10, 20)  # Raises exception.
>>> MyClass.my_method(10, kwonly1=20)  # Ok.

For compatibility reasons you may wish to not always raise an exception so a WARN mode is available. Rather than raise an exception a warning will be emitted.

>>> @positional(1, enforcement=positional.WARN):
... def fn(pos1, kwonly=1):
...     ...

Available modes are:

  • positional.EXCEPT - the default, raise an exception.
  • positional.WARN - emit a warning.