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 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:

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.

VS Code Extension

image

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


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-0.7.1.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

auto_typing_final-0.7.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: auto_typing_final-0.7.1.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.19

File hashes

Hashes for auto_typing_final-0.7.1.tar.gz
Algorithm Hash digest
SHA256 56a090b211470ec9bbb6bd787eb22e1b374413d5dfade97fbb3261f4cca215c5
MD5 f9083e1e67a619a48d70173fc5e318cd
BLAKE2b-256 e285ed511df42eb7c2af14d713fc0afd0ad6517adbe7a34627808f8ea8893c5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for auto_typing_final-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ba78ba171f1c19c2a201ec08d7bd7f3f6b4b8fef878a59c9ea3a2d7c2e7b81c
MD5 e7c9dc3014b035df1b50ed95989003ed
BLAKE2b-256 0cbab2d4f66161bd72a2d5a0b12273f16a9a6fa216015e3fa953ea57792ce3ab

See more details on using hashes here.

Supported by

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