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

This commit is contained in:
Anthony Sottile 2019-01-18 11:30:28 -08:00 committed by Serhiy Storchaka
parent 39ed289a35
commit 7417622617
3 changed files with 13 additions and 1 deletions

View File

@ -346,7 +346,10 @@ class _ABC(type):
except AttributeError: except AttributeError:
return False return False
else: 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) return type.__instancecheck__(cls, inst)
def _new(cls, *args, **kwargs): def _new(cls, *args, **kwargs):
@ -384,3 +387,6 @@ _const_types = {
NameConstant: (type(None), bool), NameConstant: (type(None), bool),
Ellipsis: (type(...),), Ellipsis: (type(...),),
} }
_const_types_not = {
Num: (bool,),
}

View File

@ -411,12 +411,16 @@ class AST_Tests(unittest.TestCase):
self.assertFalse(isinstance(ast.Str('42'), ast.Bytes)) self.assertFalse(isinstance(ast.Str('42'), ast.Bytes))
self.assertFalse(isinstance(ast.Num(42), ast.NameConstant)) self.assertFalse(isinstance(ast.Num(42), ast.NameConstant))
self.assertFalse(isinstance(ast.Num(42), ast.Ellipsis)) 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.Num))
self.assertFalse(isinstance(ast.Constant(42), ast.Str)) self.assertFalse(isinstance(ast.Constant(42), ast.Str))
self.assertFalse(isinstance(ast.Constant('42'), ast.Bytes)) self.assertFalse(isinstance(ast.Constant('42'), ast.Bytes))
self.assertFalse(isinstance(ast.Constant(42), ast.NameConstant)) self.assertFalse(isinstance(ast.Constant(42), ast.NameConstant))
self.assertFalse(isinstance(ast.Constant(42), ast.Ellipsis)) 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.Num))
self.assertFalse(isinstance(ast.Constant(), ast.Str)) self.assertFalse(isinstance(ast.Constant(), ast.Str))

View File

@ -0,0 +1,2 @@
``ast.Constant(boolean)`` no longer an instance of :class:`ast.Num`. Patch by Anthony
Sottile.