import typing class Meta(type): def __new__(mcls, name, bases, namespace, **kwargs): cls = super().__new__(mcls, name, bases, namespace, **kwargs) for key, value in namespace.items(): if '__globals__' in dir(value): namespace[key].__globals__[cls.__name__] = cls return cls def test(): class Test(metaclass=Meta): def foo() -> 'Test': ... print(typing.get_type_hints(Test.foo)) class Test2(Test): def bar() -> 'Test2': ... print(typing.get_type_hints(Test2.foo)) print(typing.get_type_hints(Test2.bar)) test()