From e217aba130cb80b4cbec98c491dd1d441d72ab53 Mon Sep 17 00:00:00 2001 From: Paul Fisher Date: Sun, 22 Sep 2024 17:50:33 -0400 Subject: [PATCH] Add tests for empty protocols on built-in objects. One of the symptoms of the earlier problem was that you could not check protocols against built-in types at all. This adds a test specifically for that situation to prevent a regression. --- tests/__init__.py | 9 +++++++++ tests/test_checkers.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/__init__.py b/tests/__init__.py index f28f2c2..848184e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -57,3 +57,12 @@ class RuntimeProtocol(Protocol): member: int def meth(self, x: str) -> None: ... + + +class EmptyStaticProtocol(Protocol): + pass + + +@runtime_checkable +class EmptyRuntimeProtocol(Protocol): + pass diff --git a/tests/test_checkers.py b/tests/test_checkers.py index d9237a9..dc60e03 100644 --- a/tests/test_checkers.py +++ b/tests/test_checkers.py @@ -52,6 +52,8 @@ from . import ( Child, Employee, + EmptyRuntimeProtocol, + EmptyStaticProtocol, JSONType, Parent, RuntimeProtocol, @@ -1193,6 +1195,22 @@ def meth(self, x: str, y: int) -> None: ) +@pytest.mark.parametrize( + "instantiate, annotation", + [ + pytest.param(True, EmptyRuntimeProtocol, id="instance_runtime"), + pytest.param(False, Type[EmptyRuntimeProtocol], id="class_runtime"), + pytest.param(True, EmptyStaticProtocol, id="instance_static"), + pytest.param(False, Type[EmptyStaticProtocol], id="class_static"), + ], +) +@pytest.mark.parametrize("instance_type", [object, str, Parent]) +class TestEmptyProtocol: + def test_empty_protocol(self, instantiate, annotation, instance_type): + subject = instance_type() if instantiate else instance_type + check_type(subject, annotation) + + class TestRecursiveType: def test_valid(self): check_type({"a": [1, 2, 3]}, JSONType)