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

[tools] Fix API doc string validation #2344

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/standard_library_tests_and_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ jobs:

# Ensure `FileCheck` from the pre-installed LLVM 15 package is visible
echo $(brew --prefix llvm@15)/bin/ >> $GITHUB_PATH

- name: Validate documentation strings
run: |
mojo doc -warn-missing-doc-strings -o /dev/null stdlib/src/ > warnings.txt 2>&1
./check-file-is-empty.py warnings.txt
if [ $? -ne 0 ]; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion Unfortunately, this won't work as you hope since the above command still exits with exit code 0 since it produces warnings (not errors). You can verify this locally:

 mojo doc -warn-missing-doc-strings -o /dev/null stdlib/src/
Included from /Users/joe/dev/mojo/stdlib/src/collections/__init__.mojo:1:
/Users/joe/dev/mojo/stdlib/src/collections/dict.mojo:49:7: warning: public symbol 'StringableKeyElement' is missing a doc string
trait StringableKeyElement(KeyElement, Stringable):
      ^
Included from /Users/joe/dev/mojo/stdlib/src/os/__init__.mojo:1:
/Users/joe/dev/mojo/stdlib/src/os/os.mojo:274:4: warning: function takes parameters, but no 'Parameters' in doc string
fn remove[pathlike: os.PathLike](path: pathlike) raises:
   ^
Included from /Users/joe/dev/mojo/stdlib/src/os/__init__.mojo:1:
/Users/joe/dev/mojo/stdlib/src/os/os.mojo:298:4: warning: function takes parameters, but no 'Parameters' in doc string
fn unlink[pathlike: os.PathLike](path: pathlike) raises:
   ^
Included from /Users/joe/dev/mojo/stdlib/src/testing/__init__.mojo:1:
/Users/joe/dev/mojo/stdlib/src/testing/testing.mojo:115:4: warning: function takes parameters, but no 'Parameters' in doc string
fn assert_equal[T: Testable](lhs: T, rhs: T, msg: String = "") raises:
   ^
Included from /Users/joe/dev/mojo/stdlib/src/testing/__init__.mojo:1:
/Users/joe/dev/mojo/stdlib/src/testing/testing.mojo:175:4: warning: function takes parameters, but no 'Parameters' in doc string
fn assert_not_equal[T: Testable](lhs: T, rhs: T, msg: String = "") raises:

combined with echo $? which reports the exit code of 0.

To "fix" this problem, you can use the check-file-is-empty.py script from #2118 that got removed (just bring it back for this PR).

Longer term, I think we should file a GitHub issue and have a discussion with @River707 and @modocache on their take on this command. The current structure of producing warnings for invalid API doc strings and exiting with an exit code of 0, in my opinion, pushes complexity to every user (such as with this check-file-is-empty.py script). I'd rather have some option (even if it's -error-missing-doc-strings or name to be bikeshedded) which would give me a non-zero exit code if there are any invalid doc strings. I think that will be the common use case for users of this command.

Copy link
Author

Choose a reason for hiding this comment

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

That is correct, i will look into it and make changes accordingly by looking at the mentioned file.

echo "::error::Documentation string validation failed. Please fix the issues and try again."
exit 1
fi


- name: Run standard library tests and examples
run: |
Expand Down
9 changes: 9 additions & 0 deletions stdlib/docs/style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ pip install pre-commit
pre-commit install
```

#### API Doc String Validation

Mojo provides a command line utility, `mojo doc`, to validate the API doc strings in your code. This ensures that your doc strings are correctly formatted and consistent with the Mojo style guidelines.

```bash
> mojo doc -warn-missing-doc-strings -o /dev/null example.mojo
All done! ✨ 🍰 ✨
1 file left unchanged.
```
and that's it!

#### Whitespace
Expand Down
45 changes: 45 additions & 0 deletions stdlib/scripts/check-file-is-empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2024, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #

import argparse
import os
import sys


def main():
parser = argparse.ArgumentParser(
description=(
"Exits successfully if the file at the given path is empty or does"
" not exist. Otherwise, prints the file's contents, then exits"
" unsuccessfully."
)
)
parser.add_argument("path")
args = parser.parse_args()

if not os.path.exists(args.path):
return

with open(args.path, "r") as f:
content = f.read().strip()
if content:
print(
f"error: '{args.path}' is not empty:\n{content}",
file=sys.stderr,
)
exit(1)


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions stdlib/scripts/check-licenses.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def main():
# this is the current file
continue
file_path = Path(target_paths[i])
file_content = file_path.read_text()
if file_content.startswith("#!"):
file_content = "\n".join(file_content.split("\n")[1:])
if not file_path.read_text().startswith(LICENSE):
files_without_license.append(file_path)

Expand Down
5 changes: 5 additions & 0 deletions stdlib/src/builtin/reversed.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ fn reversed[
Args:
value: The list to get the reversed iterator of.

Parameters:
mutability: Indicates if the list is mutable or immutable.
self_life: The lifetime policy for the list.
T: The type of elements contained in the list.

Returns:
The reversed iterator of the list.
"""
Expand Down
5 changes: 5 additions & 0 deletions stdlib/src/collections/optional.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ struct Optional[T: CollectionElement](CollectionElement, Boolable):
value (for instance with `or_else`), you'll get garbage unsafe data out.

Parameters:
<<<<<<< HEAD
mutability: Indicates if the optional is mutable or immutable.
self_life: The lifetime policy for the optional.
=======
mutability: Whether the Optional is mutable.
self_life: The Optional's lifetime.
>>>>>>> upstream/nightly

Returns:
A reference to the contained data of the option as a Reference[T].
Expand Down