@@ -67,14 +67,17 @@ def next(obj, default=nothing):
6767NO_VIOLATIONS_RETURN_CODE = 0
6868VIOLATIONS_RETURN_CODE = 1
6969INVALID_OPTIONS_RETURN_CODE = 2
70+ VARIADIC_MAGIC_METHODS = ('__init__' , '__call__' , '__new__' )
7071
7172
7273def humanize (string ):
7374 return re (r'(.)([A-Z]+)' ).sub (r'\1 \2' , string ).lower ()
7475
7576
7677def 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 )
7881
7982
8083def is_ascii (string ):
@@ -156,8 +159,8 @@ class Function(Definition):
156159 def is_public (self ):
157160 if self .all is not None :
158161 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 ('_' )
161164
162165
163166class NestedFunction (Function ):
@@ -174,7 +177,9 @@ def is_public(self):
174177 # Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
175178 if re (r"^{0}\." .format (self .name )).match (decorator .name ):
176179 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 ))
178183 return self .parent .is_public and name_is_public
179184
180185
@@ -633,6 +638,7 @@ def to_rst(cls):
633638D102 = D1xx .create_error ('D102' , 'Missing docstring in public method' )
634639D103 = D1xx .create_error ('D103' , 'Missing docstring in public function' )
635640D104 = D1xx .create_error ('D104' , 'Missing docstring in public package' )
641+ D105 = D1xx .create_error ('D105' , 'Missing docstring in magic method' )
636642
637643D2xx = ErrorRegistry .create_group ('D2' , 'Whitespace Issues' )
638644D200 = D2xx .create_error ('D200' , 'One-line docstring should fit on one line '
@@ -988,8 +994,9 @@ def check_docstring_missing(self, definition, docstring):
988994 if (not docstring and definition .is_public or
989995 docstring and is_blank (eval (docstring ))):
990996 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 }
9931000 return codes [type (definition )]()
9941001
9951002 @check_for (Definition )
0 commit comments