Skip to content
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

Issue 1011: Non strict operation with slice #3373

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

skv0zsneg
Copy link
Contributor

I have made things!

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

Description

Hi! I've add non strict slice operation checking. I guess there is can be some use cases that I might not have remembered. Hope we can figure this out 🤝

'expression',
[
# reverse
'items = items[::-1]',
Copy link
Member

Choose a reason for hiding this comment

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

items = part is not needed

class StricterSliceOperations(base.BaseNodeVisitor):
"""Check for stricter operation with slices."""

def visit_Slice(self, node: ast.Slice):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def visit_Slice(self, node: ast.Slice):
def visit_Slice(self, node: ast.Slice) -> None:

self._check_pop_through_slice(node)
self.generic_visit(node)

def _check_reverse_through_slice(self, node: ast.Slice):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def _check_reverse_through_slice(self, node: ast.Slice):
def _check_reverse_through_slice(self, node: ast.Slice) -> None:

(and all other places)

Copy link

codecov bot commented Apr 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e1d1084) to head (c483b16).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #3373   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          361       362    +1     
  Lines        11830     11881   +51     
  Branches       808       815    +7     
=========================================
+ Hits         11830     11881   +51     

☔ 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.

Comment on lines +17 to +21
'other = items[:]',
'other = items[::]',
# pop
'items = items[:-1]',
'some, items = "some", items[:-1]',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'other = items[:]',
'other = items[::]',
# pop
'items = items[:-1]',
'some, items = "some", items[:-1]',
'items[:]',
'items[::]',
# pop
'items[:-1]',
'items[None:-1]',
'items[None:-1:1]',

Copy link
Member

Choose a reason for hiding this comment

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

Don't forget to test corner cases with None and explicit steps.

[
# copy
'items[::]',
'items[:]',
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I don't udnerstand why this works. It should always raise an error. [:] slice must be forbidden by this rule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works because I check assign node for closest parent before add violation. Will fix it 👍

'items[0]',
'items[:2]',
'a = items[1::-1]',
'a = items[0]',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'a = items[0]',
'items[0:]',

# other
'items[0]',
'items[:2]',
'a = items[1::-1]',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'a = items[1::-1]',
'items[1::-1]',
'items[1:4:-1]',
'items[:x]',
'items[:-x]',
'items[::-x]',
'items[::x]',

Comment on lines +3062 to +3075
# bad
items = items[::-1]
# good
items.reverse()

# bad
other = items[:]
# good
other = items.copy()

# bad
items = items[:-1]
# good
items.pop()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# bad
items = items[::-1]
# good
items.reverse()
# bad
other = items[:]
# good
other = items.copy()
# bad
items = items[:-1]
# good
items.pop()
# Correct:
items.reverse()
items.copy()
items.pop()
# Wrong:
items[::-1] # .reverse()
items[:] # .copy()
items[:-1] # .pop()

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.

2 participants