Add name type in C0103 message

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changesetcdea6d2431b5
branchdefault
phasepublic
hiddenno
parent revision#6416aabd2d6a integrate patch from Peter Hammond to check protocol completness and avoid false R0903. Closes #104420
child revision#7a1e32ae0c60 don't want to run a checker only because of a Fatal error
files modified by this revision
checkers/base.py
test/messages/func_w0110.txt
test/messages/func_w0133.txt
test/messages/func_w0623.txt
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1348116187 -7200
# Thu Sep 20 06:43:07 2012 +0200
# Node ID cdea6d2431b5b4bd1a407dd1df4620087b87038f
# Parent 6416aabd2d6ad0064f1c25c9cf6a61c8b00d9e7f
Add name type in C0103 message

diff --git a/checkers/base.py b/checkers/base.py
@@ -580,11 +580,11 @@
1      msgs = {
2      'C0102': ('Black listed name "%s"',
3                'blacklisted-name',
4                'Used when the name is listed in the black list (unauthorized \
5                names).'),
6 -    'C0103': ('Invalid name "%s" (should match %s)',
7 +    'C0103': ('Invalid name "%s" for type %s (should match %s)',
8                'invalid-name',
9                'Used when the name doesn\'t match the regular expression \
10                associated to its type (constant, variable, class...).'),
11 
12      }
@@ -714,20 +714,23 @@
13          """check for a name using the type's regexp"""
14          if is_inside_except(node):
15              clobbering, _ = clobber_in_except(node)
16              if clobbering:
17                  return
18 -
19          if name in self.config.good_names:
20              return
21          if name in self.config.bad_names:
22              self.stats['badname_' + node_type] += 1
23              self.add_message('C0102', node=node, args=name)
24              return
25          regexp = getattr(self.config, node_type + '_rgx')
26          if regexp.match(name) is None:
27 -            self.add_message('C0103', node=node, args=(name, regexp.pattern))
28 +            type_label = {'inlinedvar': 'inlined variable',
29 +                          'const': 'constant',
30 +                          'attr': 'attribute',
31 +                          }.get(node_type, node_type)
32 +            self.add_message('C0103', node=node, args=(name, type_label, regexp.pattern))
33              self.stats['badname_' + node_type] += 1
34 
35 
36  class DocStringChecker(_BasicChecker):
37      msgs = {
diff --git a/test/messages/func_w0110.txt b/test/messages/func_w0110.txt
@@ -1,1 +1,1 @@
38 -C:  8:a: Invalid name "a" (should match [a-z_][a-z0-9_]{2,30}$)
39 +C:  8:a: Invalid name "a" for type function (should match [a-z_][a-z0-9_]{2,30}$)
diff --git a/test/messages/func_w0133.txt b/test/messages/func_w0133.txt
@@ -1,9 +1,9 @@
40 -C:  8:Run.B: Invalid name "B" (should match [A-Z_][a-zA-Z0-9]+$)
41 -C: 12:Run: Invalid name "bBb" (should match [a-z_][a-z0-9_]{2,30}$)
42 -C: 26:HOHOHOHO: Invalid name "HOHOHOHO" (should match [a-z_][a-z0-9_]{2,30}$)
43 -C: 28:HOHOHOHO: Invalid name "HIHIHI" (should match [a-z_][a-z0-9_]{2,30}$)
44 -C: 31:xyz: Invalid name "xyz" (should match [A-Z_][a-zA-Z0-9]+$)
45 -C: 36:xyz.Youplapoum: Invalid name "Youplapoum" (should match [a-z_][a-z0-9_]{2,30}$)
46 -C: 46: Invalid name "benpasceluila" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
47 -C: 52:Correct.__init__: Invalid name "_Ca_va_Pas" (should match [a-z_][a-z0-9_]{2,30}$)
48 +C:  8:Run.B: Invalid name "B" for type class (should match [A-Z_][a-zA-Z0-9]+$)
49 +C: 12:Run: Invalid name "bBb" for type variable (should match [a-z_][a-z0-9_]{2,30}$)
50 +C: 26:HOHOHOHO: Invalid name "HOHOHOHO" for type function (should match [a-z_][a-z0-9_]{2,30}$)
51 +C: 28:HOHOHOHO: Invalid name "HIHIHI" for type variable (should match [a-z_][a-z0-9_]{2,30}$)
52 +C: 31:xyz: Invalid name "xyz" for type class (should match [A-Z_][a-zA-Z0-9]+$)
53 +C: 36:xyz.Youplapoum: Invalid name "Youplapoum" for type method (should match [a-z_][a-z0-9_]{2,30}$)
54 +C: 46: Invalid name "benpasceluila" for type constant (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
55 +C: 52:Correct.__init__: Invalid name "_Ca_va_Pas" for type attribute (should match [a-z_][a-z0-9_]{2,30}$)
56  W:  8:Run.B: Unused variable 'B'
diff --git a/test/messages/func_w0623.txt b/test/messages/func_w0623.txt
@@ -1,8 +1,8 @@
57 -C: 28:some_function: Invalid name "FOO" (should match [a-z_][a-z0-9_]{2,30}$)
58 -C: 41: Invalid name "exc3" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
59 -C: 55: Invalid name "OOPS" (should match [a-z_][a-z0-9_]{2,30}$)
60 +C: 28:some_function: Invalid name "FOO" for type variable (should match [a-z_][a-z0-9_]{2,30}$)
61 +C: 41: Invalid name "exc3" for type constant (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
62 +C: 55: Invalid name "OOPS" for type variable (should match [a-z_][a-z0-9_]{2,30}$)
63  W: 18:some_function: Redefining name 'RuntimeError' from object 'exceptions' in exception handler
64  W: 20:some_function: Redefining name 'OSError' from builtins in exception handler
65  W: 20:some_function: Unused variable 'OSError'
66  W: 22:some_function: Redefining name 'MyError' from outer scope (line 7) in exception handler
67  W: 22:some_function: Unused variable 'MyError'