Skip to main content

Python PEP505

This package polyfills PEP 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.

Usage

After 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 Information

Null-Coalescing Operators

This 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 an await expression.

The coalesce operator works as follows: A ?? B is the equivalent of:

(X if (X := A) is not None else B)

A is evaluated only once; B is not evaluated if A is not None.

Maybe-Assign (??=)

The maybe-assign operator ??= is a special form of augmented assignment, which will assign to the left-hand side only if it is None.

The maybe-assign operator works as follows: A ??= B is the equivalent of:

if A is None:
    A = B

B is not evaluated if A is not None.

Maybe-Dot (?.)

The maybe-dot operator ?. prevents AttributeError from occuring if the object for which an attribute is being accessed is None; instead, the expression evaluates to None.

The maybe-dot operator works as follows: A?.B is the equivalent of:

(X.B if (X := A) is not None else None)

A is evaluated only once.

Maybe-Subscript (?[)

The maybe-subscript operator ?[ prevents TypeError from occuring if the object for which an item is being accessed is None; instead, the expression evaluates to None.

The maybe-subscript operator works as follows: A?[B] is the equivalent of:

(X[B] if (X := A) is not None else None)

A is evaluated only once; B is not evaluated if A is not None.

How the PEP505 Package Works

This 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 in src/grammar/python_pep505.gram, and compiled with the normal:

python3 -m pegen src/grammar/python_pep505.gram -o src/pep505/parser.py

Note that parser.py is then further modified slightly to use the local version of the pegen package (replacing pegen. imports with .pegen.).

Temporary Variables

In 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 Source

This package depends on the parsinghook package. If that package is also installed from source, it is necessary to install the parsinghook.pth file from that package in site-packages.

Running the Tests

The tests can be run with python3 -m pep505.test.

Thanks and Credits

This package stands on the shoulders of giants.

PEP 505 was authored by Mark E. Haase and Steve Dower.

The .pegen.parser and .pegen.tokenizer modules are from pegen by 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.

Metadata

Release files for pep505 0.9.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pep505 0.9.3
File Size Uploaded
pep505-0.9.3.tar.gz 37.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pep505 0.9.3
File Interpreter ABI Platform
pep505-0.9.3-py3-none-any.whl Python 3 none any Details

Total release size: 75.0 kB

Release files / pep505-0.9.3.tar.gz

Download URL pep505-0.9.3.tar.gz
Size 37.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9a413fcd5de67084db6a601abd61dba824496b4962a61e3753f16ce814bea0b3
BLAKE2b-256 checksum
How to use checksums
30a17c1cd0e5c0decbfc7fe60af377c6f83779eb048da848420baafcc4214020
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.6.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0

Release files / pep505-0.9.3-py3-none-any.whl

Download URL pep505-0.9.3-py3-none-any.whl
Size 37.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38360022ad1bee94d8fcfe6ef282287248182087aa4971dc8f66dad3f4015209
BLAKE2b-256 checksum
How to use checksums
e3118918fe465e310bb6b61a1820eed04b890a7c4199106c6d457f027b01559b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.6.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.9.3 This release

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page