Skip to content

Commit 44b7e73

Browse files
committed
Make sure docstub generated stubs are ignored
when walking a source package.
1 parent 4a86e0e commit 44b7e73

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/docstub/_path_utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,15 @@ def _walk_source_package(path, *, ignore_regex):
222222

223223
elif is_python_or_stub_file(path):
224224
stub_path = path.with_suffix(".pyi")
225-
if stub_path == path or not stub_path.is_file():
226-
# If `path` is a stub file return it. If it is a regular Python
227-
# file, only return it if no corresponding stub file exists.
225+
# We check if a corresponding stub exists (even if `path` is a stub),
226+
# one that isn't generated by docstub itself
227+
is_valid_stub = stub_path.is_file() and not is_docstub_generated(stub_path)
228+
229+
if path != stub_path and not is_valid_stub:
230+
# `path` isn't stub and no a stub exists that should take precedence
231+
yield path
232+
elif path == stub_path and is_valid_stub:
233+
# `path` is stub and not generated by docstub
228234
yield path
229235

230236
elif path.is_dir():

tests/test_path_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from docstub._path_utils import walk_source_package
3+
from docstub._path_utils import STUB_HEADER_COMMENT, walk_source_package
44

55

66
class Test_walk_source_package:
@@ -47,6 +47,26 @@ def test_not_a_package(self, tmp_path):
4747
with pytest.raises(TypeError, match=".* must be a Python file or package"):
4848
next(walk_source_package(tmp_path))
4949

50+
def test_single_with_docstub_generated_stub(self, tmp_path):
51+
script_py = tmp_path / "script.py"
52+
script_py.touch()
53+
script_stub = tmp_path / "script.pyi"
54+
with script_stub.open("w") as io:
55+
io.write(STUB_HEADER_COMMENT)
56+
57+
paths = sorted(walk_source_package(script_py))
58+
assert paths == [script_py]
59+
60+
def test_package_with_docstub_generated_stub(self, tmp_path):
61+
init_py = tmp_path / "__init__.py"
62+
init_py.touch()
63+
init_stub = tmp_path / "__init__.pyi"
64+
with init_stub.open("w") as io:
65+
io.write(STUB_HEADER_COMMENT)
66+
67+
paths = sorted(walk_source_package(tmp_path))
68+
assert paths == [init_py]
69+
5070
@pytest.mark.parametrize("name", ["script.py", "script.pyi"])
5171
def test_ignore_single_file(self, tmp_path, name):
5272
top_stub = tmp_path / name

0 commit comments

Comments
 (0)