Refer to Python documentation __class_getitem__:
A class can generally only be parameterized if it defines the special class method
__class_getitem__().
And typing.Type:
Deprecated since version 3.9:
builtins.typenow supports[]. See PEP 585 and Generic Alias Type.
This means that after version 3.9, we can use type[class] instead of typing.Type[class], but in pycharm, when I use the former, the IDE tells me:
Class
typeundefined__getitem__, therefore, the[]operator cannot be used on its instance.
And I confirmed this in interactive Python:
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list[int]
list[int]
>>> list.__class_getitem__(int)
list[int]
>>> type[int]
type[int]
>>> type.__class_getitem__(int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'type' has no attribute '__class_getitem__'
>>> type.__getitem__(int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'type' has no attribute '__getitem__'. Did you mean: '__setattr__'?
I noticed that "generally" is marked in italics in the first document, which means that it is not guaranteed to be correct for all types, so it does not mean that the document is wrong here. But I don't understand why Python adopt a special implementation for type (Because it is a metaclass?). The constant warnings of the IDE may make me give up using type and use typing.Type instead.
Update:
From the first document, we can know the reason why type doesn't implement the __getitem__ method:
Usually, the subscription of an object using square brackets will call the
__getitem__()instance method defined on the object’s class. However, if the object being subscribed is itself a class, the class method__class_getitem__()may be called instead.__class_getitem__()should return a GenericAlias object if it is properly defined.
Therefore, type does not implement __getitem__ to avoid the occurrence of types similar to int[str]. However, this is not the reason why type doesn't implement __class_getitem__. Even if it has this implementation, according to the document description, it will not affect the instance of type.