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

Enforce stricter slice operations #1011

Open
orsinium opened this issue Nov 18, 2019 · 9 comments
Open

Enforce stricter slice operations #1011

orsinium opened this issue Nov 18, 2019 · 9 comments
Assignees
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule

Comments

@orsinium
Copy link
Collaborator

orsinium commented Nov 18, 2019

Rule request

Thesis

The list has a set of methods that duplicates operations that you can do with + operator or slices:

# bad
items += [item]
# good
items.append(item)

# bad
items = [item] + items
# good
items.insert(0, item)

# bad
items += other
# good
items.extend(other)

# bad
items = items[::-1]
# good
items.reverse()

# bad
items = sorted(items)
# good
items.sort()

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

# bad
items[:] = []
# good
items.clear()

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

Reasoning

We have two ways to do something. Let's prefer a more descriptive way.

This issue contains a lot of related cases. Consider splitting it by smaller ones.

@orsinium orsinium added the rule request Adding a new rule label Nov 18, 2019
@orsinium
Copy link
Collaborator Author

If you agree and it's implementable, I can later do the same issue for sets, that's also fun.

@sobolevn
Copy link
Member

Let's implement several rules that are not bound to types:

  • items[::-1] should not be used. Use reversed or .reverse()
  • items[:] should not be used. Use .copy()
  • items = items[:-1] should not be used. Use items.pop()

@orsinium as always - awesome suggestions! Thanks!

@sobolevn sobolevn added this to the Version 0.15 milestone Nov 19, 2019
@sobolevn sobolevn changed the title Prefer list methods to + and slice Enforce stricter slice operations Nov 19, 2019
@sobolevn sobolevn added help wanted Extra attention is needed level:starter Good for newcomers labels Nov 19, 2019
@Eric4Jiang
Copy link

Hi! I would love to contribute and try to resolve this issue if no one else is on it.

@sobolevn
Copy link
Member

Sure, @Eric4Jiang! Thanks a lot! Assigned.

@Eric4Jiang
Copy link

Eric4Jiang commented Dec 11, 2019

@sobolevn

Hi! Just to confirm, would items[::] fall into the same category as items[:] because it can be replaced with .copy()?

@sobolevn
Copy link
Member

Sure!

@orsinium
Copy link
Collaborator Author

Hmmm, that gives me an idea for one more rule. I've made #1071. Thank you, @Eric4Jiang!

@bDrwx
Copy link

bDrwx commented Nov 26, 2023

Hi, @sobolevn. I'm gonna try to make this issue. Is it still relevant?

@orsinium
Copy link
Collaborator Author

@bDrwx Thank you! Looks like there is still no implementation for the rule. Go ahead! I've assigned you to the issue 👍🏿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

4 participants