Malformed type comment halts checking
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
# library.py
def library_fn():
print("Type checking halts after this line because the comment is a syntax error.") # type: bad
x: int = "1"
return x
if __name__ == "__main__":
res = library_fn()
print(f"You can still run me, even though I have a syntax error. See, the result is {res}")
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running mypy on the issue's library.py example and inspect the parse step where the malformed type comment stops checking. Trace how an option flag and note diagnostic would be configured, then add coverage for continuing past the comment and reporting the later type error; done means the opt-in behavior works without changing the default.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100