Skip to content

Commit

Permalink
Add some type tests for defaulted typevar classes
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Jun 19, 2024
1 parent 49bc709 commit 87451f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/trio/_tests/type_tests/raisesgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def check_inheritance_and_assignments() -> None:
assert a


def check_matcher_typevar_default(e: Matcher) -> object:
assert e.exception_type is not None
exc: type[BaseException] = e.exception_type
# this would previously pass, as the type would be `Any`
e.exception_type().blah() # type: ignore
return exc # Silence Pyright unused var warning


def check_basic_contextmanager() -> None:
# One level of Group is correctly translated - except it's a BaseExceptionGroup
# instead of an ExceptionGroup.
Expand Down
29 changes: 29 additions & 0 deletions src/trio/_tests/type_tests/task_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Check that started() can only be called for TaskStatus[None]."""

from trio import TaskStatus
from typing_extensions import assert_type


async def check_status(
none_status_explicit: TaskStatus[None],
none_status_implicit: TaskStatus,
int_status: TaskStatus[int],
) -> None:
assert_type(none_status_explicit, TaskStatus[None])
assert_type(none_status_implicit, TaskStatus[None]) # Default typevar
assert_type(int_status, TaskStatus[int])

# Omitting the parameter is only allowed for None.
none_status_explicit.started()
none_status_implicit.started()
int_status.started() # type: ignore

# Explicit None is allowed.
none_status_explicit.started(None)
none_status_implicit.started(None)
int_status.started(None) # type: ignore

none_status_explicit.started(42) # type: ignore
none_status_implicit.started(42) # type: ignore
int_status.started(42)
int_status.started(True)

0 comments on commit 87451f5

Please sign in to comment.