[ty] Add unbound type variable detection in annotations by AlexWaygood · Pull Request #23641 · astral-sh/ruff (original) (raw)
added the ty
Multi-file analysis & type inference
label
… their bound scope
This adds validation for three missing checks around correct use of type variables:
- Reject unbound type variables in module-scope annotations (e.g.,
x: T) - Reject unbound type variables in function/class bodies where they aren't
part of the enclosing generic context (e.g.,
Sinsidedef f(x: T)) - Reject type variables from outer class scopes used in inner class bodies
(e.g.,
class C[T]: class Bad: x: T)
The implementation:
- Adds a new
UNBOUND_TYPE_VARIABLElint rule in diagnostic.rs - Modifies
bind_typevarin generics.rs to track class scope boundaries, preventing inner classes from accessing outer class type parameters - Adds
check_for_unbound_type_variablemethod in type_expression.rs that detects when a TypeVar remains unbound afterin_type_expressionprocessing - Uses a
check_unbound_typevarsflag on the builder, enabled only during annotation expression processing, to avoid false positives in TypeVar defaults, explicit class specialization, and other internal contexts - Updates existing TODO test comments in scoping.md to proper error assertions
https://claude.ai/code/session_01FUqdwkfw72xwPnuhqqpvp6
Co-authored-by: Douglas Creager dcreager@dcreager.net
carljm removed their request for review
AlexWaygood deleted the claude/add-type-variable-validation-pw9Ns branch
carljm added a commit that referenced this pull request
- main: (30 commits)
[ty] Introduce
types::bool,types::context_managerandtypes::iteration(#23681) [ty] MoveKnownInstanceType, and related types, to a newknown_instance.rssubmodule (#23680) [ty] Addinvalid-enum-member-annotationlint rule (#23648) [ruff] Fix false positive forre.splitwith empty string pattern (RUF055) (#23634) [ty] MoveUnionTypeandIntersectionTypeto a newtypes::set_theoreticsubmodule (#23678) [ty] Add unbound type variable detection in annotations (#23641) [ty] Removespecialize_constrainedfrom constraint set module (#23677) [ty] Add partial support and validation forUnpackwhen used with tuple types (#23651) Update prek dependencies (#23661) [ty] makeStaticClassLiteral::explicit_basesconverge better in cycles (#23601) Improvements to CLAUDE.md (#23633) [ty] Move subscript logic out ofbuilder.rs(#23653) Update Artifact GitHub Actions dependencies (#23676) Update actions/attest-build-provenance to 4.1.0 (#23654) Update Rust crate clearscreen to v4.0.5 (#23664) fix renovateactions/*-artifactupdates (#23675) Update Rust crate clap to v4.5.60 (#23663) Update Rust crate unicode-ident to v1.0.24 (#23668) Update Rust crate anyhow to v1.0.102 (#23662) Update Rust crate pyproject-toml to v0.13.7 (#23666) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})