python / python/mypy

Cannot ignore type errors in multiline strings

Open
#9,484 12 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-ignore
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

stringy = "foo"

f"""\
{stringy.nope()}
"""  # type: ignore

(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)

test.py:4: error: "str" has no attribute "nope"
Found 1 error in 1 file (checked 1 source file)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the supplied test.py snippet using Python 3.8 and mypy 0.782, then compare the diagnostic location with Python 3.7. Trace multiline f-string diagnostics and # type: ignore handling; done means the ignore on the closing triple-quoted string suppresses the reported error consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.