python / python/mypy

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

Aperta
#21,048 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-reachability topic-type-form topic-type-narrowing
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Il report non indica alcun file sorgente né alcun test esistente. Inizia eseguendo il reproducer fornito con --enable-incomplete-feature=TypeForm e --warn-unreachable, quindi traccia la gestione delle sovrapposizioni di TypeForm per typing.NewType, types.GenericAlias e types.UnionType. Il lavoro è completato quando quei rami sono considerati raggiungibili e un test di regressione copre i casi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.