Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 7c19fa7

Browse files
committed
Added D105: missing docstring in magic method
1 parent 1db2853 commit 7c19fa7

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

docs/release_notes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Bug Fixes
2727

2828

2929
0.6.0 - July 20th, 2015
30-
---------------------------
30+
-----------------------
3131

3232
New Features
3333

src/pep257.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ def next(obj, default=nothing):
6767
NO_VIOLATIONS_RETURN_CODE = 0
6868
VIOLATIONS_RETURN_CODE = 1
6969
INVALID_OPTIONS_RETURN_CODE = 2
70+
VARIADIC_MAGIC_METHODS = ('__init__', '__call__', '__new__')
7071

7172

7273
def humanize(string):
7374
return re(r'(.)([A-Z]+)').sub(r'\1 \2', string).lower()
7475

7576

7677
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)
7881

7982

8083
def 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

163166
class 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):
633638
D102 = D1xx.create_error('D102', 'Missing docstring in public method')
634639
D103 = D1xx.create_error('D103', 'Missing docstring in public function')
635640
D104 = D1xx.create_error('D104', 'Missing docstring in public package')
641+
D105 = D1xx.create_error('D105', 'Missing docstring in magic method')
636642

637643
D2xx = ErrorRegistry.create_group('D2', 'Whitespace Issues')
638644
D200 = 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)

src/tests/test_cases/test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def _ok_since_private():
2828
def __init__(self=None):
2929
pass
3030

31+
@expect('D105: Missing docstring in magic method')
32+
def __str__(self=None):
33+
pass
34+
35+
@expect('D102: Missing docstring in public method')
36+
def __call__(self=None, x=None, y=None, z=None):
37+
pass
38+
3139

3240
@expect('D103: Missing docstring in public function')
3341
def function():

0 commit comments

Comments
 (0)