python / python/mypy

TypeForm does not overlap with classes that represent special forms at runtime.

Offen
#21,048 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

A value annotated as a TypeForm is not considered to overlap with various clases used to represent special forms at runtime.

I have tested typing.NewType, types.GenericAlias, and types.UnionType. [^1]

Given a variable defined as x: TypeForm[object] when using isinstance(x, typing.NewType) (or any of the above types), the truthy branch that follows is considered unreachable.

This does not happen with isinstance(x, type) though.

To Reproduce

from typing_extensions import TypeForm
import typing
import types

Y = typing.NewType("Y", int)

x1: TypeForm[object] = Y  # no issue, `Y` is a valid type expression

if isinstance(x1, typing.NewType):
    print("new type") # error: Statement is unreachable  [unreachable]
    
x2: TypeForm[object] = list[int]  # no issue, `list[int]` is a valid type expression

if isinstance(x2, types.GenericAlias):
    print("generic alias") # error: Statement is unreachable  [unreachable]
    
x3: TypeForm[object] = int | str  # no issue, `int | str` is a valid type expression

if isinstance(x3, types.UnionType):
    print("generic alias") # error: Statement is unreachable  [unreachable]
    
x4: TypeForm[object] = int  # no issue, `int` is a valid type expression

if isinstance(x4, type):
    print("generic alias") # no issue, code is reachable

https://mypy-play.net/?mypy=master&python=3.14&flags=warn-unreachable&gist=ea667f1dabb249a088448f23252c92c1

Expected Behavior

The code within the branches to be considered reachable.

Actual Behavior

main.py:10: error: Statement is unreachable  [unreachable]
main.py:15: error: Statement is unreachable  [unreachable]
main.py:20: error: Statement is unreachable  [unreachable]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.19.1 and main branch
  • Mypy command-line flags: --enable-incomplete-feature=TypeForm --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 1.14

[^1]: I think think is all the publicly documented classes?

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

Der Bericht nennt weder eine Quelldatei noch einen vorhandenen Test. Führe zunächst den mitgelieferten Reproducer mit --enable-incomplete-feature=TypeForm und --warn-unreachable aus und verfolge anschließend die Behandlung von Überschneidungen bei TypeForm für typing.NewType, types.GenericAlias und types.UnionType. Erledigt ist die Aufgabe, wenn diese Zweige als erreichbar betrachtet werden und ein Regressionstest die Fälle abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

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