[red-knot] Switch to a handwritten parser for mdtest error assertions#16422
[red-knot] Switch to a handwritten parser for mdtest error assertions#16422AlexWaygood merged 8 commits intomainfrom
Conversation
|
| // This is necessary since we only parse assertions lazily, | ||
| // and sometimes we know before parsing any assertions that an assertion will be unmatched, | ||
| // e.g. if we've exhausted all diagnostics but there are still assertions left. |
There was a problem hiding this comment.
Seems a little odd that you only get assertion-parsing errors if there is at least one diagnostic that could match the assertion, but I think in practice it's fine?
There was a problem hiding this comment.
Would it be hard to force-parse unmatched assertions?
There was a problem hiding this comment.
I don't think it fits neatly into the current architecture. I'm going to defer it for now.
| impl<'a> ErrorAssertion<'a> { | ||
| fn from_str(source: &'a str) -> Result<Self, ErrorAssertionParseError<'a>> { | ||
| ErrorAssertionParser::new(source).parse() | ||
| } | ||
| } |
There was a problem hiding this comment.
This was my first thought too, but unfortunately I don't think it works because of the fact that ErrorAssertion is generic over a lifetime :-( Or at least, I can't seem to make it work
| comment | ||
| .strip_prefix(TYPE_PREFIX) | ||
| .map(Self::Revealed) | ||
| .or_else(|| comment.strip_prefix(ERROR_PREFIX).map(Self::Error)) |
There was a problem hiding this comment.
Not necessarily something for this PR but two common errors that I made in the past:
- I wrote
error[code]:which is wrong - I misspelled
errororrevealed - Too much or not enough whitespace between
#and the pragma keyword`
The second one is probably easy to detect (error on any comment of the form <pragma>: and allowlist legit pragmas (fmt, type, knot)
There was a problem hiding this comment.
The second one is probably easy to detect (error on any comment of the form
<pragma>:and allowlist legit pragmas (fmt,type,knot)
Hah, you would think so... I've just spent a bit of time playing around with it, and it seems surprisingly hard to shoehorn into our current architecture :-(
I'm making the whitespace handling more lenientm though; that's easy to fix
| // This is necessary since we only parse assertions lazily, | ||
| // and sometimes we know before parsing any assertions that an assertion will be unmatched, | ||
| // e.g. if we've exhausted all diagnostics but there are still assertions left. |
There was a problem hiding this comment.
Would it be hard to force-parse unmatched assertions?
e8bf3ad to
9056629
Compare
9056629 to
7826cfd
Compare
* main: [red-knot] Switch to a handwritten parser for mdtest error assertions (#16422) [red-knot] Disallow more invalid type expressions (#16427) Bump version to Ruff 0.9.9 (#16434) Check `LinterSettings::preview` for version-related syntax errors (#16429) Avoid caching files with unsupported syntax errors (#16425) Prioritize "bug" label for changelog sections (#16433) [`flake8-copyright`] Add links to applicable options (`CPY001`) (#16421) Fix string-length limit in documentation for PYI054 (#16432) Show version-related syntax errors in the playground (#16419) Allow passing `ParseOptions` to inline tests (#16357) Bump version to 0.9.8 (#16414) [red-knot] Ignore surrounding whitespace when looking for `<!-- snapshot-diagnostics -->` directives in mdtests (#16380) Notify users for invalid client settings (#16361) Avoid indexing the project if `configurationPreference` is `editorOnly` (#16381)
* dcreager/alist-type: Remove unneeded shear override Update property test CI Move alist into red_knot_python_semantic [red-knot] Switch to a handwritten parser for mdtest error assertions (#16422) [red-knot] Disallow more invalid type expressions (#16427) Bump version to Ruff 0.9.9 (#16434) Check `LinterSettings::preview` for version-related syntax errors (#16429) Avoid caching files with unsupported syntax errors (#16425) Prioritize "bug" label for changelog sections (#16433) [`flake8-copyright`] Add links to applicable options (`CPY001`) (#16421) Fix string-length limit in documentation for PYI054 (#16432) Show version-related syntax errors in the playground (#16419) Allow passing `ParseOptions` to inline tests (#16357) Bump version to 0.9.8 (#16414) [red-knot] Ignore surrounding whitespace when looking for `<!-- snapshot-diagnostics -->` directives in mdtests (#16380) Notify users for invalid client settings (#16361) Avoid indexing the project if `configurationPreference` is `editorOnly` (#16381)
Summary
I keep on making mistakes when writing mdtests that the framework makes hard to debug. The latest iteration here was when I added a test that looked like this while working on #16321:
Notice the mistake there? It took me an annoying amount of time to figure it out. The error in the test is that the asserted error message is missing its closing
"character. But mdtest doesn't tell you that if you apply this diff tomain:Instead it says:
This PR switches our parsing of
# error:assertion comments from regexes to a custom parser. This allows us to explicitly reject malformed assertion messages like this rather than misinterpreting them. Helpful messages are now given to the user in this and many other cases when a malformed assertion message is identified. Assertion messages are also now parsed lazily when matching assertion messages to the diagnostics that are emitted: this allows us to print tailored error messages to the terminal about the malformed assertion messages and recover from them, which would be harder if they were parsed eagerly.Test Plan
I added many unit tests to the
red_knot_testcrate. I also manually checked that if you apply the diff given above, mdtest now produces this error message: