diff --git a/docs/checks.md b/docs/checks.md index f950da2..9a88c31 100644 --- a/docs/checks.md +++ b/docs/checks.md @@ -1872,7 +1872,7 @@ if x is None: ## FURB170: `use-regex-pattern-methods` -Categories: `readability` `regex` +Categories: `performance` `readability` `regex` If you are passing a compiled regular expression to a regex function, consider calling the regex method on the pattern itself: It is faster, and diff --git a/refurb/checks/regex/use_pattern_method.py b/refurb/checks/regex/use_pattern_method.py index a8ece2d..d6b0511 100644 --- a/refurb/checks/regex/use_pattern_method.py +++ b/refurb/checks/regex/use_pattern_method.py @@ -1,8 +1,8 @@ from dataclasses import dataclass -from mypy.nodes import CallExpr, RefExpr, Var +from mypy.nodes import CallExpr, RefExpr -from refurb.checks.common import is_same_type +from refurb.checks.common import get_mypy_type, is_same_type from refurb.error import Error @@ -36,7 +36,7 @@ class ErrorInfo(Error): name = "use-regex-pattern-methods" code = 170 - categories = ("readability", "regex") + categories = ("performance", "readability", "regex") # This table represents the function calls that we will emit errors for. The @@ -74,12 +74,8 @@ def check(node: CallExpr, errors: list[Error]) -> None: if not arg_format: return - match pattern: - case RefExpr(node=Var(type=ty)) if is_same_type(ty, "re.Pattern"): - pass - - case _: - return + if not is_same_type(get_mypy_type(pattern), "re.Pattern"): + return min_len = len([arg for arg in arg_format if arg is ...]) diff --git a/test/data/err_170.py b/test/data/err_170.py index 19391f9..7e761fa 100644 --- a/test/data/err_170.py +++ b/test/data/err_170.py @@ -21,6 +21,11 @@ _ = search(PATTERN, "hello world") +class C: + p: re.Pattern + +_ = re.sub(C().p, "hello world", "goodbye world") + # these should not diff --git a/test/data/err_170.txt b/test/data/err_170.txt index 700b0fd..bb0dc86 100644 --- a/test/data/err_170.txt +++ b/test/data/err_170.txt @@ -12,3 +12,4 @@ test/data/err_170.py:18:5 [FURB170]: Replace `re.sub(x, ..., ..., count=...)` wi test/data/err_170.py:19:5 [FURB170]: Replace `re.subn(x, ..., ...)` with `x.subn(..., ...)` test/data/err_170.py:20:5 [FURB170]: Replace `re.subn(x, ..., ..., count=...)` with `x.subn(..., ..., count=...)` test/data/err_170.py:22:5 [FURB170]: Replace `re.search(x, ...)` with `x.search(...)` +test/data/err_170.py:27:5 [FURB170]: Replace `re.sub(x, ..., ...)` with `x.sub(..., ...)`