Skip to content

Commit

Permalink
add type tests for all exported objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Mar 29, 2023
1 parent 9ba894d commit 4edfd3d
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ else
# 'coverage xml' to generate the report that it uses, and that will only
# apply the ignore patterns in the current directory's .coveragerc.
cp ../.coveragerc .

# Until this repository is officially marked with py.typed, we need to temporarily
# create the file so pytest-mypy-plugins will work
if [ ! -f "$INSTALLDIR/py.typed" ]; then
touch "$INSTALLDIR/py.typed"
fi

if pytest -r a --junitxml=../test-results.xml --run-slow ${INSTALLDIR} --cov="$INSTALLDIR" --verbose; then
PASSED=true
else
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ name = "Miscellaneous internal changes"
showcontent = true

[tool.pytest.ini_options]
addopts = ["--strict-markers", "--strict-config"]
addopts = ["--strict-markers", "--strict-config", "--mypy-same-process", "--mypy-only-local-stub"]
xfail_strict = true
faulthandler_timeout = 60
markers = ["redistributors_should_skip: tests that should be skipped by downstream redistributors"]
Expand Down
9 changes: 9 additions & 0 deletions test-requirements.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# For tests
pytest >= 5.0 # for faulthandler in core
pytest-cov >= 2.6.0

# only used on 3.10+, but test-requirements.txt is generated with py3.8
pytest-mypy-plugins ; python_version > "3.7"
# version requirement on top-level package does not flow trough to it's dependencies
# so we list all requirements from pytest-mypy-plugins here.
chevron ; python_version > "3.7"
pyyaml ; python_version > "3.7"
regex ; python_version > "3.7"

