Skip to content

Commit ea90952

Browse files
committed
fix: Don't consider imported objects as public
Discussion-169: mkdocstrings/python#169
1 parent cb8b97a commit ea90952

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/griffe/mixins.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ def is_public(self) -> bool:
367367
# TODO: Return regular True/False values in next version.
368368
if self.public is not None: # type: ignore[attr-defined]
369369
return _True if self.public else _False # type: ignore[return-value,attr-defined]
370-
if self.is_wildcard_exposed:
371-
return _True # type: ignore[return-value]
370+
if self.parent and self.parent.is_module and bool(self.parent.exports): # type: ignore[attr-defined]
371+
return _True if self.name in self.parent.exports else _False # type: ignore[attr-defined,return-value]
372372
if self.has_private_name:
373373
return _False # type: ignore[return-value]
374374
# The following condition effectively filters out imported objects.
@@ -389,6 +389,9 @@ def __init__(self, value: bool) -> None: # noqa: FBT001
389389
def __bool__(self) -> bool:
390390
return self.value
391391

392+
def __repr__(self) -> str:
393+
return repr(self.value)
394+
392395
def __call__(self, *args: Any, **kwargs: Any) -> bool: # noqa: ARG002
393396
warnings.warn(
394397
"This method is now a property and should be accessed as such (without parentheses).",

tests/test_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_diff_griffe(old_code: str, new_code: str, expected_breakages: list[Brea
181181
for breakage, expected_kind in zip(breaking, expected_breakages):
182182
assert breakage.kind is expected_kind
183183
# check with aliases
184-
import_a = "from ._mod_a import a"
184+
import_a = "from ._mod_a import a\n__all__ = ['a']"
185185
old_modules = {"__init__.py": import_a, "_mod_a.py": old_code}
186186
new_modules = {"__init__.py": new_code and import_a, "_mod_a.py": new_code}
187187
with temporary_visited_package("package_old", old_modules) as old_package: # noqa: SIM117

tests/test_public_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Tests for public API handling."""
2+
3+
from griffe.tests import temporary_visited_module
4+
5+
6+
def test_not_detecting_imported_objects_as_public() -> None:
7+
"""Imported objects not listed in `__all__` must not be considered public."""
8+
with temporary_visited_module("from abc import ABC\ndef func(): ...") as module:
9+
assert not module["ABC"].is_public
10+
assert module["func"].is_public # control case

0 commit comments

Comments
 (0)