inline configurations (mypy comments) in strings still work
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
I take it the mypy inline configuration comments are supposed to work like regular hot comments, and not just activate whenever they appear as characters in the source file, such as in a string. However, as I discovered while investigating https://github.com/python/mypy/pull/17875#issuecomment-2939335586, they aren't actually parsed according to the Python syntax rules (because this would be slower and more complicated, at least given our current implementation) and therefore they do work if they're in a string, provided they are at the start of a line.
For example, in the following code, untyped defs are disallowed and an error is raised:
"""
This is just a silly little multi-line string literal, surely it couldn't—
# mypy: disallow-untyped-defs
"""
def foo():
return 1
Your Environment
- Mypy version used: this was probably 1.15, but I'd be surprised if it's been fixed by 1.17 (current as of time of writing)
Related
This, #12358, and https://github.com/python/mypy/issues/19366 form a loose family of "things we could easily fix if we had a parser that told us about comments to begin with" ie if https://github.com/python/mypy/pull/17875#issuecomment-2587759851 ¶2 or https://github.com/python/cpython/issues/101494#issuecomment-2395119743 were implemented.
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 with the reproducer in the issue and read the linked inline-configuration documentation, related issue #12358, issue #19366, and the referenced pull request and CPython discussion. Done means a mypy configuration comment inside a string is ignored while the same comment in actual source comments continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100