python / python/mypy

Union type narrowing behaves differently depending on argument position

Aperta
#16,401 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-join-v-union topic-type-context
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

In the following example the type narrowing of the union types works as expected if it appears in the first or second argument position, but breaks if it appears in position three (or later).

This issue probably falls into the topic-join-v-union.

To Reproduce

Example on mypy playground

from typing import Callable, TypeVar

T = TypeVar("T")

def func(
    a: Callable[[], T],
    b: Callable[[], T],
    c: Callable[[], T],
) -> T:
    raise NotImplementedError()
        

class Foo:
    ...
    
class Bar:
    ...

def make_foo() -> Foo:
    raise NotImplementedError()
    
def make_bar() -> Bar:
    raise NotImplementedError()
    
def make_foo_or_bar() -> Foo | Bar:
    raise NotImplementedError()

x1: Foo | Bar = func(a=make_foo_or_bar, b=make_foo, c=make_bar)
x2: Foo | Bar = func(a=make_foo, b=make_foo_or_bar, c=make_bar)
x3: Foo | Bar = func(a=make_foo, b=make_bar, c=make_foo_or_bar)

Expected Behavior

The example should type check properly.

Actual Behavior

Inferring the return type works properly for x1 and x2, but it fails for x3 with:

Incompatible types in assignment (expression has type "object", variable has type "Foo | Bar")  [assignment]

Considering that the code is fully valid and symmetric, there should not be a difference in the behavior.

Side note: The issue cannot be work-around at call site by just swapping the order of a and c in the func(...) call. Apparently it has to do with the order in which they appear in the function signature.

Your Environment

  • Mypy version used: 1.6.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10, 3.11

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 con il riproduttore nell’issue ed eseguilo con mypy 1.6.1 o con la versione attuale per confermare la differenza posizionale. Traccia l’inferenza dei tipi degli argomenti callable per func e indaga sul motivo per cui il terzo parametro produce object; il lavoro è completato quando x1, x2 e x3 passano tutti il controllo dei tipi in modo coerente.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.