Skip to content

Commit

Permalink
add regular expression support for generated-members (closes #69738)
Browse files Browse the repository at this point in the history
proposed: by Bruno Clermont
  • Loading branch information
Alain Leufroy committed Jul 8, 2011
1 parent 67bbca5 commit bc66f92
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 3 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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
Expand Down
20 changes: 16 additions & 4 deletions checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"""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

Expand Down Expand Up @@ -92,11 +95,11 @@ class should be ignored. A mixin class is detected if its name ends with \
('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.'}
),
)

Expand All @@ -122,9 +125,18 @@ def visit_getattr(self, node):
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:
Expand Down
5 changes: 3 additions & 2 deletions examples/pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ ignored-classes=SQLObject
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]
Expand Down

0 comments on commit bc66f92

Please sign in to comment.