From 568bf02b632e412901fb444776e7dd86b4a15aca Mon Sep 17 00:00:00 2001 From: Abduaziz Ziyodov Date: Mon, 5 May 2025 22:39:52 +0500 Subject: [PATCH 1/2] Explain that GenericAlias objects are not considered to be classes --- docs/spec/generics.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/spec/generics.rst b/docs/spec/generics.rst index 65eeea941..d824a05f1 100644 --- a/docs/spec/generics.rst +++ b/docs/spec/generics.rst @@ -397,6 +397,17 @@ the runtime class of the objects created by instantiating them doesn't record the distinction. This behavior is called "type erasure"; it is common practice in languages with generics (e.g. Java, TypeScript). +Additionally, at the runtime, objects like ``Node[int]`` will not be considered as a class, +even though they behave like them. This is because these objects are instances of ``GenericAlias``:: + + import inspect + + inspect.isclass(Node) # True + inspect.isclass(Node[int]) # False + inspect.isclass(Node[str]) # False + + type(Node[int]) # + Using generic classes (parameterized or not) to access attributes will result in type check failure. Outside the class definition body, a class attribute cannot be assigned, and can only be looked up by accessing it through a From 3b20764b642ce835843ff02c518133c0bdece7f6 Mon Sep 17 00:00:00 2001 From: Abduaziz Ziyodov Date: Tue, 6 May 2025 07:17:57 +0500 Subject: [PATCH 2/2] Small explanation(context) for 'how GenericAlias objects behave like classes' --- docs/spec/generics.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/spec/generics.rst b/docs/spec/generics.rst index d824a05f1..52eaed062 100644 --- a/docs/spec/generics.rst +++ b/docs/spec/generics.rst @@ -397,8 +397,8 @@ the runtime class of the objects created by instantiating them doesn't record the distinction. This behavior is called "type erasure"; it is common practice in languages with generics (e.g. Java, TypeScript). -Additionally, at the runtime, objects like ``Node[int]`` will not be considered as a class, -even though they behave like them. This is because these objects are instances of ``GenericAlias``:: +Additionally, objects like ``Node[int]`` will not be considered as a class at the runtime, +even though they behave like them (e.g they can be instantiated). This is because these objects are instances of ``GenericAlias``:: import inspect