Skip to content

inline configurations (mypy comments) in strings still work #19441

@wyattscarpenter

Description

@wyattscarpenter

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 #17875 (comment), 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 #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 #17875 (comment) ¶2 or python/cpython#101494 (comment) were implemented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-ignore# type: ignore comments

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions