You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[1.7] Any in decorated function with TypeVar #16482
This looks different to me, but could maybe be the same as #16481.
aiohttp_admin/security.py:18:1: error: Type of decorated function contains type
"Any" ("_lru_cache_wrapper[TypeAdapter[Any]]") [misc]
def _get_schema(t: Type[_T]) -> TypeAdapter[_T]:
^
aiohttp_admin/security.py:24:5: error: Returning Any from function declared to
return "_T" [no-any-return]
return _get_schema(t).validate_python(value)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 2 errors in 1 file (checked 20 source files)
fromcollections.abcimportHashablefromfunctoolsimportlru_cachefromtypingimportType, TypeVarfrompydanticimportTypeAdapter_T=TypeVar("_T", bound=Hashable)
@lru_cachedef_get_schema(t: Type[_T]) ->TypeAdapter[_T]:
returnTypeAdapter(t)
defcheck(t: Type[_T], value: object) ->_T:
"""Validate value is of static type t."""return_get_schema(t).validate_python(value)
fromcollections.abcimportHashablefromfunctoolsimportlru_cachefromtypingimportType, TypeVar_T=TypeVar("_T", bound=Hashable)
@lru_cachedef_get_schema(t: Type[_T]) ->list[_T]:
return []
defcheck(t: Type[_T], value: object) ->_T:
"""Validate value is of static type t."""return_get_schema(t)[0]
@ilevkivskyi I think you're mistaken. It's replacing my TypeVar with Any, and throwing away the type information for my function's return type. This is a regression in 1.7.
Look specifically at the error output, the first error says TypeAdapter[Any]. The second shows that any time I use _get_schema() it returns Any.
I just realised I forgot to include the arguments, but this reproduces with mypy --strict --disallow-any-decorated.
I understand this is counterintuitive, but taking into account signature of lru_cache this is correct behavior. For example, the inferred type of _get_schema is _lru_cache_wrapper[list[Any]], previously mypy inferred some nonsense type like _lru_cache_wrapper[list[_T]], and this is why this error was absent.
Yep, OK, I just went through typeshed and realised it made sense, but not why it appeared to work in older versions. Seems like it should use ParamSpec or something...
Activity
ilevkivskyi commentedon Nov 13, 2023
lru_cachehas a bunch ofAnys. You can add somereveal_type()s to see what is going on.Dreamsorcerer commentedon Nov 13, 2023
@ilevkivskyi I think you're mistaken. It's replacing my TypeVar with Any, and throwing away the type information for my function's return type. This is a regression in 1.7.
Look specifically at the error output, the first error says
TypeAdapter[Any]. The second shows that any time I use _get_schema() it returnsAny.I just realised I forgot to include the arguments, but this reproduces with
mypy --strict --disallow-any-decorated.ilevkivskyi commentedon Nov 13, 2023
I understand this is counterintuitive, but taking into account signature of
lru_cachethis is correct behavior. For example, the inferred type of_get_schemais_lru_cache_wrapper[list[Any]], previously mypy inferred some nonsense type like_lru_cache_wrapper[list[_T]], and this is why this error was absent.Dreamsorcerer commentedon Nov 13, 2023
Yep, OK, I just went through typeshed and realised it made sense, but not why it appeared to work in older versions. Seems like it should use ParamSpec or something...
Dreamsorcerer commentedon Nov 13, 2023
Ah, that was already tried. If anybody else wants to track the current state of lru_cache, it's in python/typeshed#6347
AlexWaygood commentedon Nov 13, 2023
Yeah, it's unfortunately a pretty difficult problem to fix in typeshed. There have been many attempts.
Dreamsorcerer commentedon Nov 15, 2023
I would note that the discussion around this issue appears to be waiting for some input from the mypy side though:
python/typing#946 (reply in thread)