Skip to content

Added check for valid representation invariant syntax#1334

Merged
david-yz-liu merged 3 commits into
pyta-uoft:masterfrom
KushGandhi3:master
May 8, 2026
Merged

Added check for valid representation invariant syntax#1334
david-yz-liu merged 3 commits into
pyta-uoft:masterfrom
KushGandhi3:master

Conversation

@KushGandhi3
Copy link
Copy Markdown
Contributor

@KushGandhi3 KushGandhi3 commented Apr 23, 2026

Proposed Changes

Adds a new custom checker invalid-representation-invariant-syntax (E9982) that reports when a class's representation invariants in its docstring contain invalid Python expression syntax. Previously, such invariants were silently skipped at runtime by python_ta.contracts._set_invariants, hiding likely typos (e.g. self.x !== 0, self.x = 5) from students.

The new checker mirrors the existing invalid-precondition-syntax (E9980) and invalid-postcondition-syntax (E9981) checks, but runs on class definitions via a new visit_classdef hook in ContractChecker. It reuses the existing parse_assertions helper with parse_token="Representation Invariant", which is the same token used by the runtime, to keep static and runtime behaviour aligned. Each invariant is whitespace-normalized (so multi-line RIs joined with \ are handled) and fed to ast.parse(..., mode="eval"); a SyntaxError emits the new message.

Closes #1303. This is the final sub-issue of #1293 ("Create checkers for contract syntax"); with #1294 and #1302 already merged, the parent issue will be fully addressed once this PR lands.

Screenshots of your changes (if applicable)

Type of Change

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 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.)

@KushGandhi3
Copy link
Copy Markdown
Contributor Author

Hi Professor Liu (@david-yz-liu), could you please approve the workflow run? CI seems to be gated as this is my first contribution to the repo. I'd request a review directly as well, but the option isn't available to non-collaborators. Thanks!

@david-yz-liu david-yz-liu self-requested a review April 29, 2026 22:21
@KushGandhi3
Copy link
Copy Markdown
Contributor Author

Hi Professor @david-yz-liu, I looked into the CI failure and it seems to be a flaky timeout in packages/python-ta/tests/test_reporters/test_html_server.py::test_websocket_message, not an issue caused by my PR.

This test sometimes races between the WebSocket server starting and watchdog fully attaching the file watcher. If the file change happens a little too early, the reload event gets missed and ws.recv() times out. My PR only adds the new invalid-representation-invariant-syntax checker (E9982) plus related tests and docs. It does not change the HTML server, WebSocket logic, watch mode, or dependencies used by this test.

@KushGandhi3
Copy link
Copy Markdown
Contributor Author

Hi Professor @david-yz-liu,

I wanted to follow up on PR #1334. The PR now has a newer head commit, and the latest PythonTA Tests workflow is showing action_required, so the jobs have not started and Coveralls has not produced a current report.

For context, the earlier CI run did execute; the only failure was packages/python-ta/tests/test_reporters/test_html_server.py::test_websocket_message in the Python 3.12/base job, while the rest of the matrix, docs, and prek passed. My PR only adds the E9982 representation-invariant syntax checker plus its tests/docs/example, and does not touch the HTML server, WebSocket/watch-mode code, or dependencies.

Could you please approve the latest workflow run when you have a chance? If the same WebSocket test flakes again, would it be possible to rerun the failed job?

Thank you!

Copy link
Copy Markdown
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 very much, @KushGandhi3! I've been a bit busy and haven't had a chance to review this until now, but you did great work here. 👍

@coveralls
Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 25408942332

Coverage increased (+0.03%) to 90.023%

Details

  • Coverage increased (+0.03%) from the base build.
  • Patch coverage: 11 of 11 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 3879
Covered Lines: 3492
Line Coverage: 90.02%
Coverage Strength: 17.46 hits per line

💛 - Coveralls

@david-yz-liu david-yz-liu merged commit 560c04c into pyta-uoft:master May 8, 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.

Create check for valid representation invariant syntax

3 participants