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.
This commit is contained in:
Serhiy Storchaka 2018-10-28 13:43:03 +02:00 committed by GitHub
parent 913876d824
commit 6015cc50bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View File

@ -346,7 +346,7 @@ class _ABC(type):
except AttributeError:
return False
else:
return type(value) in _const_types[cls]
return isinstance(value, _const_types[cls])
return type.__instancecheck__(cls, inst)
def _new(cls, *args, **kwargs):

View File

@ -399,6 +399,10 @@ class AST_Tests(unittest.TestCase):
self.assertFalse(isinstance(ast.Constant(), ast.NameConstant))
self.assertFalse(isinstance(ast.Constant(), ast.Ellipsis))
class S(str): pass
self.assertTrue(isinstance(ast.Constant(S('42')), ast.Str))
self.assertFalse(isinstance(ast.Constant(S('42')), ast.Num))
def test_subclasses(self):
class N(ast.Num):
def __init__(self, *args, **kwargs):