Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sachaa-thanasius/experimental-late-bound-defaults
A way to mess with PEP 671 syntax in python.
https://github.com/sachaa-thanasius/experimental-late-bound-defaults
Last synced: 9 days ago
JSON representation
A way to mess with PEP 671 syntax in python.
- Host: GitHub
- URL: https://github.com/sachaa-thanasius/experimental-late-bound-defaults
- Owner: Sachaa-Thanasius
- License: mit
- Created: 2024-03-30T00:33:15.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-03-30T14:53:47.000Z (9 months ago)
- Last Synced: 2024-03-31T03:29:49.502Z (9 months ago)
- Language: Python
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# experimental-late-bound-defaults
A way to mess with [PEP 671](https://peps.python.org/pep-0671/) syntax in python without needing a CPython fork.## Installation
```shell
python -m pip install https://github.com/Sachaa-Thanasius/experimental-late-bound-defaults
```# Usage
All you need is the encoding declaration at the top of your file and you're good to go for using the late binding syntax.```python
# coding: experimental-late-bound-defaultsdef example_func(
a: int,
b: list[int] => ([a] * a)
c: int => (len(b) + a),
) -> tuple[list[object], int]:
# If late-bound arguments like b and c aren't passed in, they're computed based
# on the expressions after the '=>', in order, before the function body executes.
assert b == ([a] * a)
assert c == (len(b) + a)
return cassert example_func(5) == 10
```## Motivation
I was annoyed with Python defaults, and resulting searches and discussion let to me being made aware of the above proposal (and [PEP 661](https://peps.python.org/pep-0671/), but I'm ignoring that for now).
I didn't want to fork CPython and change all the grammar to try this out, so I cheated with a custom codecs and tokens/AST transformation.## Caveats
The late-bound expression must be surrounded by parentheses, unlike the syntax proposed in the PEP. This may be fixed in the future.## Acknowledgements
Thank you to [@asottile](https://github.com/asottile) and his future packages (e.g. [future-fstrings](https://github.com/asottile-archive/future-fstrings)) for providing an outline for how to modify code like this.