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

[New Rule] Merge duplicate startswith / endswith calls #169

Open
Skylion007 opened this issue Feb 21, 2023 · 1 comment
Open

[New Rule] Merge duplicate startswith / endswith calls #169

Skylion007 opened this issue Feb 21, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Skylion007
Copy link
Contributor

Explanation

Explain briefly why you think this makes the code simpler.
This is similar to the isinstance call merging. You can also merge startswith, endswith calls on strings. This means the builtin method only needs to iterate through the string once. flake8-PIE already enables this rule as PIE810.

Example

# Bad
a = "abcdef"
if a.startswith("abc") or a.startswith("xyz"):
     print("alphabet found")

# Good
a = "abcdef"
if a.startswith("abc", "xyz"):
     print("alphabet found")

can be repeated with endswith as well

@Skylion007 Skylion007 added the enhancement New feature or request label Feb 21, 2023
@janosh
Copy link

janosh commented May 6, 2023

Came here to suggest the same rule. 👍

Note that the good example is missing parens:

- if a.startswith("abc", "xyz"):
+ if a.startswith(("abc", "xyz")):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants