Skip to content

Commit 6015cc5

Browse files
bpo-32892: Support subclasses of base types in isinstance checks for AST constants. (GH-9934)
Some projects (e.g. Chameleon) create ast.Str containing an instance of the str subclass.
1 parent 913876d commit 6015cc5

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Lib/ast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def __instancecheck__(cls, inst):
346346
except AttributeError:
347347
return False
348348
else:
349-
return type(value) in _const_types[cls]
349+
return isinstance(value, _const_types[cls])
350350
return type.__instancecheck__(cls, inst)
351351

352352
def _new(cls, *args, **kwargs):

Lib/test/test_ast.py

+4
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ def test_isinstance(self):
399399
self.assertFalse(isinstance(ast.Constant(), ast.NameConstant))
400400
self.assertFalse(isinstance(ast.Constant(), ast.Ellipsis))
401401

402+
class S(str): pass
403+
self.assertTrue(isinstance(ast.Constant(S('42')), ast.Str))
404+
self.assertFalse(isinstance(ast.Constant(S('42')), ast.Num))
405+
402406
def test_subclasses(self):
403407
class N(ast.Num):
404408
def __init__(self, *args, **kwargs):

0 commit comments

Comments
 (0)