]> granicus.if.org Git - python/commitdiff
bpo-32892: Support subclasses of base types in isinstance checks for AST constants...
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 28 Oct 2018 11:43:03 +0000 (13:43 +0200)
committerGitHub <noreply@github.com>
Sun, 28 Oct 2018 11:43:03 +0000 (13:43 +0200)
Some projects (e.g. Chameleon) create ast.Str containing an instance
of the str subclass.

Lib/ast.py
Lib/test/test_ast.py

index de3df1473e5ea218553b67bac58008bfcb4fcfcd..4c8c7795ff822c16322efd5569e9adebf1a1d2db 100644 (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):
index 10be02eee0c03c4bc52cfc8419381ebafe15fb56..4bbdc3b82daa527e4c06e25f336087fba4fac2f4 100644 (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):