Skip to content

Add check for valid function postcondition syntax#1306

Merged
david-yz-liu merged 3 commits intopyta-uoft:masterfrom
PraneethS42:issue-1302
Feb 22, 2026
Merged

Add check for valid function postcondition syntax#1306
david-yz-liu merged 3 commits intopyta-uoft:masterfrom
PraneethS42:issue-1302

Conversation

@PraneethS42
Copy link
Contributor

@PraneethS42 PraneethS42 commented Feb 21, 2026

Proposed Changes

This pull request works on #1302. Adds to the checker contract_checker.py by adding logic to alert the user if a function docstring contains invalid syntax within one of its postconditions. This also leverages the pre-existing parse_assertions method from python-ta/contracts/__init__.py.

Adds an example file and appropriate changes in documentation, as well as tests inside the same test_contract_checker.py file, but within a different class.

...

Screenshots of your changes (if applicable)

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality) x
🐛 Bug fix (non-breaking change that fixes an issue)
♻️ Refactoring (internal change to codebase, without changing functionality)
🚦 Test update (change that only adds or modifies tests)
📚 Documentation update (change that only updates documentation)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

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:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • I have updated the project Changelog (this is required for all changes).
  • If this is my first contribution, I have added myself to the list of contributors.

After opening your pull request:

  • I have verified that the pre-commit.ci checks have passed.
  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)

@coveralls
Copy link
Collaborator

coveralls commented Feb 21, 2026

Pull Request Test Coverage Report for Build 22266478072

Details

  • 13 of 13 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 89.984%

Totals Coverage Status
Change from base Build 22209214024: 0.02%
Covered Lines: 3450
Relevant Lines: 3834

💛 - 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."""
Copy link
Contributor

Choose a reason for hiding this comment

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

rename cleaned_condition as well (in general prefer not reusing the same variable name across different contexts)

@PraneethS42
Copy link
Contributor Author

Implemented, also decided to use the string replace() method to substitute the string as it felt more intuitive

@PraneethS42 PraneethS42 changed the title Issue 1302 Add check for valid function postcondition syntax Feb 22, 2026
Copy link
Contributor

@david-yz-liu david-yz-liu left a comment

Choose a reason for hiding this comment

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

Nice work, @PraneethS42!

@david-yz-liu david-yz-liu merged commit 1a6e739 into pyta-uoft:master Feb 22, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants