python / python/mypy

Can't narrow `Sequence[<X>]` to `Tuple[<X>, ...]` using `isinstance(..., tuple)`

Offen
#16,933 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

Using isinstance(<Sequence[X]>, tuple) narrows the type of <Sequence[X]> to Tuple[Any, ...], not Tuple[X, ...] as one would expect.

To Reproduce

[mypy-play.net]
[pyright-play.net[^1]] (pyright handles this as expected)

from __future__ import annotations
from typing_extensions import (
        assert_type,
        Sequence,
        Tuple,
        )

value: Sequence[str]

if isinstance(value, tuple):
    assert_type(value, Tuple[str, ...])
            # expected: (this works)
            # actual: "Expression is of type 'tuple[Any, ...]',
            #          not 'tuple[str, ....]'"

[^1]: the pyright example code is slightly modified (1) to avoid an error around value being uninitialized, and (2) to reflect that pyright behaves as expected

Expected Behavior

if instance(value, ...) should only ever narrow the type of value

Actual Behavior

Mypy narrows the container type, but forgets the element type

Your Environment

  • Mypy version used: 1.8.0; bug exists back to at least 1.0
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.8, 3.12

Existing issues

While it seems extremely unlikely that this issue hasn't been reported, I haven't been able to locate anything obviously related among the ~217 issues that mention both isinstance and tuple. Issue #2456 may be vaguely related.

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

Reproduziere das Beispiel im verlinkten Fall auf mypy-play.net und untersuche, wie mypy isinstance(value, tuple) für Sequence[str] eingrenzt. Vergleiche das Ergebnis mit der assert_type-Erwartung und dem verlinkten pyright-Verhalten; abgeschlossen ist die Aufgabe, wenn der eingegrenzte Wert str-Elemente als Tuple[str, ...] beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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