closes #74742: make allowed name for first argument of class method configurable

(patch by Google)

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changeset68aadb7d88eb
branchdefault
phasepublic
hiddenno
parent revision#511d09d223c8 [doc] more documentation fixes
child revision#a51589d31587 closes #74087: handle case where inference of a module return YES
files modified by this revision
ChangeLog
checkers/classes.py
test/messages/func_e0203.txt
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1315380247 -7200
# Wed Sep 07 09:24:07 2011 +0200
# Node ID 68aadb7d88eb54de6945b00323ddb90f8bf00796
# Parent 511d09d223c84cf4e24a7eb1ce8253f56fd698a9
closes #74742: make allowed name for first argument of class method configurable

(patch by Google)

diff --git a/ChangeLog b/ChangeLog
@@ -1,7 +1,11 @@
1  ChangeLog for PyLint
2  ====================
3 +--
4 +    * #74742: make allowed name for first argument of class method
5 +      configurable (patch by Google)
6 +
7 
8  2011-07-18  --  0.24.0
9      * #69738: add regular expressions support for "generated-members"
10 
11      * ids of logging and string_format checkers have been changed:
diff --git a/checkers/classes.py b/checkers/classes.py
@@ -61,11 +61,11 @@
12                first argument has no argument defined.'),
13      'E0213': ('Method should have "self" as first argument',
14                'Used when a method has an attribute different the "self" as\
15                first argument. This is considered as an error since this is\
16                a so common convention that you shouldn\'t break it!'),
17 -    'C0202': ('Class method should have "cls" as first argument', # E0212
18 +    'C0202': ('Class method should have %s as first argument', # E0212
19                'Used when a class method has an attribute different than "cls"\
20                as first argument, to easily differentiate them from regular \
21                instance methods.'),
22      'C0203': ('Metaclass method should have "mcs" as first argument', # E0214
23                'Used when a metaclass method has an attribute different the \
@@ -154,10 +154,17 @@
24                   'type' : 'csv',
25                   'metavar' : '<method names>',
26                   'help' : 'List of method names used to declare (i.e. assign) \
27  instance attributes.'}
28                  ),
29 +               ('valid-classmethod-first-arg',
30 +                {'default' : ('cls',),
31 +                 'type' : 'csv',
32 +                 'metavar' : '<argument names>',
33 +                 'help' : 'List of valid names for the first argument in \
34 +a class method.'}
35 +                ),
36 
37                 )
38 
39      def __init__(self, linter=None):
40          BaseChecker.__init__(self, linter)
@@ -383,12 +390,20 @@
41          elif metaclass:
42              if first != 'mcs':
43                  self.add_message('C0203', node=node)
44          # class method
45          elif node.type == 'classmethod':
46 -            if first != 'cls':
47 -                self.add_message('C0202', node=node)
48 +            if first not in self.config.valid_classmethod_first_arg:
49 +                if len(self.config.valid_classmethod_first_arg) == 1:
50 +                    valid = repr(self.config.valid_classmethod_first_arg[0])
51 +                else:
52 +                    valid = ', '.join(
53 +                      repr(v)
54 +                      for v in self.config.valid_classmethod_first_arg[:-1])
55 +                    valid = '%s or %r' % (
56 +                        valid, self.config.valid_classmethod_first_arg[-1])
57 +                self.add_message('C0202', args=valid, node=node)
58          # regular method without self as argument
59          elif first != 'self':
60              self.add_message('E0213', node=node)
61 
62      def _check_bases_classes(self, node):
diff --git a/test/messages/func_e0203.txt b/test/messages/func_e0203.txt
@@ -1,2 +1,1 @@
63 -C: 12:Abcd.abcd: Class method should have "cls" as first argument
64 -
65 +C: 12:Abcd.abcd: Class method should have 'cls' as first argument