python / python/mypy

False-positive type errors when passing **kwargs to a function that has other keyword args

Offen
#18,481 8 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

needs discussion
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

If a function signature contains one or more explicit keyword parameters followed by a **kwargs parameter, and the function is called on kwargs only, then mypy generates false positive type errors.

The number of errors generated is unbounded and appears to be equal to the number of distinct types found among the explicit keyword parameters. In addition, the errors are mutually inconsistent with one another (see example below).

To Reproduce

from typing import Any, Optional

def example(
	*,
	a: Optional[int] = None,
	b: Optional[float] = None,
	c: Optional[bool] = None,
	d: Optional[list] = None,
	**kwargs
) -> None:
	print(kwargs)

kwargs = {'hello': 'world'}
example(**kwargs)

Expected Behavior

I'd expect this to pass type-checking: it looks perfectly correct to me. All I'm doing is passing kwargs into kwargs.

Actual Behavior

example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[int]"  [arg-type]
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[float]"  [arg-type]
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[bool]"  [arg-type]
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[list[Any]]"  [arg-type]
Found 4 errors in 1 file (checked 1 source file)

In addition to being false positives, these errors are mutually inconsistent: mypy is claiming that an Optional[int] is expected in Argument 1, and also that an Optional[float] is expected in Argument 1, and also that an Optional[bool] is expected in Argument 1, and so on!

If I change the last line to this:

example(a=None, b=None, c=None, d=None, **kwargs)

then the errors go away. Is this really what mypy wants me to do??

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: None
  • Mypy configuration options from pyproject.toml:
allow_redefinition = true
disable_error_code = "no-any-unimported, no-any-return"
disallow_any_unimported = true
check_untyped_defs = true
follow_imports = "silent"
plugins = "pydantic.mypy"
show_error_codes = true
strict_optional = false
warn_return_any = true
warn_unused_ignores = true
  • Python version used: 3.9.20

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Beispiel aus dem Issue mit mypy 1.14.1 zu reproduzieren, und verfolge den Pfad der Prüfung von Schlüsselwortargumenten für explizite Schlüsselwortparameter, auf die **kwargs folgt. Füge einen Regressionstest für den gezeigten Aufruf hinzu und überprüfe, dass er keine falsch-positiven Fehler erzeugt, während Aufrufe mit inkompatiblen expliziten Schlüsselwörtern weiterhin geprüft werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.