python / python/typing

Introduce a Not type

Aperta
#801 27 commenti 29 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: feature
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

This is a continuation of this discussion on the Intersection issue.

I have a different use case for the Not[...] type, but it is related to the original discussion. My usecase is to be able to tell the type checker that two types can never have an intersection. The specific case is for the phantom-types library where there are two types NonEmpty and Empty.

Currently there is no way to tell mypy that a variable can't both be NonEmpty and Empty, so this code passes without error:

from __future__ import annotations
from phantom.sized import NonEmpty, Empty

i: tuple[int, ...] = ()

assert isinstance(i, Empty)
assert isinstance(i, NonEmpty)

reveal_type(i)

The output shows that mypy interprets i as an intersection between tuple, Empty and NonEmpty:

intx.py:9: note: Revealed type is 'intx.<subclass of "tuple", "Empty", and "NonEmpty">'

Of course this code would error at runtime, but it would be preferable if this could be expressible so that type checkers can give an error here, similarly to how mypy gives errors for unreachable code.

A suggestion for how this could be expressed is to allow subclassing a Not type, so simplified the definitions of the two mentioned types would like this:

class NonEmpty:
    ...

class Empty(Not[NonEmpty]):
    ...

There are also other types in the library that would benefit from this like TZAware and TZNaive.

Edit: To clarify, I think that this type would be valuable even without an Intersection type which is why I opened a separate issue. This is because (as showed in the example) there already implicit intersections in mypy. I don't know how the other type checkers treats this.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

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

Leggi prima la discussione collegata su Intersection e gli esempi sized.py e datetime.py di phantom-types. Poi esamina il comportamento di mypy mostrato nell’issue e confronta il modo in cui gli altri type checker gestiscono le intersezioni implicite. Il lavoro è completato quando sono stati definiti un design approvato da un maintainer per esprimere tipi disgiunti e un comportamento concordato dei checker.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.