Skip to content

Added invalid precondition syntax checker#1300

Merged
david-yz-liu merged 4 commits intopyta-uoft:masterfrom
PraneethS42:issue-1294
Feb 17, 2026
Merged

Added invalid precondition syntax checker#1300
david-yz-liu merged 4 commits intopyta-uoft:masterfrom
PraneethS42:issue-1294

Conversation

@PraneethS42
Copy link
Contributor

@PraneethS42 PraneethS42 commented Feb 12, 2026

Proposed Changes

This pull request works on #1294. Creates a custom checker called invalid_precondition_syntax_checker.py that alerts the user if a function docstring contains invalid syntax within one of its preconditions. This currently only supports single-line preconditions, and leverages the preexisting parse_assertions method from python-ta/contracts/__init__.py.

Adds an example file and appropriate changes in documentation, as well as tests.
...

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

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 12, 2026

Pull Request Test Coverage Report for Build 22076057401

Details

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

Totals Coverage Status
Change from base Build 21888778658: 0.1%
Covered Lines: 3440
Relevant Lines: 3824

💛 - Coveralls

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.

Thanks @PraneethS42, this is looking good. I just left a few inline comments.


def demo_function(x: int, y: int) -> int:
"""
Demonstrates e9980_invalid_precondition_syntax
Copy link
Contributor

Choose a reason for hiding this comment

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

Write this summary line on the same line as the opening """

- x !== 0 # error on this line
- y != 0 # no error on this line
"""
return x//y
Copy link
Contributor

Choose a reason for hiding this comment

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

add spaces around the //

"""
```

E(9980)=
Copy link
Contributor

Choose a reason for hiding this comment

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

The E is in the wrong location. The purpose of this line is to create an anchor to this section, which you can test locally by adding #E9980 to the end of the URL when visiting the documentation. You should be taken directly to this location.

```python
def demo_function(x: int, y: int) -> int:
"""
Demonstrates e9980_invalid_precondition_syntax
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remember to update the style in this corrected code block, parallel to the comments I made in the example file

---
```

The first precondition incorrectly attempts to use the inequality operator, which is not valid Python syntax.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be explicit, especially for beginners, write "(!=)" immediately after "inequality operator"

from pylint.checkers.utils import only_required_for_messages
from pylint.lint import PyLinter

from python_ta.contracts import parse_assertions
Copy link
Contributor

Choose a reason for hiding this comment

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

For python_ta imports it's better to use relative imports because this is all part of the same package.

from python_ta.contracts import parse_assertions


class InvalidPreconditionSyntaxChecker(BaseChecker):
Copy link
Contributor

Choose a reason for hiding this comment

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

While it's true that your current task only involves preconditions, in the future this can be extended to other forms of conditions (see #1293).

Please rename this to ContractChecker and update the docstring and name. You don't need to change the name or details of the error message (a single checker can support multiple error messages).


### Invalid syntax in function precondition (E9980)

This error occurs when a function precondition contains invalid Python syntax.
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, please specify "invalid Python expression syntax". I would also add an explanatory sentence saying that valid Python statements (e.g. assignment statements like x = 5) are also flagged, as they aren't Python expressions.

"E9980": (
"Invalid syntax in precondition: %s",
"invalid-precondition-syntax",
"Reported when a precondition contains invalid Python syntax.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to my comment in the documentation, please say "invalid Python expression syntax" here


### 💫 New checkers

- `invalid_precondition_syntax_checker`: Added new checker that checks if a function contains invalid syntax within its precondition statements.
Copy link
Contributor

Choose a reason for hiding this comment

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

The terminology here is a bit misleading, but if you look at past entries, here we actually are documenting the specific errors (by name) and not the checker classes themselves.

Please revise, following the format of past entries

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 736a0c2 into pyta-uoft:master Feb 17, 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