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
str counts as a Callable[..., str], but not as a Callable[..., TypeVar] #16867
I ran into what I think is a bug while trying to write a config retrieval library for a Flask server. I'd like to pass a return_type argument into my get_config function. When the argument is provided, I'd like the function to return a value as that type.
To Reproduce
Here's a pared-down version of my function that still causes MyPy to error out. Pyright seems to think this code is valid.
fromtypingimportCallable, TypeVarValueType=TypeVar("ValueType")
defget_config(return_type: Callable[..., ValueType] =str) ->ValueType:
returnreturn_type(42)
# mypy get_config_typed.py# error: Incompatible default for argument "return_type" (default has type "type[str]", argument has type "Callable[..., ValueType]") [assignment]
The same code works fine when ValueType is replaced with str:
fromtypingimportCallabledefget_config(return_type: Callable[..., str] =str) ->str:
returnreturn_type(42)
# mypy get_config_typed.py# Success: no issues found in 1 source file
Actual Behavior
error: Incompatible default for argument "return_type" (default has type "type[str]", argument has type "Callable[..., ValueType]") [assignment]
Your Environment
% mypy --version
mypy 1.8.0 (compiled: yes)
% python --version
Python 3.9.17
% uname -a
Darwin <...> 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:54:51 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6030 arm64
Mypy version used: 1.8.0
Mypy command-line flags: N/A
Mypy configuration options from mypy.ini (and other config files): N/A
Activity
picnixz commentedon Feb 4, 2024
One workaround is to use overloads.