Skip to content

Commit 568bf02

Browse files
Explain that GenericAlias objects are not considered to be classes
1 parent 3149e8c commit 568bf02

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

docs/spec/generics.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,17 @@ the runtime class of the objects created by instantiating them doesn't
397397
record the distinction. This behavior is called "type erasure"; it is
398398
common practice in languages with generics (e.g. Java, TypeScript).
399399

400+
Additionally, at the runtime, objects like ``Node[int]`` will not be considered as a class,
401+
even though they behave like them. This is because these objects are instances of ``GenericAlias``::
402+
403+
import inspect
404+
405+
inspect.isclass(Node) # True
406+
inspect.isclass(Node[int]) # False
407+
inspect.isclass(Node[str]) # False
408+
409+
type(Node[int]) # <class 'typing._GenericAlias'>
410+
400411
Using generic classes (parameterized or not) to access attributes will result
401412
in type check failure. Outside the class definition body, a class attribute
402413
cannot be assigned, and can only be looked up by accessing it through a

0 commit comments

Comments
 (0)