Skip to content

Commit

Permalink
fix: Don't consider imported objects as public
Browse files Browse the repository at this point in the history
Discussion-169: mkdocstrings/python#169
  • Loading branch information
pawamoy committed Jun 17, 2024
1 parent cb8b97a commit ea90952
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/griffe/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ def is_public(self) -> bool:
# TODO: Return regular True/False values in next version.
if self.public is not None: # type: ignore[attr-defined]
return _True if self.public else _False # type: ignore[return-value,attr-defined]
if self.is_wildcard_exposed:
return _True # type: ignore[return-value]
if self.parent and self.parent.is_module and bool(self.parent.exports): # type: ignore[attr-defined]
return _True if self.name in self.parent.exports else _False # type: ignore[attr-defined,return-value]
if self.has_private_name:
return _False # type: ignore[return-value]
# The following condition effectively filters out imported objects.
Expand All @@ -389,6 +389,9 @@ def __init__(self, value: bool) -> None: # noqa: FBT001
def __bool__(self) -> bool:
return self.value

def __repr__(self) -> str:
return repr(self.value)

def __call__(self, *args: Any, **kwargs: Any) -> bool: # noqa: ARG002
warnings.warn(
"This method is now a property and should be accessed as such (without parentheses).",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_diff_griffe(old_code: str, new_code: str, expected_breakages: list[Brea
for breakage, expected_kind in zip(breaking, expected_breakages):
assert breakage.kind is expected_kind
# check with aliases
import_a = "from ._mod_a import a"
import_a = "from ._mod_a import a\n__all__ = ['a']"
old_modules = {"__init__.py": import_a, "_mod_a.py": old_code}
new_modules = {"__init__.py": new_code and import_a, "_mod_a.py": new_code}
with temporary_visited_package("package_old", old_modules) as old_package: # noqa: SIM117
Expand Down
10 changes: 10 additions & 0 deletions tests/test_public_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Tests for public API handling."""

from griffe.tests import temporary_visited_module


def test_not_detecting_imported_objects_as_public() -> None:
"""Imported objects not listed in `__all__` must not be considered public."""
with temporary_visited_module("from abc import ABC\ndef func(): ...") as module:
assert not module["ABC"].is_public
assert module["func"].is_public # control case

0 comments on commit ea90952

Please # to comment.