You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that inspect.isclass returns False for GenericAlias instances #132467
importinspectfromtypingimportTypeVar, GenericT=TypeVar("T")
classNode(Generic[T]):
x: T# Instance attribute (see below)def__init__(self, label: T|None=None) ->None:
self.label=labelx=Node("") # Inferred type is Node[str]y=Node(0) # Inferred type is Node[int]z=Node() # Inferred type is Node[Any]# Also OK:y=Node[int](0)
print(inspect.isclass(Node)) # Trueprint(inspect.isclass(Node[int])) # False (shouldn't this be True?)print(inspect.isclass(Node[str])) # False (shouldn't this be True?)
In the example above, shouldn't calls such as inspect.isclass(Node[int]) return True, since they can be instantiated and used like normal classes?
@sobolevn: I also agree that changing anything in inspect.is* can break a lot of code. And it is easier to adapt any checks in your own code: inspect.isclass(obj) or isinstance(obj, GenericAlias) - for example.
isinstance(Node[int], GenericAlias) is also returning False:
# Definition of generic Node is same>>>inspect.isclass(Node[int])
False# ok>>>fromtypesimportGenericAlias>>>inspect.isclass(Node[int]) orisinstance(Node[int], GenericAlias)
False# ?>>>isinstance(Node[int], GenericAlias)
False# ?
Version: Python 3.14.0a7+, is that expected behaviour?
I see that, there was discussion about types.GenericAlias VS typing._GenericAlias. As I understand this is documented in code, should we document in online docs ?
Activity
AbduazizZiyodov commentedon Apr 13, 2025
@sobolevn Can you assign me to this issue ? I will work on it
sobolevn commentedon Apr 13, 2025
@AbduazizZiyodov sure, please go ahead :)
AbduazizZiyodov commentedon May 3, 2025
isinstance(Node[int], GenericAlias)is also returningFalse:Version:
Python 3.14.0a7+, is that expected behaviour?AbduazizZiyodov commentedon May 3, 2025
But this behaves as expected:
I see that, there was discussion about
types.GenericAliasVStyping._GenericAlias. As I understand this is documented in code, should we document in online docs ?AbduazizZiyodov commentedon May 6, 2025
PRs #133504 , python/typing#1993
gh-132467: Document and test that generic aliases are not classes (#1…
[3.15] gh-132467: Document and test that generic aliases are not clas…
[3.14] pythongh-132467: Document and test that generic aliases are no…
[3.13] gh-132467: Document and test that generic aliases are not clas…
[3.14] gh-132467: Document and test that generic aliases are not clas…
pythongh-132467: Document and test that generic aliases are not class…
Merge pull request #1 from rohitjain25/clarify-generic-alias-isclass-…