Added invalid precondition syntax checker#1300
Added invalid precondition syntax checker#1300david-yz-liu merged 4 commits intopyta-uoft:masterfrom
Conversation
Pull Request Test Coverage Report for Build 22076057401Details
💛 - Coveralls |
david-yz-liu
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
add spaces around the //
| """ | ||
| ``` | ||
|
|
||
| E(9980)= |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.", |
There was a problem hiding this comment.
Similar to my comment in the documentation, please say "invalid Python expression syntax" here
packages/python-ta/CHANGELOG.md
Outdated
|
|
||
| ### 💫 New checkers | ||
|
|
||
| - `invalid_precondition_syntax_checker`: Added new checker that checks if a function contains invalid syntax within its precondition statements. |
There was a problem hiding this comment.
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
david-yz-liu
left a comment
There was a problem hiding this comment.
Nice work, @PraneethS42!
Proposed Changes
This pull request works on #1294. Creates a custom checker called
invalid_precondition_syntax_checker.pythat 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 preexistingparse_assertionsmethod frompython-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
Xor a brief description next to the type or types that best describe your changes.)Checklist
Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)