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

proposed: by Bruno Clermont

authorAlain Leufroy <alain.leufroy@logilab.fr>
changesetd706dd303f1b
branchdefault
phasepublic
hiddenno
parent revision#51ee3f8f32c7 built-in checkers use [01-50] base message ids (closes #68057)
child revision#5aa6eda6d3a4 Fix --ignore option documentation to match reality (closes #22273)
files modified by this revision
ChangeLog
checkers/typecheck.py
examples/pylintrc
# HG changeset patch
# User Alain Leufroy <alain.leufroy@logilab.fr>
# Date 1310133124 -7200
# Fri Jul 08 15:52:04 2011 +0200
# Node ID d706dd303f1b56bcffdeade49419c7ce8e8a97b7
# Parent 51ee3f8f32c7cc4f120916a98243053246ee7e8a
add regular expression support for generated-members (closes #69738)

proposed: by Bruno Clermont

diff --git a/ChangeLog b/ChangeLog
@@ -1,9 +1,11 @@
1  ChangeLog for PyLint
2  ====================
3 
4 -	--
5 +
6 +    * #69738: add regular expressions support for "generated-members"
7 +
8      * ids of logging and string_format checkers have been changed:
9        logging: 65 -> 12, string_format: 99 -> 13
10        Also add documentation to say that ids of range 1-50 shall be reserved
11        to pylint internal checkers
12 
diff --git a/checkers/typecheck.py b/checkers/typecheck.py
@@ -14,10 +14,13 @@
13  # this program; if not, write to the Free Software Foundation, Inc.,
14  # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  """try to find more bugs in the code using astng inference capabilities
16  """
17 
18 +import re
19 +import shlex
20 +
21  from logilab import astng
22  from logilab.astng import InferenceError, NotFoundError, YES, Instance
23 
24  from pylint.interfaces import IASTNGChecker
25  from pylint.checkers import BaseChecker
@@ -90,15 +93,15 @@
26  of Zope acquired attributes to generated-members.'}
27                  ),
28                 ('generated-members',
29                  {'default' : (
30          'REQUEST', 'acl_users', 'aq_parent'),
31 -                 'type' : 'csv',
32 +                 'type' : 'string',
33                   'metavar' : '<members names>',
34                   'help' : 'List of members which are set dynamically and \
35  missed by pylint inference system, and so shouldn\'t trigger E0201 when \
36 -accessed.'}
37 +accessed. Python regular expressions are accepted.'}
38                  ),
39          )
40 
41      def open(self):
42          # do this in open since config not fully initialized in __init__
@@ -120,13 +123,22 @@
43          to avoid to much false positives for now, we'll consider the code as
44          correct if a single of the inferred nodes has the accessed attribute.
45 
46          function/method, super call and metaclasses are ignored
47          """
48 -        if node.attrname in self.generated_members:
49 +        # generated_members may containt regular expressions
50 +        # (surrounded by quote `"` and followed by a comma `,`)
51 +        # REQUEST,aq_parent,"[a-zA-Z]+_set{1,2}"' =>
52 +        # ('REQUEST', 'aq_parent', '[a-zA-Z]+_set{1,2}')
53 +        if isinstance(self.config.generated_members, str):
54 +            gen = shlex.shlex(self.config.generated_members)
55 +            gen.whitespace += ','
56 +            self.config.generated_members = tuple(tok.strip('"') for tok in gen)
57 +        for pattern in self.config.generated_members:
58              # attribute is marked as generated, stop here
59 -            return
60 +            if re.match(pattern, node.attrname):
61 +                return
62          try:
63              infered = list(node.expr.infer())
64          except InferenceError:
65              return
66          # list of (node, nodename) which are missing the attribute
diff --git a/examples/pylintrc b/examples/pylintrc
@@ -111,12 +111,13 @@
67  # When zope mode is activated, add a predefined set of Zope acquired attributes
68  # to generated-members.
69  zope=no
70 
71  # List of members which are set dynamically and missed by pylint inference
72 -# system, and so shouldn't trigger E0201 when accessed.
73 -generated-members=REQUEST,acl_users,aq_parent
74 +# system, and so shouldn't trigger E0201 when accessed. Note that regular
75 +# expressions are accepted (surrounded by quote `"` and followed by a comma `,`)
76 +generated-members=REQUEST,acl_users,aq_parent,"[a-zA-Z]+_set{1,2}",
77 
78 
79  [BASIC]
80 
81  # Required attributes for module, separated by a comma