Commits

Alain Leufroy committed d706dd3

add regular expression support for generated-members (closes #69738)

proposed: by Bruno Clermont

  • Participants
  • Parent commits 51ee3f8

Comments (0)

Files changed (3)

 ChangeLog for PyLint
 ====================
 
-	--
+
+    * #69738: add regular expressions support for "generated-members"
+
     * ids of logging and string_format checkers have been changed:
       logging: 65 -> 12, string_format: 99 -> 13
       Also add documentation to say that ids of range 1-50 shall be reserved

File checkers/typecheck.py

 """try to find more bugs in the code using astng inference capabilities
 """
 
+import re
+import shlex
+
 from logilab import astng
 from logilab.astng import InferenceError, NotFoundError, YES, Instance
 
                ('generated-members',
                 {'default' : (
         'REQUEST', 'acl_users', 'aq_parent'),
-                 'type' : 'csv',
+                 'type' : 'string',
                  'metavar' : '<members names>',
                  'help' : 'List of members which are set dynamically and \
 missed by pylint inference system, and so shouldn\'t trigger E0201 when \
-accessed.'}
+accessed. Python regular expressions are accepted.'}
                 ),
         )
 
 
         function/method, super call and metaclasses are ignored
         """
-        if node.attrname in self.generated_members:
+        # generated_members may containt regular expressions
+        # (surrounded by quote `"` and followed by a comma `,`)
+        # REQUEST,aq_parent,"[a-zA-Z]+_set{1,2}"' =>
+        # ('REQUEST', 'aq_parent', '[a-zA-Z]+_set{1,2}')
+        if isinstance(self.config.generated_members, str):
+            gen = shlex.shlex(self.config.generated_members)
+            gen.whitespace += ','
+            self.config.generated_members = tuple(tok.strip('"') for tok in gen)
+        for pattern in self.config.generated_members:
             # attribute is marked as generated, stop here
-            return
+            if re.match(pattern, node.attrname):
+                return
         try:
             infered = list(node.expr.infer())
         except InferenceError:

File examples/pylintrc

 zope=no
 
 # List of members which are set dynamically and missed by pylint inference
-# system, and so shouldn't trigger E0201 when accessed.
-generated-members=REQUEST,acl_users,aq_parent
+# system, and so shouldn't trigger E0201 when accessed. Note that regular
+# expressions are accepted (surrounded by quote `"` and followed by a comma `,`)
+generated-members=REQUEST,acl_users,aq_parent,"[a-zA-Z]+_set{1,2}",
 
 
 [BASIC]