]> granicus.if.org Git - python/commitdiff
bpo-35733: Make isinstance(ast.Constant(boolean), ast.Num) be false. (GH-11547)
authorAnthony Sottile <asottile@umich.edu>
Fri, 18 Jan 2019 19:30:28 +0000 (11:30 -0800)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 18 Jan 2019 19:30:28 +0000 (21:30 +0200)
Lib/ast.py
Lib/test/test_ast.py
Misc/NEWS.d/next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst [new file with mode: 0644]

index 4c8c7795ff822c16322efd5569e9adebf1a1d2db..03b8a1b16b7a8c43304701a3a80049e85488091c 100644 (file)
@@ -346,7 +346,10 @@ class _ABC(type):
             except AttributeError:
                 return False
             else:
-                return isinstance(value, _const_types[cls])
+                return (
+                    isinstance(value, _const_types[cls]) and
+                    not isinstance(value, _const_types_not.get(cls, ()))
+                )
         return type.__instancecheck__(cls, inst)
 
 def _new(cls, *args, **kwargs):
@@ -384,3 +387,6 @@ _const_types = {
     NameConstant: (type(None), bool),
     Ellipsis: (type(...),),
 }
+_const_types_not = {
+    Num: (bool,),
+}
index 897e705a42ca2066407786ef15ba556ca75e1bb2..4bf77ff046e1a4e7e2f477a9a57e60f48c176a68 100644 (file)
@@ -411,12 +411,16 @@ class AST_Tests(unittest.TestCase):
         self.assertFalse(isinstance(ast.Str('42'), ast.Bytes))
         self.assertFalse(isinstance(ast.Num(42), ast.NameConstant))
         self.assertFalse(isinstance(ast.Num(42), ast.Ellipsis))
+        self.assertFalse(isinstance(ast.NameConstant(True), ast.Num))
+        self.assertFalse(isinstance(ast.NameConstant(False), ast.Num))
 
         self.assertFalse(isinstance(ast.Constant('42'), ast.Num))
         self.assertFalse(isinstance(ast.Constant(42), ast.Str))
         self.assertFalse(isinstance(ast.Constant('42'), ast.Bytes))
         self.assertFalse(isinstance(ast.Constant(42), ast.NameConstant))
         self.assertFalse(isinstance(ast.Constant(42), ast.Ellipsis))
+        self.assertFalse(isinstance(ast.Constant(True), ast.Num))
+        self.assertFalse(isinstance(ast.Constant(False), ast.Num))
 
         self.assertFalse(isinstance(ast.Constant(), ast.Num))
         self.assertFalse(isinstance(ast.Constant(), ast.Str))
diff --git a/Misc/NEWS.d/next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst b/Misc/NEWS.d/next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst
new file mode 100644 (file)
index 0000000..8e5ef9b
--- /dev/null
@@ -0,0 +1,2 @@
+``ast.Constant(boolean)`` no longer an instance of :class:`ast.Num`.  Patch by Anthony
+Sottile.