python / python/mypy

Mypy does not detect redundant keyword arguments in 'str.format()'

Open
#11,336 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

To Reproduce
DOES NOT DETECT

from typing import Final

spam: Final[str] = "spam: {eggs}"
spam_eggs = spam.format(eggs="EGGS", foo="bar")

DOES NOT DETECT

from typing import Final

spam: str = "spam: {eggs}"
spam_eggs = spam.format(eggs="EGGS", foo="bar")

DOES NOT DETECT

spam = "spam: {eggs}"
spam_eggs = spam.format(eggs="EGGS", foo="bar")

DETECTS error: Not all arguments converted during string formatting [str-format]

from typing import Final

spam: Final = "spam: {eggs}"
spam_eggs = spam.format(eggs="EGGS", foo="bar")

Expected Behavior

Always detect redundant keys in the 'format()' and raise an appropriate error.

Actual Behavior

Redundant keys in the 'format()' are detected only when I use Final and only when the Final is without arguments.

Your Environment

  • Mypy version used: 0.910
  • Python version used: 3.9
  • Operating system and version: Lubuntu 20.04

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 running the four examples from the issue with mypy 0.910 and compare which annotations change the result. Trace the string-format checking entry point to determine why redundant keyword arguments are recognized only for an unparameterized Final. Done means the expected error is reported consistently for all shown examples.

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.