@@ -66,14 +66,17 @@ def next(obj, default=nothing):
66
66
NO_VIOLATIONS_RETURN_CODE = 0
67
67
VIOLATIONS_RETURN_CODE = 1
68
68
INVALID_OPTIONS_RETURN_CODE = 2
69
+ VARIADIC_MAGIC_METHODS = ('__init__' , '__call__' , '__new__' )
69
70
70
71
71
72
def humanize (string ):
72
73
return re (r'(.)([A-Z]+)' ).sub (r'\1 \2' , string ).lower ()
73
74
74
75
75
76
def is_magic (name ):
76
- return name .startswith ('__' ) and name .endswith ('__' )
77
+ return (name .startswith ('__' ) and
78
+ name .endswith ('__' ) and
79
+ name not in VARIADIC_MAGIC_METHODS )
77
80
78
81
79
82
def is_ascii (string ):
@@ -154,8 +157,8 @@ class Function(Definition):
154
157
def is_public (self ):
155
158
if self .all is not None :
156
159
return self .name in self .all
157
- else : # TODO: are there any magic functions? not methods
158
- return not self .name .startswith ('_' ) or is_magic ( self . name )
160
+ else :
161
+ return not self .name .startswith ('_' )
159
162
160
163
161
164
class NestedFunction (Function ):
@@ -172,7 +175,9 @@ def is_public(self):
172
175
# Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
173
176
if re (r"^{0}\." .format (self .name )).match (decorator .name ):
174
177
return False
175
- name_is_public = not self .name .startswith ('_' ) or is_magic (self .name )
178
+ name_is_public = (not self .name .startswith ('_' ) or
179
+ self .name in VARIADIC_MAGIC_METHODS or
180
+ is_magic (self .name ))
176
181
return self .parent .is_public and name_is_public
177
182
178
183
@@ -628,6 +633,7 @@ def to_rst(cls):
628
633
D102 = D1xx .create_error ('D102' , 'Missing docstring in public method' )
629
634
D103 = D1xx .create_error ('D103' , 'Missing docstring in public function' )
630
635
D104 = D1xx .create_error ('D104' , 'Missing docstring in public package' )
636
+ D105 = D1xx .create_error ('D105' , 'Missing docstring in magic method' )
631
637
632
638
D2xx = ErrorRegistry .create_group ('D2' , 'Whitespace Issues' )
633
639
D200 = D2xx .create_error ('D200' , 'One-line docstring should fit on one line '
@@ -1347,8 +1353,9 @@ def check_docstring_missing(self, definition, docstring):
1347
1353
if (not docstring and definition .is_public or
1348
1354
docstring and is_blank (eval (docstring ))):
1349
1355
codes = {Module : D100 , Class : D101 , NestedClass : D101 ,
1350
- Method : D102 , Function : D103 , NestedFunction : D103 ,
1351
- Package : D104 }
1356
+ Method : (lambda : D105 () if is_magic (definition .name )
1357
+ else D102 ()),
1358
+ Function : D103 , NestedFunction : D103 , Package : D104 }
1352
1359
return codes [type (definition )]()
1353
1360
1354
1361
@check_for (Definition )
0 commit comments