Plugin hook to selectively silence error messages
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Descripción
For developing an Either-like monad that types correctly and also behaves nicely at runtime I currently use the following pattern to independently declare the types for mypy and the runtime implementation
from typing import Generic, TypeVar, TYPE_CHECKING
L = TypeVar('L')
R = TypeVar('R')
if TYPE_CHECKING: # type declarations
class Either(Generic[L, R]): pass
Left = Either[L, None]
Right = Either[None, R]
else: # runtime implementation
class Either(): pass
class Left(Either): pass
class Right(Either): pass
# type checking features
a: Left[int]
b: Either[int, None]
a = b # typechecks
b = a # typechecks
# runtime features
isinstance(Left(), Left) #works at runtime
isinstance(Left(), Either) #works at runtime
During static analysis it is important that Left and Right get treated as aliases for Either so that assignments get typechecked correctly (even though at runtime they are subclasses (and thus subtypes) of Either). Additionally I'd like to be able and use isinstance checks at runtime to determine whether a result is an instance of Left or Right (or more general if something is an instance of Either). This mostly works (assignments typecheck and runtime behavior is as excepted), however mypy (rightfully) complains about the isinstance checks that
Parameterized generics cannot be used with class or instance checks mypy(error)
In cases like these it would be useful if one could tell mypy that a generic class can be used with instance checks. This would also be somewhat similar to how one can declare protocol classes to support runtime instance checks with the @runtime_checkable decorator.
This would also allow user defined generics to behave similar to types from typing like List where
isinstance([1,2,3], List) # typechecks
isinstance([1,2,3], List[int]) # Parameterized generics cannot be ...
vs
isinstance(Left(1), Left) # should typecheck, currently doesn't
isinstance(Left(1), Left[int]) # Parameterized generics cannot be ...
I'd propose that @runtime_checkable marks an Generic accordingly so that in the example it would be
#...
if TYPE_CHECKING: # type declarations
@runtime_checkable
class Either(Generic[L, R]): pass
#...
Addendum
The issue is actually not with Either per se but rather with the type aliases Left and Right. For mypy
isinstance(Left(1), Left) #looks similar to 'isinstance([1,2,3], List)'
actually is
isinstance(Left(1), Either[L, None]) # looks similar to 'isinstance([1,2,3], List[int])'
I still think this would be a useful feature to be able and better support the dynamic nature of Python with the if TYPE_CHECKING pattern.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
No se nombran archivos de implementación ni tests. Empieza rastreando cómo mypy gestiona las comprobaciones de isinstance que involucran alias genéricos y compáralo con el comportamiento existente de List descrito en el issue. Se considerará terminado decidir e implementar el tratamiento previsto para las comprobaciones genéricas sin parametrizar y parametrizadas, con cobertura para los ejemplos Either, Left y Right.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100