python / python/mypy

inline configurations (mypy comments) in strings still work

Open
#19,441 0 comments 0 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

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.