Python constants: Blocking reassignment with Final and mypy

Python constants: Blocking reassignment with Final and mypy

Can you get Python to enforce constants? Yes, with type checking:

A = 10
A = 20

Ruff won’t complain. Instead, type it as Final and run mypy:

from typing import Final
A : Final = 10
A = 20

mypy myprog.py
myprog.py:6: error: Cannot assign to final name “NAME” [misc]