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

Extend RUF055 with more patterns #14738

Open
MichaReiser opened this issue Dec 2, 2024 · 0 comments
Open

Extend RUF055 with more patterns #14738

MichaReiser opened this issue Dec 2, 2024 · 0 comments
Labels
rule Implementing or modifying a lint rule

Comments

@MichaReiser
Copy link
Member

MichaReiser commented Dec 2, 2024

Here are some slightly more complex examples not supported by the initial implementation in #14659:

re.search(r"abc", s) is None       # ⇒ "abc" not in s
re.search(r"abc", s) is not None   # ⇒ "abc" in s
re.sub(r"abc", "", s, count=1)     # ⇒ s.replace("abc", "", count=1)
re.split(r"abc", s, maxsplit=1)    # ⇒ s.split("abc", maxsplit=1)
re.search(r"^abc", s)              # ⇒ s.startswith("abc")
re.search(r"abc$", s)              # ⇒ s.endswith("abc")
re.search(r"^abc$", s)             # ⇒ s == "abc"
re.match(r"abc|xyz|123", s)        # ⇒ s.startswith(("abc", "xyz", "123"))
re.search(r"^abc|^xyz|^123", s)
re.search(r"^(?:abc|xyz|123)", s)
re.search(r"abc$|xyz$|123$", s)    # ⇒ s.endswith(("abc", "xyz", "123"))
re.search(r"(?:abc|xyz|123)$", s)
re.fullmatch(r"abc|xyz|123", s)    # ⇒ s in {"abc", "xyz", "123"}
re.search(r"^(?:abc|xyz|123)$", s)
re.match(r"[abc]", s)              # ⇒ s.startswith(("a", "b", "c"))
re.search(r"[abc]$", s)            # ⇒ s.endswith(("a", "b", "c"))
re.fullmatch(r"[abc]", s)          # ⇒ s in {"a", "b", "c"}
# Handle the ignore case flag
re.match(r"AbC", s, re.I)          # ⇒ s.casefold().startswith("abc")
re.search(r"AbC", s, re.I)         # ⇒ "abc" in s.casefold()
re.fullmatch(r"AbC", s, re.I)      # ⇒ "abc" == s.casefold()
# Handle any escaped special characters
re.fullmatch(r"""!"\#\$%\&'\(\)\*\+,\-\./:;<=>\?@\[\\\]\^_`\{\|\}\~""", s) # ⇒ s == """!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"""

Edit: The .endswith() examples would be unsafe, per #12283 (comment).

Originally posted by @tdulcet in #12283

Great to see that the rule was implemented!

re.search(r"abc$", s) # ⇒ s.endswith("abc")

This is not equivalent:

>>> s = "abc\n"
>>> re.search(r"abc$", s)
<re.Match object; span=(0, 3), match='abc'>
>>> s.endswith("abc")
False

I'm also not sure about the re.I ones; are those transformations correct in the presence of exotic casing conventions like in Turkish?

And I'm not sure the replacement is worth it in cases where the replacement is a little more complex (e.g., passing a tuple to startswith). The goal of the rule should be to make code simpler; with those transformations, that may not be the case any more.

Originally posted by @JelleZijlstra in #12283

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

1 participant