Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc][api] add a function to check if an api is really public #46261

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ci/ray_ci/doc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,23 @@ def get_canonical_name(self) -> str:
attribute = getattr(attribute, token)

return f"{attribute.__module__}.{attribute.__qualname__}"

def _is_private_name(self) -> bool:
"""
Check if this API has a private name. Private names are those that start with
underscores.
"""
name_has_underscore = self.name.split(".")[-1].startswith("_")
is_internal = ".internal." in self.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this has been addressed in a follow-up PR, but should be ._internal?


return name_has_underscore or is_internal

def is_public(self) -> bool:
"""
Check if this API is public. Public APIs are those that are annotated as public
and not have private names.
"""
return (
self.annotation_type == AnnotationType.PUBLIC_API
and not self._is_private_name()
)
2 changes: 0 additions & 2 deletions ci/ray_ci/doc/autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ def _parse_autodoc_rst(self, rst_file: str) -> List[API]:
with open(rst_file, "r") as f:
line = f.readline()
while line:
line = line.strip()

# parse currentmodule block
if line.startswith(_SPHINX_CURRENTMODULE_HEADER):
module = line[len(_SPHINX_CURRENTMODULE_HEADER) :].strip()
Expand Down
49 changes: 49 additions & 0 deletions ci/ray_ci/doc/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,54 @@ def test_get_canonical_name():
)


def test_is_private_name():
test_data = [
{
"input": "a.b._private_function",
"output": True,
},
{
"input": "a.b.internal.public_function",
"output": True,
},
{
"input": "b.c.public_class",
"output": False,
},
]
for test in test_data:
assert (
API(
name=test["input"],
annotation_type=AnnotationType.UNKNOWN,
code_type=CodeType.FUNCTION,
)._is_private_name()
== test["output"]
)


def test_is_public():
assert not API(
name="a.b._private_function",
annotation_type=AnnotationType.PUBLIC_API,
code_type=CodeType.FUNCTION,
).is_public()
assert not API(
name="a.b.internal.public_function",
annotation_type=AnnotationType.PUBLIC_API,
code_type=CodeType.FUNCTION,
).is_public()
assert not API(
name="a.b.public_function",
annotation_type=AnnotationType.DEPRECATED,
code_type=CodeType.FUNCTION,
).is_public()
assert API(
name="a.b.public_function",
annotation_type=AnnotationType.PUBLIC_API,
code_type=CodeType.FUNCTION,
).is_public()


if __name__ == "__main__":
sys.exit(pytest.main(["-v", __file__]))
2 changes: 1 addition & 1 deletion ci/ray_ci/doc/test_autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_walk():
f.write("\tapi_02.rst\n")
with open(os.path.join(tmp, "api_01.rst"), "w") as f:
f.write(".. currentmodule:: ci.ray_ci.doc\n")
f.write(".. autosummary::\n\n")
f.write(".. autosummary::\n")
f.write("\t~mock.mock_function\n")
f.write("\tmock.mock_module.mock_w00t\n")
with open(os.path.join(tmp, "api_02.rst"), "w") as f:
Expand Down
Loading