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
Overloads with Generics "does not accept all possible arguments" #19053
I have a function whose return type is determined by the type of an input parameter (with a default value). I have attempted to type-hint it with generics. I get "error: Overloaded function implementation does not accept all possible arguments of signature".
To Reproduce
In the toy example below, the function has two return types (original-type or list) depending on two possible types of the force_list input (Force or NoForce). For completeness I have overloaded all 4 permutations of the input and output types.
fromtypingimportTypeVar, assert_type, overloadclassForce: ...
ForceInstance=Force()
classNoForce: ...
NoForceInstance=NoForce()
T=TypeVar("T")
@overloaddeflist_or_orig(x: list[T], force_list: Force= ...) ->list[T]: ...
@overloaddeflist_or_orig(x: list[T], force_list: NoForce= ...) ->list[T]: ...
@overloaddeflist_or_orig(x: T, force_list: Force= ...) ->list[T]: ...
@overloaddeflist_or_orig(x: T, force_list: NoForce= ...) ->T: ...
deflist_or_orig(x: list[T] |T, force_list: Force|NoForce=ForceInstance) ->list[T] |T:
"""Return a list or scalar depending on force_list flag."""ifisinstance(x, list):
returnxelifisinstance(force_list, Force):
return [x]
else:
returnxa_list=list_or_orig(1) # Since default is Force, this will return list[int]assert_type(a_list, list[int])
b_list=list_or_orig(1, ForceInstance)
assert_type(b_list, list[int])
a_int=list_or_orig(1, NoForceInstance) # This will return intassert_type(a_int, int)
Expected Behavior
No errors when running through mypy.
Actual Behavior
main.py:29: error: Overloaded function implementation does not accept all possible arguments of signature 1 [misc]
main.py:29: error: Overloaded function implementation does not accept all possible arguments of signature 2 [misc]
Found 2 errors in 1 file (checked 1 source file)
Notes
If I replace the generic type T with a specific type, say, int then it's fine.
I appreciate there is an ambiguity between list[T] and T but have included the less-general list[T] first in the overloads.
(this doesn't affect external callers and the body still passes)
New signature is no less specific that OP original def, so it certainly doesn't accept any arguments not accepted by that one, confirming this as a bug.
Activity
sterliakov commentedon May 9, 2025
This is definitely a bug. As a workaround, you can simplify the implementation signature: https://mypy-play.net/?mypy=latest&python=3.12&gist=9b83a8f13349f84b0fa64914e7440065
(this doesn't affect external callers and the body still passes)
New signature is no less specific that OP original def, so it certainly doesn't accept any arguments not accepted by that one, confirming this as a bug.