@@ -66,14 +66,17 @@ def next(obj, default=nothing):
6666NO_VIOLATIONS_RETURN_CODE = 0
6767VIOLATIONS_RETURN_CODE = 1
6868INVALID_OPTIONS_RETURN_CODE = 2
69+ VARIADIC_MAGIC_METHODS = ('__init__' , '__call__' , '__new__' )
6970
7071
7172def humanize (string ):
7273 return re (r'(.)([A-Z]+)' ).sub (r'\1 \2' , string ).lower ()
7374
7475
7576def 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 )
7780
7881
7982def is_ascii (string ):
@@ -154,8 +157,8 @@ class Function(Definition):
154157 def is_public (self ):
155158 if self .all is not None :
156159 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 ('_' )
159162
160163
161164class NestedFunction (Function ):
@@ -172,7 +175,9 @@ def is_public(self):
172175 # Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
173176 if re (r"^{0}\." .format (self .name )).match (decorator .name ):
174177 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 ))
176181 return self .parent .is_public and name_is_public
177182
178183
@@ -628,6 +633,7 @@ def to_rst(cls):
628633D102 = D1xx .create_error ('D102' , 'Missing docstring in public method' )
629634D103 = D1xx .create_error ('D103' , 'Missing docstring in public function' )
630635D104 = D1xx .create_error ('D104' , 'Missing docstring in public package' )
636+ D105 = D1xx .create_error ('D105' , 'Missing docstring in magic method' )
631637
632638D2xx = ErrorRegistry .create_group ('D2' , 'Whitespace Issues' )
633639D200 = D2xx .create_error ('D200' , 'One-line docstring should fit on one line '
@@ -1347,8 +1353,9 @@ def check_docstring_missing(self, definition, docstring):
13471353 if (not docstring and definition .is_public or
13481354 docstring and is_blank (eval (docstring ))):
13491355 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 }
13521359 return codes [type (definition )]()
13531360
13541361 @check_for (Definition )
0 commit comments