No project description provided
Project description
auto-typing-final
Auto-fixer for Python code that adds typing.Final
annotation to variable assignments inside functions that are not reassigned, and removes the annotation from variables that are mutated.
def foo() -> None:
- a = 2
+ a: typing.Final = 2
- b: typing.Final = 2
+ b = 2
b = 3
Basically, this, but handles different operations (like usage of nonlocal
, augmented assignments: +=
, etc) as well.
- Keeps mypy happy.
- Ignores global variables to avoid confusion with the type aliases like
Fruit = Apple | Banana
. - Ignores class variables: it is common to use
typing.ClassVar
instead oftyping.Final
. - Adds global import if it's not imported yet.
- Inspects one file at a time.
How To Use
Having uv installed:
uvx auto-typing-final .
or:
pipx run auto-typing-final .
Options
You can specify --check
flag to check the files instead of actually fixing them:
auto-typing-final . --check
Also, you can choose import style from two options: typing-final
(default) and final
:
auto-typing-final . --import-style typing-final
typing-final
enforcesimport typing
andtyping.Final
,final
enforcesfrom typing import Final
andFinal
.
VS Code Extension
The extension uses LSP server bundled with the CLI. To get started, add auto-typing-final
to your project:
uv add auto-typing-final --dev
or:
poetry add auto-typing-final --group=dev
After that, install the extension: https://marketplace.visualstudio.com/items?itemName=vrslev.auto-typing-final. In Python environments that have auto-typing-final
installed, extension will be activated automatically.
Import style can be configured in settings: "auto-typing-final.import-style": "typing-final"
or "auto-typing-final.import-style": "final"
.
Library code of currently activated environment will be ignored (for example, .venv/bin/python
is active interpreter, all code inside .venv
will be ignored).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for auto_typing_final-0.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f09fef92fdfbfe93ddf6d8769de94e9f34724fc227b3d6764d8009d62b602f6b |
|
MD5 | e22df9a114b7e1c115710892f070d634 |
|
BLAKE2b-256 | c7ca74c05ec176899e9837dfe1a65b5d5eb854d984a49edc4ab11310d3a4f5c8 |