-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Cannot ignore type errors in multiline strings #9484
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
Comments
I can't repro the code provided passing type checking on any (mypy, python) version:
Two workarounds: you can alias the expression before your f-string or use If there is an issue here, it's probably going to be a wontfix, because mypy gets the location of type-ignores from stdlib's AST in 3.8. |
So you're saying multiline format strings are always going behave this way now? That kinda says that they're just fundamentally incompatible with mypy… that seems… suboptimal, to say the least. :( |
First and foremost, I'm saying I can't repro your issue (that the behaviour of mypy changes here when using different Python versions). |
Sure, and I don't know why that is. But what you're saying is that for you this happens with all versions of Python. That's even worse! I did indeed ultimately have to pull out virtually all the embedded expressions, into separate variables, so I could |
You're right, I probably shouldn't have been so hasty to close. I took a look at the code and there seems to be opportunity to lie about lineno's of JoinedStr values in fastparse. That said, if you find that there is a repro that passes on some (mypy, python_version) combination, that would be great and increases the chance this issue has a happy ending. |
I tried figuring out what's special about my local setup, but wasn't able to - it's a bunch of LinkedIn-specific & -internal stuff, with plenty of complexity, so it's neither all that surprising that the behaviour is different, nor trivial to figure out why. And important follow-up though re. workarounds: I didn't know |
I do see a slight difference between 3.7-, and 3.8+. Up to 3.7, the error points at the |
(The major difference is that when mypy is run using 3.7, the parser is typed_ast, while with 3.8+ it's the stdlib ast module.) |
Just FYI, this is still broken with Python 3.9.x (w/ mypy 0.790 & 0.800, at least). |
This is still broken with Python 3.11.2 (w/ mypy 1.0.1). |
Once PEP 701 (https://peps.python.org/pep-0701) is accepted you should be able to put the comment inside the interpolation. |
Just ran into the same issue, thanks @gvanrossum for pointing out the PEP! Looks like Python 3.12 does indeed allow comments inside of an f-string and mypy picks them up. So, @wadetregaskis-linkedin’s example above now works: stringy = "foo"
f"""{
stringy.nope() # type: ignore
}""" Note, however, the placement of the curly braces! |
(ignore that this is a true positive - my real code is much more complicated and is actually valid, the above is just a contrived example to show the core problem)
In Python 3.7 with mypy 0.782, it reports the error on the closing
"""
(if you remove the# type: ignore
), so it picks up the ignore directive and all is well. In Python 3.8, it reports the error on the previous line. There's no way to add# type: ignore
on that line, and adding it anywhere else has no effect.In other cases it tends to report the error as being on the line with the opening
"""
… I'm not sure why in this particular, contrived example it behaves differently. But either way, it's a major problem, since there's no practical workaround for this [in my much larger, real-world code]. It's blocking the upgrade to Python 3.8 for one of my major projects.(the output from mypy w/ Python 3.8)
The text was updated successfully, but these errors were encountered: