Skip to content

feat: forbid redundant trailing colon in slice (fixes #1071)#3642

Open
f1sherFM wants to merge 2 commits into
wemake-services:masterfrom
f1sherFM:fix-issue-1071
Open

feat: forbid redundant trailing colon in slice (fixes #1071)#3642
f1sherFM wants to merge 2 commits into
wemake-services:masterfrom
f1sherFM:fix-issue-1071

Conversation

@f1sherFM
Copy link
Copy Markdown
Contributor

I have made things!

Added WPS367 (RedundantTrailingSliceViolation) to detect redundant trailing colons inside subscript slices that the AST parser cannot distinguish. Introduced RedundantTrailingSliceVisitor in visitors/tokenize/subscripts.py to catch cases like a[1:4:], a[1::], and a[:4:] at the token level. Also extended the existing RedundantSubscriptViolation test suite with previously uncovered step=1 scenarios (::1, 1::1, 0::1).

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Closes #1071

🙏 Please, if you or your company is finding wemake-python-styleguide valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/wemake-python-styleguide. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.

Comment thread wemake_python_styleguide/violations/consistency.py Outdated
def __init__(self, *args, **kwargs) -> None:
"""Initialize state for bracket tracking."""
super().__init__(*args, **kwargs)
self._bracket_stack: list[list] = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please create a structure for this. Even a tuple with a type alias will work. list[list] is not correct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if you need a mutable structure you can use TypedDict

@f1sherFM
Copy link
Copy Markdown
Contributor Author

@sobolevn
Fixed both points:

  1. Changed .. versionadded:: 1.6.31.7.0 — you're right, this is a new feature.

  2. Replaced list[list] with a proper TypedDict (_SliceBracketState) for bracket tracking. Since we need mutability during token traversal, I used TypedDict with module-level string constants for field names to avoid WPS226. Pushed as amended commit.

Comment thread wemake_python_styleguide/visitors/tokenize/subscripts.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forbid explicit step in slice

2 participants