Search code examples
pythonpython-3.xpython-3.7python-3.8pep570

Positional only parameters compatibility with python 3.7


I want to use the new positional only arguments syntax defined in PEP570, but I also want to maintain compatibility with python 3.7 (directly running the script with def f(a, /, b): directly results in a syntax error). Is there anyway to do this?

If there isn't, for package maintainers, do they have to refrain from using the new feature until python 3.7 support is dropped?


Solution

  • Short answer: No, there is no way to do this.

    I suppose technically you might be able to rig up a hack in your setup.py to programmatically remove the positional-only syntax when installed on an older version of Python. But that's getting into extreme kludge territory; in practice, your choices are:

    1. Don't use positional-only parameters until 3.7 is out of support
    2. Maintain separate code bases for 3.7 and earlier vs. 3.8+
    3. Don't support 3.7 and earlier in your own project even though it's still a supported version of Python; require users to update (the effort involved in updating from 3.x to 3.x+1 is not extreme; this isn't like dropping support for Python 2 ten years ago or anything)