Repository navigation
feat: parse type parameter defaults - #22
Merged
Merged
Conversation
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Regenerate the parser with type parameter defaults and the f-string colon precedence fix. Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
Co-authored-by: WillBooster (Codex CLI) <agent@willbooster.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Python 3.13 type parameter defaults currently produce parse errors in class, function, and type-alias declarations, as reported in
tree-sitter/tree-sitter-python#346. The published parser reproduces all three failures.Requirements
required: Improve the fork by addressing eligible upstream items in dedicated PRs, avoiding duplicate PRs.
required: Parse PEP 696 defaults in class, function, and type-alias declarations, including TypeVar, TypeVarTuple, and ParamSpec forms.
chosen: Keep defaults restricted to declaration lists, since generic type arguments do not permit assignment syntax.
chosen: Represent each defaulted declaration with a default_type_parameter node exposing name, optional bound, and value fields; keep existing non-default declarations intact.
chosen: Reuse the parser's existing expression language for defaults, allowing an unpacked default only for TypeVarTuple; leave compiler-level ordering and type-validity checks to Python tooling. Parenthesized named expressions are accepted by Python 3.14 ast.parse but rejected by compile in symbol-table validation; recursive filtering would diverge from existing bound behavior.
chosen: Preserve the existing multi-version expression handling of legacy async/await identifiers in bounds and values. Published 2.1.1 accepts both in nondefault bounds and annotations, although current CPython ast.parse rejects them there too. Reject a special bare-identifier filter for defaults: it would inconsistently leave nested occurrences accepted and duplicate the expression grammar; tightening shared expression syntax is a separate language-version policy change. Declaration names introduced here remain restricted to current contextual identifiers.
chosen: Maintain one structural type subset for names, qualified names, generics, unions, grouping, lists and tuples, recursively exposing type nodes for actual rendered highlighting; retain the complete existing expression grammar as a fallback for calls, conditions and other expressions. The subset must accept only expression syntax and preserve operator precedence; do not import declaration constraints or promise type coloring for arbitrary value computations. Dynamic selection preserves the fallback, whereas enclosing query captures are overridden by nested variable captures. These are the shared invariants for the highlight repair chain.
chosen: Scoped generic arguments use expression alternatives and the ordinary slice production, never declaration constraints; this preserves slice limits and excludes declarations nested in defaults while retaining recursive type highlighting.
chosen: Share inline grammar rules for defaulted declaration names and member/generic bases so each accepted identifier set has one definition; inlining avoids additional parser reductions or public tree nodes.
chosen: Parse unpacked generic operands through the same structural expression subset and share current contextual declaration names in generic/member bases; this keeps nested type coloring without adding legacy await/async ambiguity to the structural subset. Legacy names continue through the existing expression fallback.
chosen: Render highlights with a worktree-local compiled-parser cache and bounded native CLI startup (170-second process timeout within a 180-second test), because cold compilation on CI can exceed the default five-second assertion timeout.
Customer Summary
Declarations such as
class Container[T = str]: pass,def factory[T = str](): pass, andtype Alias[T = str] = list[T]parse successfully. Bounds, constraints, parameter-specification defaults, and variadic type defaults are supported.Technical Summary
Inline name and member-base rules share contextual identifier definitions across declaration forms. Unpacked generic operands retain recursive type nodes for highlighting, and every segment of qualified type names receives the type capture, including nested default member chains.
Add a declaration-only default_type_parameter production and regenerate the profiled parser. The explicit conflict with primary_expression preserves bounded parameters while the parser determines whether a default follows. Ordinary generic arguments retain their separate production. Remove the known-failure entry for CPython’s type-parameter tests, which now parse successfully. The supported declaration forms follow PEP 696; expressions retain the parser's existing language-version policy. A recursive structural type subset preserves rendered highlighting for qualified names, generics, unions, grouping and containers, while the expression fallback preserves other computations.
Testing
bun run verifypass. Existing code-gauge violations are outside changed code.