Skip to main content

Enforce using keyword arguments where possible.

Project description

Build Status PyPI

strict-kwargs

Enforce using keyword arguments where possible.

strict-kwargs is a standalone CLI implemented in Rust. It parses Python with Ruff's Python parser and AST crates, then uses its own resolver plus ty for type-aware call resolution where static names alone are not enough.

For example, if we have a function which takes two regular arguments, there are three ways to call it. With this tool, only the form where keyword arguments are used is accepted.

"""Showcase errors when calling a function without naming the arguments."""


def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b


add(a=1, b=2)  # OK
add(1, 2)  # strict-kwargs reports this
add(1, b=2)  # strict-kwargs reports this

Why?

  • In the same spirit as a formatter - think black or ruff format - this lets you stop spending time discussing whether a particular function call should use keyword arguments.
  • Sometimes positional arguments are best at first, and then more and more are added and code becomes unclear, without anyone stopping to refactor to keyword arguments.
  • Type checkers give better errors when keyword arguments are used. For example, with positional arguments, you may see, Argument 5 to "add" has incompatible type "str"; expected "int". This requires that you count the arguments to see which one is wrong. With named arguments, you get Argument "e" to "add" has incompatible type "str"; expected "int".

How it works

strict-kwargs has two resolution layers:

  • A built-in resolver parses checked files, first-party modules, vendored typeshed stubs, and discovered site-packages using Ruff's Python parser and AST crates.
  • For calls that need richer inference, strict-kwargs asks ty's language server for hover and definition information. ty is a required dependency of the Python package so results do not depend on whether it happens to be installed separately.

The fixer uses the same detection path, but only rewrites calls when the target parameter names are known unambiguously.

Installation

uv tool install strict-kwargs

or:

pip install strict-kwargs

This is tested on Python 3.11+.

Usage

strict-kwargs .                 # check a directory
strict-kwargs fix .             # rewrite positional args to keyword args in place
strict-kwargs fix --diff .      # preview the rewrite, write nothing
strict-kwargs fix --fix-synthesized-constructors .  # opt into one declined category
strict-kwargs --python .venv .  # point type resolution at an environment

Exit codes are:

  • 0: clean
  • 1: violations found
  • 2: operational error

fix only rewrites calls it can name unambiguously. Ambiguous calls are counted as declined. Single-signature calls are rewritten by default, including calls that require deeper type inference. Overloaded calls are rewritten by default only when analysis selects one precise overload arm and the rewritten argument types are precise enough. Synthesized constructors are the only opt-in category, because generated constructor models can differ from runtime behaviour when class construction is customized.

  • --fix-synthesized-constructors: rewrite dataclass and NamedTuple constructors whose signatures were synthesized from fields. These can differ from runtime behaviour when class construction is customized.

Use --python to point third-party resolution at an interpreter, virtual environment, or sys.prefix. Missing paths are errors. A missing --python path is warned about and ignored.

pre-commit

repos:
  - repo: https://github.com/adamtheturtle/strict-kwargs-pre-commit
    rev: 2026.5.19.post3  # pin to a release tag
    hooks:
      - id: strict-kwargs

Configuration

Configuration lives in pyproject.toml:

[tool.strict_kwargs]
ignore_names = ["main.func", "builtins.str"]
fix_synthesized_constructors = true

This is useful especially for builtins which can look strange with keyword arguments. For example, str(object=1) is not idiomatic. Set fix_synthesized_constructors = true to make strict-kwargs fix rewrite dataclass and NamedTuple constructors without passing --fix-synthesized-constructors each time.

To find the name of a function to ignore, set the following configuration:

[tool.strict_kwargs]
debug = true

Then run strict-kwargs and look for the debug output.

Comparison with mypy-strict-kwargs

mypy-strict-kwargs is a mypy plugin that enforces the same rule during type checking.

Use strict-kwargs if you type-check with ty, if you prefer a standalone linter without plugins, or if you want automatic rewrites with strict-kwargs fix.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

strict_kwargs-2026.5.19.post3-py3-none-win_amd64.whl (2.1 MB view details)

Uploaded Python 3Windows x86-64

strict_kwargs-2026.5.19.post3-py3-none-manylinux_2_39_x86_64.whl (2.3 MB view details)

Uploaded Python 3manylinux: glibc 2.39+ x86-64

strict_kwargs-2026.5.19.post3-py3-none-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file strict_kwargs-2026.5.19.post3-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for strict_kwargs-2026.5.19.post3-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 dbbfb69d3be9070777cb1c5350f1a120df756e19ffe9cd2080c94ec715471459
MD5 d1ebfa346a6ef8b5e61964841c0d4471
BLAKE2b-256 ba3f73c489d29d7fbffc09a371e258d1bb68afc15f43861b257ce4800888692b

See more details on using hashes here.

Provenance

The following attestation bundles were made for strict_kwargs-2026.5.19.post3-py3-none-win_amd64.whl:

Publisher: release.yml on adamtheturtle/strict-kwargs

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

File details

Details for the file strict_kwargs-2026.5.19.post3-py3-none-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for strict_kwargs-2026.5.19.post3-py3-none-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 133aa54e96ca3884465fef378919fcfb4bc00e94777251d6d3a5490ebef3d11a
MD5 1fa0353603ed082f990c80f0919277b8
BLAKE2b-256 87b409bdae71f1b1ee7a54245b67eb34cead80696e12ca6e4d83ca5d3f202ac5

See more details on using hashes here.

Provenance

The following attestation bundles were made for strict_kwargs-2026.5.19.post3-py3-none-manylinux_2_39_x86_64.whl:

Publisher: release.yml on adamtheturtle/strict-kwargs

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

File details

Details for the file strict_kwargs-2026.5.19.post3-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for strict_kwargs-2026.5.19.post3-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d778b7b77e7ba5a068d5a4f3bef0600be37ebebc26ae25c6355140e98103b98e
MD5 feb57c1ad0b58bd65f16fac3b2b1ea5f
BLAKE2b-256 23b4e51d972e18c777793fd6af12f8e2785521252f81967f5365a2e72e2dfc61

See more details on using hashes here.

Provenance

The following attestation bundles were made for strict_kwargs-2026.5.19.post3-py3-none-macosx_11_0_arm64.whl:

Publisher: release.yml on adamtheturtle/strict-kwargs

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