async_generator >= 1.9
# ipython 7.x is the last major version supporting Python 3.7
ipython < 7.35 # for the IPython traceback integration tests
Expand Down
17 changes: 15 additions & 2 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ build==0.10.0
# via pip-tools
cffi==1.15.1
# via cryptography
chevron==0.14.0 ; python_version > "3.7"
# via pytest-mypy-plugins
click==8.1.3
# via
# black
Expand All @@ -36,7 +38,9 @@ cryptography==39.0.2
# trustme
# types-pyopenssl
decorator==5.1.1
# via ipython
# via
# ipython
# pytest-mypy-plugins
dill==0.3.6
# via pylint
exceptiongroup==1.1.1 ; python_version < "3.11"
Expand Down Expand Up @@ -68,7 +72,9 @@ mccabe==0.6.1
# flake8
# pylint
mypy==1.1.1 ; implementation_name == "cpython"
# via -r test-requirements.in
# via
# -r test-requirements.in
# pytest-mypy-plugins
mypy-extensions==1.0.0 ; implementation_name == "cpython"
# via
# -r test-requirements.in
Expand Down Expand Up @@ -119,8 +125,15 @@ pytest==7.2.2
# via
# -r test-requirements.in
# pytest-cov
# pytest-mypy-plugins
pytest-cov==4.0.0
# via -r test-requirements.in
pytest-mypy-plugins==1.10.1 ; python_version > "3.7"
# via -r test-requirements.in
pyyaml==6.0 ; python_version > "3.7"
# via pytest-mypy-plugins
regex==2023.3.23 ; python_version > "3.7"
# via pytest-mypy-plugins
sniffio==1.3.0
# via -r test-requirements.in
sortedcontainers==2.4.0
Expand Down
45 changes: 45 additions & 0 deletions trio/tests/typecheck/test_all_public_types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
- case: |-
check_in_yaml
mypy_config: |
ignore_missing_imports = True
main: |-
import trio
reveal_type(trio.MemoryReceiveChannel) # N: Revealed type is "def [ReceiveType] (state: trio._channel.MemoryChannelState[ReceiveType`1], closed: builtins.bool =, tasks: builtins.set[trio._core._run.Task] =) -> trio._channel.MemoryReceiveChannel[ReceiveType`1]"
reveal_type(trio.MemoryReceiveChannel.aclose) # N: Revealed type is "def [ReceiveType] (self: trio._channel.MemoryReceiveChannel[ReceiveType`1]) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.MemoryReceiveChannel.clone) # N: Revealed type is "def [ReceiveType] (self: trio._channel.MemoryReceiveChannel[ReceiveType`1]) -> trio._channel.MemoryReceiveChannel[ReceiveType`1]"
reveal_type(trio.MemoryReceiveChannel.close) # N: Revealed type is "def [ReceiveType] (self: trio._channel.MemoryReceiveChannel[ReceiveType`1])"
reveal_type(trio.MemoryReceiveChannel.receive) # N: Revealed type is "def [ReceiveType] (self: trio._channel.MemoryReceiveChannel[ReceiveType`1]) -> typing.Coroutine[Any, Any, ReceiveType`1]"
reveal_type(trio.MemoryReceiveChannel.receive_nowait) # N: Revealed type is "def [ReceiveType] (self: trio._channel.MemoryReceiveChannel[ReceiveType`1]) -> ReceiveType`1"
reveal_type(trio.MemoryReceiveChannel.statistics) # N: Revealed type is "def [ReceiveType] (self: trio._channel.MemoryReceiveChannel[ReceiveType`1]) -> trio._channel.MemoryChannelStats"
reveal_type(trio.MemorySendChannel) # N: Revealed type is "def [SendType] (state: trio._channel.MemoryChannelState[SendType`1], closed: builtins.bool =, tasks: builtins.set[trio._core._run.Task] =) -> trio._channel.MemorySendChannel[SendType`1]"
reveal_type(trio.MemorySendChannel.aclose) # N: Revealed type is "def [SendType] (self: trio._channel.MemorySendChannel[SendType`1]) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.MemorySendChannel.clone) # N: Revealed type is "def [SendType] (self: trio._channel.MemorySendChannel[SendType`1]) -> trio._channel.MemorySendChannel[SendType`1]"
reveal_type(trio.MemorySendChannel.close) # N: Revealed type is "def [SendType] (self: trio._channel.MemorySendChannel[SendType`1])"
reveal_type(trio.MemorySendChannel.send) # N: Revealed type is "def [SendType] (self: trio._channel.MemorySendChannel[SendType`1], value: SendType`1) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.MemorySendChannel.send_nowait) # N: Revealed type is "def [SendType] (self: trio._channel.MemorySendChannel[SendType`1], value: SendType`1)"
reveal_type(trio.MemorySendChannel.statistics) # N: Revealed type is "def [SendType] (self: trio._channel.MemorySendChannel[SendType`1]) -> trio._channel.MemoryChannelStats"
reveal_type(trio.Process.encoding) # N: Revealed type is "None"
reveal_type(trio.Process.errors) # N: Revealed type is "None"
reveal_type(trio.Process.universal_newlines) # N: Revealed type is "builtins.bool"
reveal_type(trio.TASK_STATUS_IGNORED) # N: Revealed type is "trio._core._run._TaskStatusIgnored"
reveal_type(trio.abc.Channel.receive) # N: Revealed type is "def [T] (self: trio._abc.ReceiveChannel[ReceiveType`1]) -> typing.Coroutine[Any, Any, ReceiveType`1]"
reveal_type(trio.abc.Channel.send) # N: Revealed type is "def [T] (self: trio._abc.SendChannel[SendType`1], value: SendType`1) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.abc.ReceiveChannel.receive) # N: Revealed type is "def [ReceiveType] (self: trio._abc.ReceiveChannel[ReceiveType`1]) -> typing.Coroutine[Any, Any, ReceiveType`1]"
reveal_type(trio.abc.SendChannel.send) # N: Revealed type is "def [SendType] (self: trio._abc.SendChannel[SendType`1], value: SendType`1) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.lowlevel.FdStream.aclose) # N: Revealed type is "def (self: trio._unix_pipes.FdStream) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.lowlevel.FdStream.close) # N: Revealed type is "def (self: trio._unix_pipes.FdStream)"
reveal_type(trio.lowlevel.FdStream.fileno) # N: Revealed type is "def (self: trio._unix_pipes.FdStream) -> builtins.int"
reveal_type(trio.lowlevel.FdStream.receive_some) # N: Revealed type is "def (self: trio._unix_pipes.FdStream, max_bytes: Union[builtins.int, None] =) -> typing.Coroutine[Any, Any, builtins.bytes]"
reveal_type(trio.lowlevel.FdStream.send_all) # N: Revealed type is "def (self: trio._unix_pipes.FdStream, data: builtins.bytes) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.lowlevel.FdStream.wait_send_all_might_not_block) # N: Revealed type is "def (self: trio._unix_pipes.FdStream) -> typing.Coroutine[Any, Any, None]"
reveal_type(trio.lowlevel.Task) # N: Revealed type is "def (parent_nursery: Any, coro: Any, runner: Any, name: Any, context: Any, next_send_fn: Any =, next_send: Any =, abort_func: Any =, custom_sleep_data: Any =, child_nurseries: Any =, eventual_parent_nursery: Any =, cancel_points: Any =, schedule_points: Any =, cancel_status: Any =) -> trio._core._run.Task"
reveal_type(trio.lowlevel.add_instrument) # N: Revealed type is "def (instrument: trio._abc.Instrument)"
reveal_type(trio.lowlevel.open_process) # N: Revealed type is "def (command: Any, *, stdin: Any =, stdout: Any =, stderr: Any =, **options: Any) -> typing.Coroutine[Any, Any, trio._subprocess.Process]"
reveal_type(trio.lowlevel.remove_instrument) # N: Revealed type is "def (instrument: trio._abc.Instrument)"
reveal_type(trio.lowlevel.start_guest_run) # N: Revealed type is "def (async_fn: Any, *args: Any, *, run_sync_soon_threadsafe: Any, done_callback: Any, run_sync_soon_not_threadsafe: Any =, host_uses_signal_set_wakeup_fd: builtins.bool =, clock: Any =, instruments: Any =, restrict_keyboard_interrupt_to_checkpoints: builtins.bool =, strict_exception_groups: builtins.bool =) -> Any"
reveal_type(trio.lowlevel.start_thread_soon) # N: Revealed type is "def (fn: Any, deliver: Any, name: Union[builtins.str, None] =) -> Any"
reveal_type(trio.lowlevel.wait_task_rescheduled) # N: Revealed type is "def (abort_func: def (def () -> <nothing>) -> trio._core._traps.Abort) -> typing.Coroutine[Any, Any, Any]"
reveal_type(trio.open_memory_channel) # N: Revealed type is "def [T] (max_buffer_size: builtins.int) -> Tuple[trio._channel.MemorySendChannel[T`1], trio._channel.MemoryReceiveChannel[T`1], fallback=trio._channel.open_memory_channel[T`1]]"
reveal_type(trio.run) # N: Revealed type is "def (async_fn: Any, *args: Any, *, clock: Any =, instruments: Any =, restrict_keyboard_interrupt_to_checkpoints: builtins.bool =, strict_exception_groups: builtins.bool =) -> Any"
reveal_type(trio.socket.IP_BIND_ADDRESS_NO_PORT) # N: Revealed type is "builtins.int"
reveal_type(trio.to_thread.run_sync) # N: Revealed type is "def (sync_fn: Any, *args: Any, *, thread_name: Union[builtins.str, None] =, cancellable: Any =, limiter: Any =) -> typing.Coroutine[Any, Any, Any]"
Loading

0 comments on commit 4edfd3d

Please sign in to comment.