Skip to main content

Automagically set typing.Final inside your functions

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 type checker happy.
  • Adds global import if it's not imported yet (import typing/from typing import Final).
  • Inspects one file at a time.
  • Is careful with global variables: adds Final only for uppercase variables, ignores variable that are referenced in global statement inside functions in current file, and avoids removing Final when it already was set (docs).
  • Ignores class variables: it is common to use typing.ClassVar instead of typing.Final.

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 enforces import typing and typing.Final,
  • final enforces from typing import Final and Final.

Also, you can set --ignore-global-vars flag to ignore global variables:

auto-typing-final . --ignore-global-vars

Ignore comment

You can ignore variables by adding # auto-typing-final: ignore comment to the line (docs).

VS Code Extension

image

The extension uses LSP server bundled with the CLI (executable name is auto-typing-final-lsp-server). To get started, add auto-typing-final to your project:

uv add auto-typing-final --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.

Settings

  • Import style can be configured in settings: "auto-typing-final.import-style": "typing-final" or "auto-typing-final.import-style": "final".
  • Ignore global variables can be configured in settings: "auto-typing-final.ignore-global-vars": true.

Notes

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_typing_final-1.0.4.tar.gz (36.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_typing_final-1.0.4-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file auto_typing_final-1.0.4.tar.gz.

File metadata

  • Download URL: auto_typing_final-1.0.4.tar.gz
  • Upload date:
  • Size: 36.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for auto_typing_final-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d8e073e3902e1d057b6d301884ceb0a14dabede4c0f6403aa8fcec510a28f50d
MD5 ba3c7d9b3fea2c321497c81dc9209158
BLAKE2b-256 98d9c16da0ba3ab22011f2d34617664a3af3adff59c08e95406a2da01d82bb75

See more details on using hashes here.

File details

Details for the file auto_typing_final-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: auto_typing_final-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for auto_typing_final-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3857733f9c76ded014d63f21a0286110c484a87ec53acf6eeb2fc583175e100a
MD5 360ef31cef4c75da289b5ee8729c2d13
BLAKE2b-256 f29324ff59a438e5c4378f30ff2dcf75115cb5eb61140d520139c037313cb68a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page