Package: pep505
Python PEP505GitHub:https://github.com/acaos/python-pep505PyPI:https://pypi.org/project/pep505/This package polyfillsPEP 505's
None-aware operators to Python 3.8 through 3.10.Later versions of Python are expected to be supported, but the grammar (in
this package) will first need to be updated before doing so. Python versions
earlier than 3.8 will not be supported.UsageAfter this package has been installed, it is activated by placing the
following at the top of any module you wish to use null-coalescing
operators in:# -*- parsing: pep505 -*-Note that this will not work for code executed directly from the command
line (e.g.python3 foo.py), but will work if you execute the code as
a module (e.g.python3 -m foo).Technical InformationNull-Coalescing OperatorsThis package adds the four null-coalescing operators from PEP 505. See
that PEP for a fuller treatment of the operators; the below are merely
simple examples.Coalesce Operator (??)The coalesce binary operator??has a higher precedence than any
other binary operator, but a lower precedence than anawaitexpression.The coalesce operator works as follows:A ?? Bis the equivalent of:(Xif(X:=A)isnotNoneelseB)Ais evaluated only once;Bis not evaluated ifAis notNone.Maybe-Assign (??=)The maybe-assign operator??=is a special form of augmented assignment,
which will assign to the left-hand side only if it isNone.The maybe-assign operator works as follows:A ??= Bis the equivalent
of:ifAisNone:A=BBis not evaluated ifAis notNone.Maybe-Dot (?.)The maybe-dot operator?.preventsAttributeErrorfrom occuring if
the object for which an attribute is being accessed isNone; instead,
the expression evaluates toNone.The maybe-dot operator works as follows:A?.Bis the equivalent of:(X.Bif(X:=A)isnotNoneelseNone)Ais evaluated only once.Maybe-Subscript (?[)The maybe-subscript operator?[preventsTypeErrorfrom occuring if
the object for which an item is being accessed isNone; instead,
the expression evaluates toNone.The maybe-subscript operator works as follows:A?[B]is the equivalent of:(X[B]if(X:=A)isnotNoneelseNone)Ais evaluated only once;Bis not evaluated ifAis notNone.How the PEP505 Package WorksThis package works by registering an import hook and taking over the
AST parsing of Python modules, using a modified version of the Python
tokenizer and AST.The four operators above are transformed into an AST compatible with
any version of Python 3.8 or later. The modified grammar can be found
insrc/grammar/python_pep505.gram, and compiled with the normal:python3-mpegensrc/grammar/python_pep505.gram-osrc/pep505/parser.pyNote thatparser.pyis then further modified slightly to use the local
version of thepegenpackage (replacingpegen.imports with.pegen.).Temporary VariablesIn order to function, this module generates temporary variable names
of the form__pep505_<lineno>_<column-offset>. These are unlikely
to conflict with any existing variables, but be aware of their existence.Installing from SourceThis package depends on theparsinghookpackage. If that package is also installed from source, it is necessary to
install theparsinghook.pthfile from that package insite-packages.Running the TestsThe tests can be run withpython3 -m pep505.test.Thanks and CreditsThis package stands on the shoulders of giants.PEP 505 was authored by Mark E. Haase and Steve Dower.The.pegen.parserand.pegen.tokenizermodules are frompegenby Guido van Rossum,
Pablo Galindo, and Lysandros Nikolaou. They are included here solely
to prevent this package from pulling in pegen's Flask and other
dependencies.In addition, the Python parser in this package is built using pegen.