hn.today

Show HN: Gawain – Require type hints on all Python variables

github.com67 points58 comments
Screenshot of Show HN: Gawain – Require type hints on all Python variables

Gawain is a command-line tool for enforcing Python type annotations more broadly than function-focused linters: it checks variables, parameters, return values, and class attributes. Its author’s motivation is to reduce reliance on vague types such as `Any` and `dict[str, Any]`. By default, assignments with an obvious type - such as a literal, a container of literals, or a constructor call - can remain unannotated if the name is not reassigned; strict mode requires an annotation on every assignment. Other rules flag unannotated constants that should use `Final`, bare `Final`, uses of `Any`, and instance attributes not declared in the class body. After annotation checks pass, Gawain runs a type checker to catch mismatches.

The tool uses Astral’s ty by default or Meta’s pyrefly, and can also run annotation checks alone without a type checker. It offers configurable modes, JSON output, named suppressions, and a pre-commit hook; installation options and checker versions are documented. A benchmark on the CPython 3.14 standard library, covering 1,847 files and nearly 975,000 lines on one machine, reported median times of 0.46 seconds in default mode and 0.36 seconds in strict mode, without type checking. The project cautions that ty is in beta and its diagnostics may change, recommending pyrefly or annotation-only mode when that matters. Building the pre-commit hook requires Rust 1.97 or newer.

Read on github.com58 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Aristotle Expands into Rust

Aristotle Expands into Rust

Aristotle has expanded its formal verification platform to Rust, allowing developers to prove code meets specifications and contains no undefined behavior, including in unsafe blocks. It can also verify that Rust rewrites behave like their original C code, targeting critical software such as smart contracts and infrastructure. (harmonic.fun)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.