-
-
Notifications
You must be signed in to change notification settings - Fork 0
Added check for return type to pre-commit #15
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
Conversation
Reviewer's Guide by SourceryThis pull request introduces comprehensive validation for the 'Returns' section in Google-style docstrings, including type annotation checks and handling of 'None' returns. It also enhances type validation to catch bare collection types within nested type annotations. Error handling in Sequence diagram for parsing docstring with type validationsequenceDiagram
participant Parser as GoogleDocstringParser
participant Extractor as _extract_sections
participant DocstringParser as parse
participant ArgsProcessor as _process_args_with_validation
participant ReturnsProcessor as _process_returns_with_validation
participant ReferencesProcessor as _process_references_section
Parser->Extractor: _extract_sections(docstring)
Extractor-->Parser: sections
Parser->DocstringParser: parse(docstring)
DocstringParser-->Parser: parsed
Parser->ArgsProcessor: _process_args_with_validation(sections, parsed, result, validate_types, collect_errors)
ArgsProcessor-->Parser: Updates result with Args
Parser->ReturnsProcessor: _process_returns_with_validation(sections, result, validate_types, collect_errors)
ReturnsProcessor-->Parser: Updates result with Returns
Parser->ReferencesProcessor: _process_references_section(sections, result)
ReferencesProcessor-->Parser: Updates result with References
Parser-->Parser: Updates result with other sections
Parser-->Parser: Removes errors key if no errors
Parser-->User: Returns result
Updated class diagram for google_docstring_parserclassDiagram
class parse_google_docstring {
+docstring: str
+validate_types: bool
+collect_errors: bool
+parse_google_docstring(docstring: str, validate_types: bool, collect_errors: bool)
+Returns: dict[str, Any]
}
note for parse_google_docstring "Added collect_errors parameter"
class _process_returns_with_validation {
+sections: dict[str, str]
+result: dict[str, Any]
+validate_types: bool
+collect_errors: bool
+_process_returns_with_validation(sections: dict[str, str], result: dict[str, Any], validate_types: bool, collect_errors: bool)
+Returns: None
}
class _validate_type_with_error_handling {
+type_str: str
+result: dict[str, Any]
+collect_errors: bool
+_validate_type_with_error_handling(type_str: str, result: dict[str, Any], collect_errors: bool)
+Returns: None
}
class _process_args_with_validation {
+sections: dict[str, str]
+parsed: Docstring
+result: dict[str, Any]
+validate_types: bool
+collect_errors: bool
+_process_args_with_validation(sections: dict[str, str], parsed: Docstring, result: dict[str, Any], validate_types: bool, collect_errors: bool)
+Returns: None
}
class _process_references_section {
+sections: dict[str, str]
+result: dict[str, Any]
+_process_references_section(sections: dict[str, str], result: dict[str, Any])
+Returns: None
}
Updated class diagram for type_validationclassDiagram
class _is_bare_collection_in_nested_type {
+token: str
+tokens: list[str]
+i: int
+bracket_stack: list[str]
+_is_bare_collection_in_nested_type(token: str, tokens: list[str], i: int, bracket_stack: list[str])
+Returns: bool
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a pre-commit check for return type annotations in docstrings and refactors the type validation workflow. Key changes include:
- Updating check_returns_type in tools/check_docstrings.py to validate the Returns section.
- Modifying tests to validate error collection instead of raising, and adjusting expected error messages.
- Refactoring functions in google_docstring_parser/google_docstring_parser.py to include error collection (via a new collect_errors flag) for both argument and returns type validations.
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tools/check_docstrings.py | Simplifies check_returns_type and adds a walrus operator for conditional assignment. |
| tests/test_type_validation_edge_cases.py | Adjusts tests to assert error messages instead of expecting exceptions. |
| tests/test_type_validation.py | Updates call signatures to include the collect_errors flag. |
| tests/test_docstring_checker/test_docstring_validation.py | Adds parametrized tests for various Returns section scenarios. |
| tests/test_docstring_checker/test_check_docstrings.py | Tests overall error reporting in the docstring checking tool. |
| pyproject.toml | Bumps the version and updates linting ignores for consistency. |
| google_docstring_parser/type_validation.py | Refines error messaging for bare collection types. |
| google_docstring_parser/google_docstring_parser.py | Refactors several functions to support error collection in type validation. |
Comments suppressed due to low confidence (2)
google_docstring_parser/google_docstring_parser.py:375
- [nitpick] Consider expanding the docstring for _validate_type_with_error_handling to explicitly describe the behavior when collect_errors is True versus when it is False, so users clearly understand whether errors are collected or raised.
def _validate_type_with_error_handling(type_str: str, result: dict[str, Any], collect_errors: bool) -> None:
google_docstring_parser/google_docstring_parser.py:476
- [nitpick] The function name _process_returns_section is very similar to _process_returns_with_validation; consider renaming one (for example, to _parse_returns_section) to clearly differentiate their responsibilities.
def _process_returns_section(sections: dict[str, str], *, validate_types: bool) -> dict[str, str] | str:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ternaus - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a changelog entry to document the new feature and any breaking changes.
- The introduction of
collect_errorsadds complexity; ensure its usage is consistent and well-understood throughout the codebase.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Summary by Sourcery
Enhance docstring parsing with improved type validation and error handling
New Features:
Enhancements:
Tests:
Chores: