I have following code
from typing import TypeVar, Type, overload
T = TypeVar('T')
@overload
def foo(bar: Type[T]) -> T:
...
@overload
def foo(bar: Type[T] | None) -> T | None:
...
def foo(bar: Type[T] | None) -> T | None:
# implementation goes here
...
class Bar:
...
bar = foo(Bar)
bar2 = foo(Bar | None) # No overload variant of "foo" matches argument type "UnionType"
How to properly type hint case for bar2
?
I tried some others:
Type[T | None]
, mypy says Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
removing 2nd overload (resulting in only Type[T]
allowed), mypy says No overload variant of "foo" matches argument type "UnionType"
(meaning 2nd overload is incorrect for that case anyways)