Introduce a Not type
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 1.8k
- Forks
- 302
- Merge moyen
- 23 h
- PR mergées (30 j)
- 8
Description
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.
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Lisez d’abord la discussion Intersection liée et les exemples sized.py et datetime.py de phantom-types. Examinez ensuite le comportement de mypy présenté dans l’issue et comparez la manière dont les autres vérificateurs de types gèrent les intersections implicites. Le travail est terminé lorsqu’un design approuvé par un maintainer pour exprimer des types disjoints et un comportement convenu des vérificateurs ont été définis.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- devtools
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100