python / python/mypy

mypy does not catch string formatting errors in `logging.log` that are listed - not interpolated

Open
#18,794 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I could not find the same issue previously reported here, but these concepts are so generic I would not be surprised user-error is the cause of my failed earch.

Bug Report

In the following script, mypy correctly identifies a string formatting error in case B, which uses print

Cases E and F will both raise exceptions and are not caught.

To Reproduce

import logging
log = logging.getLogger(__name__)

a = "1"
b = "2"

print("print.passes A (%s) %s" % (a, b))
try:
    print("print.fail B [mypy catches] (%s) %s" % (a, ))
except TypeError:
    pass

log.critical("log.passes C (%s) %s" % (a, b))
log.critical("log.passes D (%s) %s" , a, b)
log.critical("log.fail E (%s) %s" , (a, b))
log.critical("log.fail F (%s) %s" , a)

Expected Behavior

I expected mypy to catch the string formatting error here.

I understand logging.log works differently and delays the interpolation. it would be nice if mypy could catch the number of arguments passed to logging to hint at a possible error or pass

Actual Behavior

mypy does not catch this

Your Environment

mypy-1.15.0

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 by reproducing the reported cases with mypy-1.15.0, focusing on the logging.log and logger.critical entry points and comparing them with print. Trace how logging calls are analyzed, then add coverage so incorrect formatting argument counts are reported without rejecting valid delayed interpolation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
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.