dsa-ou / allowed

Check if a program only uses a subset of the Python language.
https://dsa-ou.github.io/allowed/
BSD 3-Clause "New" or "Revised" License
10 stars 6 forks source link

Ignore individual lines #34

Closed mwermelinger closed 9 months ago

mwermelinger commented 10 months ago

Most linters permit creating exceptions when we know it's OK to break a rule. This is typically done by adding a comment like # noqa: F401 to disable reporting error F401 just for that line. The exact form of the comment depends on the linter.

In the case of allowed, this would be useful when creating an assignment that contains scaffolding code that students aren't required to understand or change. If the scaffolding code contains non-taught constructs, allowed will report them. This may confuse students and makes it harder to distinguish violations in teacher's code and student's code.

Since allowed doesn't have different error codes, a comment like # allowed should be enough, stating that the constructs in that line are allowed.

mwermelinger commented 10 months ago

A construct like try ... except ... should have the comment on the try line and not report a violation for that line and the except line.

mwermelinger commented 9 months ago

I don't know how linters robustly check if the end-of-line comment includes noqa: ...: are comments accessible from the AST? As a first approach, we could just try something like line.rstrip().endswith("# allowed"). If that's true, that line isn't further checked.

Of course, how to allow one construct but not another that occurs in the same line, is a different issue :-)

mwermelinger commented 9 months ago

Actually, low effort: just ~5 LOC. The try/except separation was resolved in previous commit.

mwermelinger commented 9 months ago

Forgot to update docs...