python / python/mypy

Allow ignore to be on any line of multi-line statement

Open
#12,341 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature

Currently, comments can be added to a line to ignore either all errors (# type: ignore), or a specific error code (eg. # type: ignore[call-arg]). When a single statement spans multiple lines, the comment has to be added on the line that mypy "decides" is the one with the error (is it the first line of the expression, rather than first line of the statement?). In some cases, it would make sense to allow the comment to be on a different line of the same statement. I propose allowing it to be on any line of the statement, and therefore to apply to the whole statement.

Pitch
Sometimes it makes more sense for it to be another line, such as when importing something that mypy thinks does not exist, it makes sense to have the ignore on the same line as the attribute that is reported as not existing.

from typing import ( # ignore currently needs to be here
    Union,
    DoesNotExist, # type: ignore[error attr-defined]
)

Sometimes, a formatter like black changes a long line into a multiple line statement. The comment that was at the end of the line is moved to the end of the last line.

def foo(bar:int) -> str: ...
def baz() -> int: ...

a = ( # type: ignore[call-arg]
    "something" 
    if baz() > 0 else 
    foo() # ignore currently needs to be here
)  # formatters (eg. black) often put ignore here

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict%2Cwarn-unused-ignores&gist=e41c530d401ac4f21cb7a21e68fea141

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 two multiline examples and the linked mypy-play reproduction; trace how mypy associates type: ignore comments with statements. Done means an ignore on any line of one multiline statement suppresses that statement's relevant error, including specific codes, without changing behavior for separate statements.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
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.