Introduction
There has been lots of changes introduced to the python language in a period of a few years. Some of these changes are internal for example switching from the LL(1) parser to the PEG parser, other changes are more public for example the numerous modifications to the builtin objects and many other added feature.
My main focus however is the changes made to the python syntax. There have been a few modifications to the syntax of python, but these modifications are quite huge when it gets to adoption. The most recent syntax change was the addition of the match
and case
soft keywords which without a doubt have numerous applications.
Not to mention the modification of union type hints to be created with |
, the addition of the walrus operator in python 3.8 and many other examples.
Problem
These changes are sources of evolution to the language, which is a good thing, but we increasingly see code which would want to adopt to this new syntax but cannot change as they would introduce syntax errors for other teams relying on their code and yet running lower versions of python. Great examples are big projects like django and the like, which still use the minimum supported python syntax. And if they’d want to try out something new provided by a new version of python, they’d have to drop support for an older version. Currently Django supports 3.8, 3.9 and 3.10, but still cannot use 3.10 specific features as 3.8 is the minimum version supported, and in doing so, it would introduce syntax errors.
solution
So in accordance to the problem above. I was suggesting the introduction of conditional compilation in python. Conditional compilation in python is not new as stated by Andre in his idea for Python 101 here. My suggestion was to be able to extend the existing machinery for conditional compilation to support this feature.
How it would work
So I was suggesting that a compiler feature be added to the __future__
module. Once someone imports compiler at the top level of the module, then the python compiler would be prepped to be ready for conditional compilation.
Then for the actual conditions. I was suggesting we extend the existing ability of using comments to switch the default encoding of python to support # -*- compileif:version -*-
and # -*- endcompileif -*-
. With this, someone can be able to activate conditional compilation for a specific section of python code in their module.
example usage
from __future__ import compiler
def check (val):
# -*- compileif:3.10 -*-
match val:
case 1:
print("got one")
case _:
print(" got no one")
# -*- compileif:3.8.7 -*-
if val == 1:
print("got one")
else:
print("got no one")
# -*- endcompileif -*-
so that’s how the code would kinda look. The compilerif comment could also support comparative operations such as # -*- compileif: <= 3.9 -*-
.
The other idea I thought about was to be able to extend the compile
builtin function to support conditional compilation. I’ve not thought out much how this would compile sections of code as yet, though I’m sure it’s possible.
pros
- enables quick adoption of new python features
- allows for the framework, library or module to evolve along with the python language
cons
- The new workflow might be hard or confusing to adopt
- I don’t think it can be easily added to more stable versions of python.
So I’m not sure if this is something worth undertaking, I invite your criticisms for this idea.