gh-105280: Ensure isinstance([], collections.abc.Mapping) always evaluates to False (#105281)

This commit is contained in:
Alex Waygood 2023-06-05 15:10:49 +01:00 committed by GitHub
parent 058b960535
commit 08756dbba6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 8 deletions

View file

@ -3,6 +3,7 @@ import collections
import collections.abc
from collections import defaultdict
from functools import lru_cache, wraps
import gc
import inspect
import itertools
import pickle
@ -2758,6 +2759,19 @@ class ProtocolTests(BaseTestCase):
with self.assertRaisesRegex(TypeError, only_classes_allowed):
issubclass(1, BadPG)
def test_isinstance_checks_not_at_whim_of_gc(self):
self.addCleanup(gc.enable)
gc.disable()
with self.assertRaisesRegex(
TypeError,
"Protocols can only inherit from other protocols"
):
class Foo(collections.abc.Mapping, Protocol):
pass
self.assertNotIsInstance([], collections.abc.Mapping)
def test_issubclass_and_isinstance_on_Protocol_itself(self):
class C:
def x(self): pass