Consider these two scenarios:
from queue import Queue
my_q: Queue[int] = Queue()
print(f"queue={my_q}")
and
from queue import Queue
class MyClass(object):
def __init__(self):
self.my_q: Queue[int] = Queue()
my_class = MyClass()
print(f"queue={my_class.my_q}")
Running the former (expectedly) throws a TypeError:
$ python3 run.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
my_q: Queue[int] = Queue()
TypeError: 'type' object is not subscriptable
However the latter doesn't, and proceeds to the print statement without issue:
$ python3 run.py
queue=<queue.Queue object at 0x7fb40265f730>
I would expect a TypeError in both cases. Why is there no TypeError when the Queue[int] type is inside of a class's __init__ method?