Skip to main content

Static analysis tool that enforces type annotation presence in Python code

Project description

typestrict

typestrict is a Python static analysis tool that enforces the presence of type annotations in Python code.

Unlike mypy or pyright which verify annotation correctness, typestrict simply checks that every variable, function argument, and return value has an annotation. If it is missing — that is an error.

# ERROR: missing type annotation
a = 3
result = get_user()

# OK: annotation present
a: int = 3
result: User = get_user()

Installation

pip install typestrict

Requires Python 3.10+.


Usage

# Check a single file
typestrict check app/models.py

# Check a directory recursively
typestrict check src/

# Output as JSON (for CI/CD)
typestrict check src/ --format json

# Check only specific rules
typestrict check src/ --select TF001,TF002

# Exit with code 1 if errors are found (pre-commit / CI)
typestrict check src/ --fail-on-error

Example output

src/app/models.py:15:4  TF001  Variable 'result' missing type annotation
src/app/models.py:23:0  TF002  Argument 'user_id' missing type annotation
src/app/views.py:8:0    TF003  Function 'get_data' missing return type annotation

Found 3 errors in 2 files

Error codes

Code Description Example violation
TF001 Variable assignment without a type annotation a = 3
TF002 Function argument without a type annotation def f(x):
TF003 Function missing a return type annotation def f():
TF004 Class attribute assigned without a type annotation self.name = "test"
TF005 Loop/context-manager variable without annotation (opt-in) for i in range(10):

Configuration (pyproject.toml)

[tool.typestrict]
# Exclude directories or files from analysis
exclude = ["tests/", "migrations/", "conftest.py"]

# Disable specific rules globally
ignore = ["TF005"]

# Disable rules for specific files
per_file_ignores = { "conftest.py" = ["TF001"], "settings.py" = ["TF001"] }

# Strict mode: enables ALL rules, including TF005
strict = false

Inline suppression

Suppress an error on a specific line:

data = json.loads(response.text)  # typestrict: ignore

Suppress a specific rule code:

data = json.loads(response.text)  # typestrict: ignore[TF001]

Pre-commit integration

Add to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/example/typestrict
    rev: v0.1.0
    hooks:
      - id: typestrict

What is skipped automatically

  • Dunder variables: __all__, __version__, etc.
  • The unused-value convention: _ = some_func()
  • Tuple unpacking: a, b = func() (hard to annotate inline)
  • Augmented assignment: counter += 1 (type already declared)
  • self and cls as the first argument of methods

Development

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Type-check the library itself
mypy

License

MIT — see LICENSE.

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

must_annotate-0.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

must_annotate-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file must_annotate-0.1.0.tar.gz.

File metadata

  • Download URL: must_annotate-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for must_annotate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0846ea4edc9de21053099ed1294c0ee9354680c7ccbed1a5b637dc0f21e42718
MD5 326d4587d02b851dd21c4d76f55dda54
BLAKE2b-256 1f1d5dbfee3aa996df1d45fecf9245d03d653cb11a11afee080cbc8e536569bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for must_annotate-0.1.0.tar.gz:

Publisher: publish.yml on PitRella/typestrict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file must_annotate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: must_annotate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for must_annotate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 252bc2ea0e89d3c67f51bfa48277e2065cee2abdf744ce295230ef02def59d53
MD5 605a9f627d0549fcfe424d943f69e13e
BLAKE2b-256 ceaeb569d7920bd9381178fe816a6d1a672f446e5578acafe1ac627961b17080

See more details on using hashes here.

Provenance

The following attestation bundles were made for must_annotate-0.1.0-py3-none-any.whl:

Publisher: publish.yml on PitRella/typestrict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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