python / python/mypy

`bool` is available even if `*constraints` of `TypeVar` is the exact types `int` and `float`

Aperta
#20,314 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

*Memo:

  • mypy --strict test.py
  • mypy 1.18.2
  • Python 3.14.0
  • Windows 11

The doc says 'Must be exactly str or bytes' about *constraints to make difference between bound and *constraints as shown below:

*Memo:

  • From my understanding:
    • Setting bound, the type and the subtypes are available.
    • Setting *constraints, only the exact types are available but not the subtypes.

T = TypeVar('T') # Can be anything
S = TypeVar('S', bound=str) # Can be any subtype of str
A = TypeVar('A', str, bytes) # Must be exactly str or bytes

But bool is available even if *constraints is the exact types int and float as shown below:

<With TypeVar (Old syntax)>:
from typing import TypeVar, Generic

T = TypeVar('T', int, float)

class MyCls(Generic[T]):
    num: T

    def __init__(self, x: T) -> None:
        self.num = x

           # ↓↓↓↓↓↓↓↓↓↓↓↓
mycls = MyCls[bool](True)
# No error
<Without TypeVar (New syntax)>:
class MyCls[T: (int, float)]:
    num: T

    def __init__(self, x: T) -> None:
        self.num = x

           # ↓↓↓↓↓↓↓↓↓↓↓↓
mycls = MyCls[bool](True)
# No error

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 mypy --strict test.py con mypy 1.18.2 e confronta la sintassi vecchia e nuova di TypeVar. Traccia il comportamento del controllo dei tipi per i TypeVars vincolati di int/float; il lavoro è completato quando l'implementazione applica in modo coerente la semantica documentata dei vincoli e la copertura delle regressioni acquisisce il risultato.

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à
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.