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

Commit b11d28d

Browse files
committed
Support for __all__ as a list with square brackets
__all__ can be a list. This adds a failing test for that and fixes it. It also warns to stderr if __all__ is mutable.
1 parent 012e042 commit b11d28d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pep257.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,24 @@ def parse_all(self):
220220
if self.current.value != '=':
221221
raise AllError('Could not evaluate contents of __all__. ')
222222
self.consume(tk.OP)
223-
if self.current.value != '(':
223+
if self.current.value not in '([':
224224
raise AllError('Could not evaluate contents of __all__. ')
225+
if self.current.value == '[':
226+
msg = ("%s WARNING: __all__ is defined as a list, this means "
227+
"pep257 cannot reliably detect contents of the __all__ "
228+
"variable, because it can be mutated. Change __all__ to be "
229+
"an (immutable) tuple, to remove this warning. Note, "
230+
"pep257 uses __all__ to detect which definitions are "
231+
"public, to warn if public definitions are missing "
232+
"docstrings. If __all__ is a (mutable) list, pep257 cannot "
233+
"reliably assume its contents. pep257 will proceed "
234+
"assuming __all__ is not mutated.\n" % self.filename)
235+
sys.stderr.write(msg)
225236
self.consume(tk.OP)
226237
s = '('
227238
if self.current.kind != tk.STRING:
228239
raise AllError('Could not evaluate contents of __all__. ')
229-
while self.current.value != ')':
240+
while self.current.value not in ')]':
230241
s += self.current.value
231242
self.stream.move()
232243
s += ')'

test_definitions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def method_2(self):
2525
def nested_3(self):
2626
"""Nested."""
2727
'''
28+
source_alt = '''
29+
__all__ = ['a', 'b'
30+
'c',]
31+
'''
2832

2933

3034
def test_parser():
@@ -55,6 +59,10 @@ def test_parser():
5559
assert nested_3.module == module
5660
assert nested_3.all == all
5761

62+
module = parse(StringIO(source_alt), 'file_alt.py')
63+
assert Module('file_alt.py', _, 1, len(source_alt.split('\n')),
64+
None, _, _, all) == module
65+
5866

5967
def _test_module():
6068

0 commit comments

Comments
 (0)