@@ -67,14 +67,17 @@ def next(obj, default=nothing):
67
67
NO_VIOLATIONS_RETURN_CODE = 0
68
68
VIOLATIONS_RETURN_CODE = 1
69
69
INVALID_OPTIONS_RETURN_CODE = 2
70
+ VARIADIC_MAGIC_METHODS = ('__init__' , '__call__' , '__new__' )
70
71
71
72
72
73
def humanize (string ):
73
74
return re (r'(.)([A-Z]+)' ).sub (r'\1 \2' , string ).lower ()
74
75
75
76
76
77
def is_magic (name ):
77
- return name .startswith ('__' ) and name .endswith ('__' )
78
+ return (name .startswith ('__' ) and
79
+ name .endswith ('__' ) and
80
+ name not in VARIADIC_MAGIC_METHODS )
78
81
79
82
80
83
def is_ascii (string ):
@@ -156,8 +159,8 @@ class Function(Definition):
156
159
def is_public (self ):
157
160
if self .all is not None :
158
161
return self .name in self .all
159
- else : # TODO: are there any magic functions? not methods
160
- return not self .name .startswith ('_' ) or is_magic ( self . name )
162
+ else :
163
+ return not self .name .startswith ('_' )
161
164
162
165
163
166
class NestedFunction (Function ):
@@ -174,7 +177,9 @@ def is_public(self):
174
177
# Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
175
178
if re (r"^{0}\." .format (self .name )).match (decorator .name ):
176
179
return False
177
- name_is_public = not self .name .startswith ('_' ) or is_magic (self .name )
180
+ name_is_public = (not self .name .startswith ('_' ) or
181
+ self .name in VARIADIC_MAGIC_METHODS or
182
+ is_magic (self .name ))
178
183
return self .parent .is_public and name_is_public
179
184
180
185
@@ -633,6 +638,7 @@ def to_rst(cls):
633
638
D102 = D1xx .create_error ('D102' , 'Missing docstring in public method' )
634
639
D103 = D1xx .create_error ('D103' , 'Missing docstring in public function' )
635
640
D104 = D1xx .create_error ('D104' , 'Missing docstring in public package' )
641
+ D105 = D1xx .create_error ('D105' , 'Missing docstring in magic method' )
636
642
637
643
D2xx = ErrorRegistry .create_group ('D2' , 'Whitespace Issues' )
638
644
D200 = D2xx .create_error ('D200' , 'One-line docstring should fit on one line '
@@ -988,8 +994,9 @@ def check_docstring_missing(self, definition, docstring):
988
994
if (not docstring and definition .is_public or
989
995
docstring and is_blank (eval (docstring ))):
990
996
codes = {Module : D100 , Class : D101 , NestedClass : D101 ,
991
- Method : D102 , Function : D103 , NestedFunction : D103 ,
992
- Package : D104 }
997
+ Method : (lambda : D105 () if is_magic (definition .name )
998
+ else D102 ()),
999
+ Function : D103 , NestedFunction : D103 , Package : D104 }
993
1000
return codes [type (definition )]()
994
1001
995
1002
@check_for (Definition )
0 commit comments