From 74176226179ed56ad1c910bec5c4100e72ab4e84 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 18 Jan 2019 11:30:28 -0800 Subject: [PATCH] bpo-35733: Make isinstance(ast.Constant(boolean), ast.Num) be false. (GH-11547) --- Lib/ast.py | 8 +++++++- Lib/test/test_ast.py | 4 ++++ .../next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-01-13-18-42-41.bpo-35733.eFfLiv.rst diff --git a/Lib/ast.py b/Lib/ast.py index 4c8c7795ff..03b8a1b16b 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -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,), +} diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 897e705a42..4bf77ff046 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -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 index 0000000000..8e5ef9b841 --- /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. -- 2.40.0