Add check for valid function postcondition syntax#1306
Merged
david-yz-liu merged 3 commits intopyta-uoft:masterfrom Feb 22, 2026
Merged
Add check for valid function postcondition syntax#1306david-yz-liu merged 3 commits intopyta-uoft:masterfrom
david-yz-liu merged 3 commits intopyta-uoft:masterfrom
Conversation
Collaborator
Pull Request Test Coverage Report for Build 22266478072Details
💛 - Coveralls |
| @only_required_for_messages("invalid-precondition-syntax") | ||
| @only_required_for_messages("invalid-precondition-syntax", "invalid-postcondition-syntax") | ||
| def visit_functiondef(self, node: nodes.FunctionDef) -> None: | ||
| """Visit function definition and check preconditions in docstring.""" |
Contributor
There was a problem hiding this comment.
Make sure to update this docstring as well
| postconditions = parse_assertions(node, parse_token="Postcondition") | ||
| for postcondition in postconditions: | ||
| cleaned_condition = re.sub(r"\s+", " ", postcondition) | ||
| parseable_condition = re.sub(r"\$return_value", "_return_value", cleaned_condition) |
Contributor
There was a problem hiding this comment.
The string $return_value should be defined in a constant somewhere in the python_ta.contracts module; please look there and use the constant
| cleaned_condition = re.sub(r"\s+", " ", condition) | ||
| for precondition in preconditions: | ||
| cleaned_condition = re.sub(r"\s+", " ", precondition) | ||
| try: |
Contributor
There was a problem hiding this comment.
rename cleaned_condition as well (in general prefer not reusing the same variable name across different contexts)
Contributor
Author
|
Implemented, also decided to use the string replace() method to substitute the string as it felt more intuitive |
david-yz-liu
approved these changes
Feb 22, 2026
Contributor
david-yz-liu
left a comment
There was a problem hiding this comment.
Nice work, @PraneethS42!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed Changes
This pull request works on #1302. Adds to the checker
contract_checker.pyby adding logic to alert the user if a function docstring contains invalid syntax within one of its postconditions. This also leverages the pre-existingparse_assertionsmethod frompython-ta/contracts/__init__.py.Adds an example file and appropriate changes in documentation, as well as tests inside the same
test_contract_checker.pyfile, but within a different class....
Screenshots of your changes (if applicable)
Type of Change
(Write an
Xor a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]into a[x]in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)