1

This is my code:

class MyClass:

    def __init__(self, a: str, b: int):
        self.a: str = a
        self.b: int = b

hints = get_type_hints(MyClass())
print(hints)

I want to do something like this:

get_type_hints(MyClass)

and get this output:

{"a": str, "b": int}

I tried using type() but didn't get an optimal solution!

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Monta na
  • 41
  • 5
  • 1
    If you have the type hints already, why not build that object yourself? `hints = {k: v.__name__ for k, v in get_type_hints(MyClass.__init__).items()}` Unless you're working with objects with thousands of variables, isn't this optimal enough? – Random Davis Aug 30 '23 at 22:05
  • The attributes and annotations do not exist on the class, they're only created in `__init__` when it gets instantiated. If you'd put the annotations on the class itself of course… – deceze Aug 31 '23 at 00:40

0 Answers0