Respect the magic trailing comma in starred type parameter lists#5244
Open
dchaudhari7177 wants to merge 2 commits into
Open
Respect the magic trailing comma in starred type parameter lists#5244dchaudhari7177 wants to merge 2 commits into
dchaudhari7177 wants to merge 2 commits into
Conversation
A magic trailing comma exploded a PEP 695 type parameter list of plain type vars, but was silently ignored once the list contained a *TypeVarTuple or **ParamSpec, collapsing the params back onto one line. is_split_before_delimiter/is_split_after_delimiter skip a star that is_vararg() recognises, so it is not mistaken for a MATH_OPERATORS delimiter. is_vararg() checks the star's parent against VARARGS_PARENTS, which listed the argument and lambda nodes but not the PEP 695 typevartuple and paramspec ones. The stars in a type parameter list were therefore scored as multiplication and power operators, and that delimiter beat the comma when the line was split. Add both nodes to VARARGS_PARENTS. This affects def, async def, class and type aliases alike. Closes psf#5243
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.
Summary
Fixes #5243. A magic trailing comma explodes a PEP 695 type-parameter list of plain type vars, but was silently ignored once the list contained a
*TypeVarTupleor**ParamSpec:collapsed back to
Root cause
is_split_before_delimiter/is_split_after_delimiterdeliberately skip a star thatis_vararg()recognises, with the comment:is_vararg()decides that by checking the star's parent againstVARARGS_PARENTS, which listed the argument/lambda nodes but not the PEP 695typevartupleandparamspecnodes:So the stars in a type-parameter list were scored as
*and**math operators, and that delimiter outranked the comma when the line was split — which is why the trailing comma stopped mattering.Adding both nodes to
VARARGS_PARENTSrestores the intended "not a delimiter" treatment.Scope
Fixes
def,async def,classandtypealiases alike, matching the issue. The non-magic case is unchanged —def all_in[T: int, U: (bytes, str), *Ts, **P]()still stays on one line (pinned by the existing fixture case).Tests
Added
magic_starred(def),MagicStarred(class) andMagicStarredAlias(type alias) totests/data/cases/type_params.py, right beside the existing plain-type-varmagic[Trailing, Comma,]case.Reverting only
src/fails the fixture with exactly the reported collapse, on all three constructs:Verification
pytest tests/ -k "not symlink"→ 464 passed, 3 skipped, 8 subtests passed — no formatting regressions across the fixture corpussymlinktests fail on a clean tree on my machine (OSError: WinError 1314 A required privilege is not held by the client— Windows needs elevation to create symlinks), verified by stashing; unrelated to this changeI'll add the
CHANGES.mdentry under Stable style in a follow-up commit on this branch, since the file asks for the PR number rather than the issue number and I only get that once this is opened.