[Python-checkins] bpo-35733: Make isinstance(ast.Constant(boolean), ast.Num) be false. (GH-11547)

Serhiy Storchaka webhook-mailer at python.org
Fri Jan 18 14:30:31 EST 2019


https://github.com/python/cpython/commit/74176226179ed56ad1c910bec5c4100e72ab4e84
commit: 74176226179ed56ad1c910bec5c4100e72ab4e84
branch: master
author: Anthony Sottile <asottile at umich.edu>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2019-01-18T21:30:28+02:00
summary:

bpo-35733: Make isinstance(ast.Constant(boolean), ast.Num) be false. (GH-11547)

files:
A Misc/NEWS.d/next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst
M Lib/ast.py
M Lib/test/test_ast.py

diff --git a/Lib/ast.py b/Lib/ast.py
index 4c8c7795ff82..03b8a1b16b7a 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -346,7 +346,10 @@ def __instancecheck__(cls, inst):
             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 @@ def __new__(cls, *args, **kwargs):
     NameConstant: (type(None), bool),
     Ellipsis: (type(...),),
 }
+_const_types_not = {
+    Num: (bool,),
+}
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 897e705a42ca..4bf77ff046e1 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -411,12 +411,16 @@ def test_isinstance(self):
         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
index 000000000000..8e5ef9b84178
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst
@@ -0,0 +1,2 @@
+``ast.Constant(boolean)`` no longer an instance of :class:`ast.Num`.  Patch by Anthony
+Sottile.



More information about the Python-checkins mailing list