python / python/mypy

`TypeIs` narrowing of union type in a generic is not working

Aperta
#20,359 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-typeguard-typeis
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Given an object of type int | bytes and a TypeIs function that narrows int | str | bytes into int | str, mypy correctly infers that the original object is really just int.

However, this doesn't work if the union is a generic parameter. That is, given Sequence[int | bytes] and a narrowing function from Sequence[int | str | bytes] to Sequence[int | str], mypy doesn't infer that the original is Sequence[int]. Instead, it infers that the original is Sequence[int | str].

I think this is a bug? But of course I might just be holding it wrong. Any help will be greatly appreciated.

To Reproduce

from typing import Callable, TypeIs, Sequence, assert_type

def check_plain(value: int | str | bytes) -> TypeIs[int | str]:
    return isinstance(value, int | str)

def check_sequence(seq: Sequence[int | str | bytes]) -> TypeIs[Sequence[int | str]]:
    return all(isinstance(value, int | str) for value in seq)

def works(value: int | bytes) -> None:
    assert check_plain(value)
    assert_type(value, int)

def doesnt(value: Sequence[int | bytes]) -> None:
    assert check_sequence(value)
    assert_type(value, Sequence[int])

https://mypy-play.net/?mypy=1.19.0&python=3.13&gist=100053f110206f3fdf37170ea75a3ca2

Actual Behavior

main.py:15: error: Expression is of type "Sequence[int | str]", not "Sequence[int]"  [assert-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.19.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13

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

Inizia eseguendo la riproduzione fornita con mypy 1.19.0 e confermando l'assert_type che fallisce per il caso generico di Sequence. Traccia la logica di narrowing di TypeIs per i parametri generici; il lavoro è completato quando l'esempio restringe value a Sequence[int] senza l'errore segnalato, mentre il caso semplice continua a funzionare.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.