Skip to content

Commit 74d4ec9

Browse files
committed
Don't add __all__ if the file contains a ` # noqa: DALL000 comment.
1 parent a7201fd commit 74d4ec9

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

flake8_dunder_all/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
A Flake8 plugin and pre-commit hook which checks to ensure modules have defined ``__all__``.
66
"""
77
#
8-
# Copyright (c) 2020 Dominic Davis-Foster <dominic@davis-foster.co.uk>
8+
# Copyright (c) 2020-2022 Dominic Davis-Foster <dominic@davis-foster.co.uk>
99
#
1010
# Based on flake8_2020
1111
# Copyright (c) 2019 Anthony Sottile
@@ -39,6 +39,7 @@
3939
from domdf_python_tools.paths import PathPlus
4040
from domdf_python_tools.typing import PathLike
4141
from domdf_python_tools.utils import stderr_writer
42+
from flake8.style_guide import find_noqa # type: ignore
4243

4344
# this package
4445
from flake8_dunder_all.utils import get_docstring_lineno, mark_text_ranges
@@ -226,8 +227,16 @@ def check_and_add_all(filename: PathLike, quote_type: str = '"') -> int:
226227
:param filename: The filename of the Python source file (``.py``) to check.
227228
:param quote_type: The type of quote to use for strings.
228229
229-
:returns: ``0`` if the file already contains a ``__all__`` declaration or has no function or class definitions;
230-
``1`` otherwise. ``4`` indicates an error parsing the file.
230+
:returns:
231+
232+
* ``0`` if the file already contains a ``__all__`` declaration,
233+
has no function or class definitions, or has a `` # noqa: DALL000 ` comment.
234+
* ``1`` If ``__all__`` is absent.
235+
* ``4`` if an error was encountered when parsing the file.
236+
237+
.. versionchanged:: 0.2.0
238+
239+
Now returns ``0`` and doesn't add ``__all__`` if the file contains a `` # noqa: DALL000 ` comment.
231240
"""
232241

233242
quotes = {"'", '"'}
@@ -238,6 +247,11 @@ def check_and_add_all(filename: PathLike, quote_type: str = '"') -> int:
238247

239248
try:
240249
source = filename.read_text()
250+
for line in source.splitlines():
251+
noqas = find_noqa(line)
252+
if noqas is not None and "DALL000" in noqas.group(1).upper().split(','):
253+
return 0
254+
241255
tree = ast.parse(source)
242256
if sys.version_info < (3, 8): # pragma: no cover (<py38)
243257
mark_text_ranges(tree, source)

tests/test_main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
pytest.param(testing_source_g, ["a_function"], 1, id="async function no __all__"),
4444
pytest.param(testing_source_h, [], 0, id="from import"),
4545
pytest.param(testing_source_i, [], 1, id="lots of lines"),
46+
pytest.param(
47+
f" # noqa: DALL000 \n{testing_source_g}",
48+
[],
49+
0,
50+
id="async function no __all__ and noqa",
51+
),
4652
]
4753
)
4854
def test_main(tmp_pathplus: PathPlus, source: str, members: List[str], ret: int):

0 commit comments

Comments
 (0)