-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Malformed type comment halts checking #15678
Comments
Option to strip type comments with syntax errors before parsing source, that would otherwise halt type checking. TODO: Add notes to the report for stripped out comments.
TODO: Figure out how to construct a unit test if the note message includes the line text.
Don't decode bytes sources to str, which avoids having to determine the encoding, which may not be UTF-8. Ensure errors object is not None before using it.
Reviewing #15686, it appears that:
One option is to do #15686 - trying to strip comments and re-parse. Beyond the ostensible perf issue (which, I agree with OP, likely isn't an issue), it's a bit crude to regex out Python code, e.g. how do you avoid matching Another option is to add
I'm a bit more inclined to the latter option, since it's simpler and doesn't involve "regex code editing". We could raise the issue with cPython / ast to allow more error recovery at least in some cases. |
I agree that #15686's approach of stripping only type comments with syntax errors isn't great. It risks creating a confusing situation when a comment that isn't intended as a type comment happens to parse to a valid AST. Instead we should provide an option that disables type comments completely (while still supporting |
Stripping out type comments that trigger syntax errors seemed to be a more incremental approach than ignoring type (except type ignore) comments altogether. And while I agree that regexs aren't great for parsing in general, the approach taken here I think makes them an appropriate tool to use in this situation, because the regex is used in conjunction with the AST parser. Before a line is stripped, it has to fail the parser first. Because of this, I don't think |
@JelleZijlstra wrote
(What did we do before the Python 3.7 cutoff to parse type comments? The I'm not sure whether
So let's assume print("# type: bad') a syntax error, which you'd try to fix into print(" which would still result in a syntax error. At that point you wouldn't want to say something like:
since that'll be misleading, so you'd have to be careful to keep the old SyntaxError around, and raise that instead. I guess that'd doable, but even more complex. What I'm wondering is whether introducing an option to parse a module with
|
We used |
Add unit tests for edges cases that are syntax errors not due to type comments.
The reparse function could be expanded upon to tweak the source and parse it again to handle any number of exceptions bases on options. This formulation also preserves the original exception and raises it if the source can't eventually be successfully parsed.
Feature
Continue type checking upon encountering a malformed type comment.
Pitch
I work with a very large code base with parts that I import from, but can't change. I'd like to use mypy to type check my code without skipping imports. Unfortunately, some imports have malformed type comments that cause mypy to halt type checking.
ignore_errors
for the modules with the malformed comments does not seem to work. I think this is because these comments are syntax errors to the ast parser.I have come up with a workaround, where at the parse step, if we get a syntax error due to one of these comments, we strip it out of the source file and parse it again.
Even though these are in fact syntax errors, the code will still run, because at the end of the day, they are just comments. So I think stripping them out is valid, provided this feature is put behind an option. I've never worked on mypy before, and this will be my first pull request (for open source, as well as mypy), so if someone could point me in the right direction to do the option flag, that would be much appreciated. I'm also having trouble displaying a note message, so any help there would be great too.
Here is an example to reproduce the issue. If you run mypy on this module, mypy will halt, and not report anything other than the syntax error.
The text was updated successfully, but these errors were encountered: