import random from typing import get_type_hints class Bar: def foo(self, a: str) -> int: return 1 class fakedict(dict): def __getattribute__(self, *args, **kwargs): if random.randint(0,1) == 1: dict.__setitem__(self,'a', int) else: dict.__setitem__(self, 'a', str) return dict.__getattribute__(self, *args, **kwargs) setattr(Bar.foo, '__annotations__', fakedict({'a': str, 'return': int})) print(get_type_hints(Bar.foo)) print(get_type_hints(Bar.foo)) print(get_type_hints(Bar.foo)) print(get_type_hints(Bar.foo)) print(get_type_hints(Bar.foo)) print(get_type_hints(Bar.foo)) print(get_type_hints(Bar.foo))