Skip to content

# type: ignore[code] under --strict in multi-line statements is inconsistent with Python's logical line concept #20471

@nocoding03

Description

@nocoding03

Bug Report

I understand that according to the current documentation and implementation, # type: ignore might be designed to work on a physical line. However, this leads to counterintuitive results.The behavior of # type: ignore[code] comments is inconsistent when applied to a multi-line logical statement (a single logical line spread across multiple physical lines due to parentheses). The outcome of the type check depends on the physical formatting of the code, even though the semantic meaning is identical.

To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=f7e411039bf2f5ec0d4062a08fe23f17&flags=strict

def untyped(x):# type: ignore[no-untyped-def]
    return x
result_a = untyped(untyped(42))  # type: ignore[no-untyped-call]

https://mypy-play.net/?mypy=latest&python=3.12&gist=f81313be96f015a58d3aac52f0dfcfb9&flags=strict

def untyped(x):# type: ignore[no-untyped-def]
    return x
result_b = untyped(
untyped(42)  # type: ignore[no-untyped-call]  
)

https://mypy-play.net/?mypy=latest&python=3.12&gist=ba8cfebd99634a80de0c3cb00c35b550&flags=strict

def untyped(x):# type: ignore[no-untyped-def]
    return x
result_c = untyped(
untyped(42)
)  # type: ignore[no-untyped-call]

Expected Behavior
a b c all success

Actual Behavior
a c success
b: error: Call to untyped function "untyped" in typed context [no-untyped-call]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:1.19.1
  • Mypy command-line flags:strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used:3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions