Skip to content

Fixes #3427 #3433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fixes #3427 #3433

wants to merge 1 commit into from

Conversation

MIXgleb
Copy link
Contributor

@MIXgleb MIXgleb commented May 26, 2025

I have made things!

  • Adds WPS481: forbids using complex slice indexes, Forbid complex grammar in slice #3427 . Only the Name, Constant, Attribute, Unary and Bin operators with simple expressions inside parentheses are allowed.

Correct:

array[3:7]
array[:-(x + 3)]
array[::-index.step]
array[start_index:end_index]
array[index.start:index.end]
array[Slice.start:]
array[(x // y):]

Wrong:

array[my_dict["start_index"]:my_list[-1]]
array[start_index():index.end()]
array[Slice().start:]
array[-(-x + 1):]
array[(x * (y + 2)):]
array[(x + y + z):]

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

The new check WPS481 found a complex slice in the wemake_python_styleguide/violations/base.py file. It has been updated and moved to a separate variable.

end_index = len(text) - len(lstripped_text)
leading_whitespaces = text[:end_index]

Copy link

codecov bot commented May 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (71eebb4) to head (b905e49).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #3433   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          362       363    +1     
  Lines        11987     12042   +55     
  Branches       821       831   +10     
=========================================
+ Hits         11987     12042   +55     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MIXgleb MIXgleb force-pushed the issue-3427 branch 2 times, most recently from 39139a5 to 2ea329b Compare May 29, 2025 18:19
Copy link
Member

@sobolevn sobolevn 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 the PR! As I said in the issue, I am still not sure about it. But, with this PR it would be easier for me to analyze existing code.

@MIXgleb
Copy link
Contributor Author

MIXgleb commented May 30, 2025

I also have an idea to allow the use of certain functions, such as len and count.

This will be stored in a global list for user customization:
FUNCTION_IN_SLICE_WHITELIST: Final = frozenset({'len', 'count'})

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 complex grammar in slice
2 participants