python / python/mypy

Inconsistent optional type resolution for AbstractSet and Set

Offen
#16,964 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

Inconsistent optional type resolution for AbstractSet and Set.

To Reproduce

from typing import AbstractSet, Set


def check(x: int) -> None: ...


def foo(x: int | None, s: AbstractSet[int]) -> None:
    if x in s:
        check(x)  # error: Argument 1 to "check" has incompatible type "int | None"; expected "int"  [arg-type]


def bar(x: int | None, s: Set[int]) -> None:
    if x in s:
        check(x)  # no error

Expected Behavior

The resolved type after x in AbstractSet and Set should be the same.

If it needs to be consistent with other type unions, the additional type resolution should not happen:

(this is how it currently works for int | str, no additional type resolution happens)

from typing import AbstractSet, Set


def check(x: int) -> None: ...

def foo(x: int | str, s: AbstractSet[int]) -> None:
    if x in s:
        check(x)  # error


def bar(x: int | str, s: Set[int]) -> None:
    if x in s:
        check(x)  # error

Actual Behavior

In a branch where x of Optional[T] is in Set[T], x's type is resolved into T, however that does not happen with AbstractSet.

Your Environment

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

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, den bereitgestellten Python-Reproducer mit mypy 1.8.0 auszuführen, und vergleiche die Fälle AbstractSet und Set in foo und bar. Verfolge die für x in jeder Sammlung durchgeführte Typverengung und ergänze eine Abdeckung für das gemeldete Verhalten. Fertig ist die Aufgabe, wenn beide Sammlungstypen den optionalen Wert entsprechend dem gewählten erwarteten Verhalten konsistent auflösen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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