I have a function take any NamedTuple class (not object) as input. here people recommend to use Type[base_class] as hint, but it does not work for NamedTuple:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from typing import Type, NamedTuple
class A:
pass
class B(A):
pass
class C(A):
pass
def process_any_subclass_type_of_A(cls: Type[A]):
if cls == B:
pass
elif cls == C:
pass
process_any_subclass_type_of_A(B) # works fine
class test_t(NamedTuple):
x: str
y: str
def func1(cls: Type[NamedTuple]):
print(cls)
func1(test_t) # mypy show error Argument 1 to "func1" has incompatible type "Type[test_t]"; expected "Type[NamedTuple]"
Isn't the test_t is subclass of NamedTuple? Why A is working but not NamedTuple?
Updates: the NamedTuple is a meta class. Then the question will be how can I type hint a set of class derived from same metaclass?