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

Commit 6edc29b

Browse files
committed
Merge pull request #139 from Nurdok/D105
Added D105: missing docstring in magic method
2 parents 44f1dd1 + ac34b3a commit 6edc29b

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

docs/release_notes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ New Features
1212
previously resulted in D100 errors ("Missing docstring in public module")
1313
will now result in D104 (#105, #127).
1414

15+
* Added the D105 error code - "Missing docstring in magic method'. This new
16+
error is turned on by default. Missing docstrings in magic method which
17+
previously resulted in D102 error ("Missing docstring in public method")
18+
will now result in D105. Note that exceptions to this rule are variadic
19+
magic methods - specifically `__init__`, `__call__` and `__new__`, which
20+
will be considered non-magic and missing docstrings in them will result
21+
in D102 (#60, #139).
22+
1523
* Support the option to exclude all error codes. Running pep257 with
1624
`--select=` (or `select=` in the configuration file) will exclude all errors
1725
which could then be added one by one using `add-select`. Useful for projects

src/pep257.py

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

7071

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

7475

7576
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)
7780

7881

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

161164
class 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):
628633
D102 = D1xx.create_error('D102', 'Missing docstring in public method')
629634
D103 = D1xx.create_error('D103', 'Missing docstring in public function')
630635
D104 = D1xx.create_error('D104', 'Missing docstring in public package')
636+
D105 = D1xx.create_error('D105', 'Missing docstring in magic method')
631637

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

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)