Return types for Overloads with "Optional[TypeVar]" arguments are deducted as just "object"
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug Report
Please see the following mypy playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=207fe333eb8e226d36b84bf34be5ca65
I personally encountered this on mypy 1.7.1 and Python 3.8, but the mypy playground link uses the latest version of everything.
Consider the following helper function:
import datetime
from typing import Any, Optional, Sized, Tuple, Type, TypeVar, Union
from typing_extensions import Literal, overload, TypeAlias
_T = TypeVar("_T")
class TypeGuardError(Exception):
pass
_THasLen = TypeVar("_THasLen", bound=Sized)
@overload
def required(
value: Optional[_T],
*,
message: Optional[str] = None,
property_name: Optional[str] = None,
) -> _T:
pass
@overload
def required(
value: Any,
*,
instance_of: Type[_T],
instance_of_message: Optional[str] = None,
message: Optional[str] = None,
property_name: Optional[str] = None,
) -> _T:
pass
def required(
value: Optional[Any],
*,
instance_of: Optional[Any] = None,
message: Optional[str] = None,
property_name: Optional[str] = None,
instance_of_message: Optional[str] = None,
) -> Any:
if property_name is not None:
message_prefix = f"{property_name}: "
else:
message_prefix = ""
if value is None:
raise TypeGuardError(message or f"{message_prefix}Value cannot be `None`.")
if instance_of and not isinstance(value, instance_of):
raise TypeGuardError(
instance_of_message
or message
or f'{message_prefix}Expected value of type "{instance_of.__name__}", got value of type "{type(value).__name__}" instead.'
)
return value
required here just checks if the value is not None, optionally verifying its type as well. If any validation fails, it's able to generate an error, with either a message you define, or a parameterizable auto-generated one.
The returned value is just the input value, but cast to its "pure type" through overloading (removing None from the type by type-matching it to Optional). But when passing the instance_of argument, the return type is whatever type you pass to that argument instead. That's the intention.
However, mypy fails my expectations. For example:
TimeDeltaLike: TypeAlias = Union[int, float, str, datetime.timedelta]
default_lock_time: Optional[TimeDeltaLike] = None
locked_for = required(
default_lock_time,
message=f"Entity does not specify a default lock time.",
)
# QUESTION: Why is the revealed type `object`?
reveal_type(locked_for)
In the last line, the revealed type is object.
When analyzing the same code through Pyright, the revealed type is int | float | str | datetime.timedelta, as I would expect.
To Reproduce
# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)
See explanation above.
Expected Behavior
See explanation above.
Actual Behavior
See explanation above.
Your Environment
- Mypy version used: 1.7.1
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.8
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
Comienza con el mypy playground enlazado y las sobrecargas required usando Optional[TypeVar]; inspecciona cómo se infiere la llamada con default_lock_time antes de reveal_type. El trabajo estará terminado cuando el tipo de retorno revelado sea int | float | str | datetime.timedelta en lugar de object, conservando el comportamiento de las sobrecargas instance_of.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100