Skip to content

[pycodestyle] Fix whitespace-related false positives and false negatives inside type-parameter lists#13704

Merged
AlexWaygood merged 2 commits intomainfrom
e251-tvar-default
Oct 10, 2024
Merged

[pycodestyle] Fix whitespace-related false positives and false negatives inside type-parameter lists#13704
AlexWaygood merged 2 commits intomainfrom
e251-tvar-default

Conversation

@AlexWaygood
Copy link
Member

Summary

Fixes #13699.

We had false positives and false negatives regarding whitespace in type-parameter lists, that led to conflicts between the linter and the formatter. Specifically, we emitted a false positive here:

def foo[T = int](): ...  # E251: Unexpected spaces around keyword / parameter equals

and a false negative here:

def foo[T:int](): ...  # should be an E231 error here (no space in between `T:` and `int`), but there isn't

This PR adds a TypeParamsState enum, to keep track of whether we're currently visiting the LogicalLineTokens in a type parameters list or not. The common solution is then used to fix the bugs in both rules.

Type parameter lists are new syntax that is only available in Python 3.13+, which is why this is only coming up now.

Test Plan

Fixtures have been added with examples of both class and function definitions that have type parameters

@AlexWaygood AlexWaygood added bug Something isn't working rule Implementing or modifying a lint rule python313 Related to Python 3.13 labels Oct 10, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Oct 10, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this! I leave the decision on whether to implement the optimization or not to you if it's feasible before the 0.7 release otherwise that can be deferred for later. I don't see any regression so it might be ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working python313 Related to Python 3.13 rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unexpected-spaces-around-keyword-parameter-equals (E251) - conflicts with formatter / false positive on python 3.13 type var defaults

3 participants