Why use named groups only to never use any of them later on in the code?
This rule raises issues every time named groups are:
- referenced while not defined;
- defined but called elsewhere in the code by their number instead.
Noncompliant code example
import re
def foo():
pattern = re.compile(r"(?P<a>.)")
matches = pattern.match("abc")
g1 = matches.group("b") # Noncompliant - group "b" is not defined
g2 = matches.group(1) # Noncompliant - Directly use 'a' instead of its group number.
Compliant solution
import re
def foo():
pattern = re.compile(r"(?P<a>.)")
matches = pattern.match("abc")
g = matches.group("a")