# 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.
```diff
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 of `typing.Final`.
- Adds global import if it's not imported yet.
- Inspects one file at a time.
## How To Use
Having uv installed:
```sh
uvx auto-typing-final .
```
or:
```sh
pipx run auto-typing-final .
```
### Options
You can specify `--check` flag to check the files instead of actually fixing them:
```sh
auto-typing-final . --check
```
Also, you can choose import style from two options: `typing-final` (default) and `final`:
```sh
auto-typing-final . --import-style typing-final
```
- `typing-final` enforces `import typing` and `typing.Final`,
- `final` enforces `from typing import Final` and `Final`.
## VS Code Extension
<img width="768" alt="image" src="https://github.com/vrslev/auto-typing-final/assets/75225148/f1541056-06f5-4caa-8c94-0a5eaf98ba15">
The extension uses LSP server bundled with the CLI. To get started, add `auto-typing-final` to your project:
```sh
uv add auto-typing-final --dev
```
or:
```sh
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).
Raw data
{
"_id": null,
"home_page": null,
"name": "auto-typing-final",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "automation, flake8, mypy, typing",
"author": null,
"author_email": "Lev Vereshchagin <mail@vrslev.com>",
"download_url": "https://files.pythonhosted.org/packages/ac/a3/b9df37b2506acad1a3ebb7fa5a141d4e697380f4abe8f18cbccaf306a5fa/auto_typing_final-0.6.0.tar.gz",
"platform": null,
"description": "# auto-typing-final\n\nAuto-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.\n\n```diff\n def foo() -> None:\n- a = 2\n+ a: typing.Final = 2\n\n- b: typing.Final = 2\n+ b = 2\n b = 3\n```\n\nBasically, this, but handles different operations (like usage of `nonlocal`, augmented assignments: `+=`, etc) as well.\n\n- Keeps mypy happy.\n- Ignores global variables to avoid confusion with the type aliases like `Fruit = Apple | Banana`.\n- Ignores class variables: it is common to use `typing.ClassVar` instead of `typing.Final`.\n- Adds global import if it's not imported yet.\n- Inspects one file at a time.\n\n## How To Use\n\nHaving uv installed:\n\n```sh\nuvx auto-typing-final .\n```\n\nor:\n\n```sh\npipx run auto-typing-final .\n```\n\n\n### Options\n\nYou can specify `--check` flag to check the files instead of actually fixing them:\n\n```sh\nauto-typing-final . --check\n```\n\nAlso, you can choose import style from two options: `typing-final` (default) and `final`:\n\n```sh\nauto-typing-final . --import-style typing-final\n```\n\n- `typing-final` enforces `import typing` and `typing.Final`,\n- `final` enforces `from typing import Final` and `Final`.\n\n\n## VS Code Extension\n\n<img width=\"768\" alt=\"image\" src=\"https://github.com/vrslev/auto-typing-final/assets/75225148/f1541056-06f5-4caa-8c94-0a5eaf98ba15\">\n\nThe extension uses LSP server bundled with the CLI. To get started, add `auto-typing-final` to your project:\n\n```sh\nuv add auto-typing-final --dev\n```\n\nor:\n\n```sh\npoetry add auto-typing-final --group=dev\n```\n\nAfter 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.\n\nImport style can be configured in settings: `\"auto-typing-final.import-style\": \"typing-final\"` or `\"auto-typing-final.import-style\": \"final\"`.\n\nLibrary code of currently activated environment will be ignored (for example, `.venv/bin/python` is active interpreter, all code inside `.venv` will be ignored).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": null,
"version": "0.6.0",
"project_urls": {
"repository": "https://github.com/vrslev/auto-typing-final"
},
"split_keywords": [
"automation",
" flake8",
" mypy",
" typing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c7ca74c05ec176899e9837dfe1a65b5d5eb854d984a49edc4ab11310d3a4f5c8",
"md5": "e22df9a114b7e1c115710892f070d634",
"sha256": "f09fef92fdfbfe93ddf6d8769de94e9f34724fc227b3d6764d8009d62b602f6b"
},
"downloads": -1,
"filename": "auto_typing_final-0.6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e22df9a114b7e1c115710892f070d634",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 11667,
"upload_time": "2024-07-20T08:01:16",
"upload_time_iso_8601": "2024-07-20T08:01:16.070001Z",
"url": "https://files.pythonhosted.org/packages/c7/ca/74c05ec176899e9837dfe1a65b5d5eb854d984a49edc4ab11310d3a4f5c8/auto_typing_final-0.6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "aca3b9df37b2506acad1a3ebb7fa5a141d4e697380f4abe8f18cbccaf306a5fa",
"md5": "ada3dc584d2a2bacf527f21f12c686fa",
"sha256": "07a2d78f38e9559668d27719840d60228c4fd4506cfc2fefd9dd95e2d97f5234"
},
"downloads": -1,
"filename": "auto_typing_final-0.6.0.tar.gz",
"has_sig": false,
"md5_digest": "ada3dc584d2a2bacf527f21f12c686fa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 34183,
"upload_time": "2024-07-20T08:01:17",
"upload_time_iso_8601": "2024-07-20T08:01:17.376865Z",
"url": "https://files.pythonhosted.org/packages/ac/a3/b9df37b2506acad1a3ebb7fa5a141d4e697380f4abe8f18cbccaf306a5fa/auto_typing_final-0.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-20 08:01:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "vrslev",
"github_project": "auto-typing-final",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "auto-typing-final"
}