auto-typing-final


Nameauto-typing-final JSON
Version 0.7.2 PyPI version JSON
download
home_pageNone
SummaryAutomagically set typing.Final inside your functions
upload_time2024-11-28 10:03:26
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT
keywords automation flake8 mypy typing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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/f0/a4/61cca1f4b6aae470e6c89c62d0e05a49ace8457052f59e26b8cb05a711ca/auto_typing_final-0.7.2.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": "Automagically set typing.Final inside your functions",
    "version": "0.7.2",
    "project_urls": {
        "Repository": "https://github.com/vrslev/auto-typing-final"
    },
    "split_keywords": [
        "automation",
        " flake8",
        " mypy",
        " typing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8721f860a90fdb1f7cc68f966cfbb46378609bebe4684921f98995d100a71740",
                "md5": "5c3c888aa6e9e853a2d474474281c45b",
                "sha256": "c3e69684120b1c3cf272f106ccf9869f0bd4eb6572481374440a66016c3ba57b"
            },
            "downloads": -1,
            "filename": "auto_typing_final-0.7.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5c3c888aa6e9e853a2d474474281c45b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 11678,
            "upload_time": "2024-11-28T10:03:25",
            "upload_time_iso_8601": "2024-11-28T10:03:25.584310Z",
            "url": "https://files.pythonhosted.org/packages/87/21/f860a90fdb1f7cc68f966cfbb46378609bebe4684921f98995d100a71740/auto_typing_final-0.7.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f0a461cca1f4b6aae470e6c89c62d0e05a49ace8457052f59e26b8cb05a711ca",
                "md5": "a94b5d653546f0c02935960128146587",
                "sha256": "a8c43b12044c3eec6a36fe42f63db782dad5f53d6252fe75208f6352d3988bb6"
            },
            "downloads": -1,
            "filename": "auto_typing_final-0.7.2.tar.gz",
            "has_sig": false,
            "md5_digest": "a94b5d653546f0c02935960128146587",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 33688,
            "upload_time": "2024-11-28T10:03:26",
            "upload_time_iso_8601": "2024-11-28T10:03:26.452758Z",
            "url": "https://files.pythonhosted.org/packages/f0/a4/61cca1f4b6aae470e6c89c62d0e05a49ace8457052f59e26b8cb05a711ca/auto_typing_final-0.7.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-28 10:03:26",
    "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"
}
        
Elapsed time: 0.37323s