Skip to content

Commit

Permalink
Add tests for empty protocols on built-in objects.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thetorpedodog committed Sep 22, 2024
1 parent cf25d56 commit e217aba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions tests/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
from . import (
Child,
Employee,
EmptyRuntimeProtocol,
EmptyStaticProtocol,
JSONType,
Parent,
RuntimeProtocol,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e217aba

Please sign in to comment.