python / python/mypy

Inconsistent optional type resolution for AbstractSet and Set

Aperta
#16,964 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

Inconsistent optional type resolution for AbstractSet and Set.

To Reproduce

from typing import AbstractSet, Set


def check(x: int) -> None: ...


def foo(x: int | None, s: AbstractSet[int]) -> None:
    if x in s:
        check(x)  # error: Argument 1 to "check" has incompatible type "int | None"; expected "int"  [arg-type]


def bar(x: int | None, s: Set[int]) -> None:
    if x in s:
        check(x)  # no error

Expected Behavior

The resolved type after x in AbstractSet and Set should be the same.

If it needs to be consistent with other type unions, the additional type resolution should not happen:

(this is how it currently works for int | str, no additional type resolution happens)

from typing import AbstractSet, Set


def check(x: int) -> None: ...

def foo(x: int | str, s: AbstractSet[int]) -> None:
    if x in s:
        check(x)  # error


def bar(x: int | str, s: Set[int]) -> None:
    if x in s:
        check(x)  # error

Actual Behavior

In a branch where x of Optional[T] is in Set[T], x's type is resolved into T, however that does not happen with AbstractSet.

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: with and without --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

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 il riproduttore Python fornito con mypy 1.8.0 e confronta i casi AbstractSet e Set in foo e bar. Traccia il restringimento del tipo eseguito per x in ciascuna raccolta e aggiungi la copertura per il comportamento segnalato. Il lavoro è completato quando entrambi i tipi di raccolta risolvono il valore opzionale in modo coerente con il comportamento previsto scelto.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.