Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types to most of trio.testing #2747

Merged
merged 23 commits into from
Aug 18, 2023
Merged

Conversation

TeamSpen210
Copy link
Contributor

This adds types to trio.testing, except for _fake_net (which is considerably more complex and should probably go in its own PR). It's fairly straightforward, though in a few cases I made functions forward on both *args/**kwargs so they can use ParamSpec, instead of just one or the other. For _check_streams, to correctly type things the functions now expect/return tuples instead of other iterables that just happen to be 2-long - in practice I don't think people would do anything else anyway. It'll just fail type checking if so.

@TeamSpen210 TeamSpen210 added the typing Adding static types to trio's interface label Aug 8, 2023
@codecov
Copy link

codecov bot commented Aug 8, 2023

Codecov Report

Merging #2747 (054fe70) into master (a316034) will decrease coverage by 0.02%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2747      +/-   ##
==========================================
- Coverage   98.93%   98.92%   -0.02%     
==========================================
  Files         113      113              
  Lines       16752    16770      +18     
  Branches     3027     3027              
==========================================
+ Hits        16574    16590      +16     
  Misses        123      123              
- Partials       55       57       +2     
Files Changed Coverage Δ
trio/_core/_run.py 99.45% <ø> (ø)
trio/_highlevel_generic.py 100.00% <100.00%> (ø)
trio/_subprocess.py 97.03% <100.00%> (ø)
trio/testing/_check_streams.py 100.00% <100.00%> (ø)
trio/testing/_checkpoints.py 100.00% <100.00%> (ø)
trio/testing/_memory_streams.py 100.00% <100.00%> (ø)
trio/testing/_network.py 100.00% <100.00%> (ø)
trio/testing/_trio_test.py 90.00% <100.00%> (+2.50%) ⬆️

... and 1 file with indirect coverage changes

Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly just a bunch of nits, looks great overall! And imo the types increase readability of a lot of this code

"trio.testing.memory_stream_pair",
"trio.testing.memory_stream_pump",
"trio.testing.open_stream_to_socket_listener",
"trio.testing.trio_test",
"trio.testing.wait_all_tasks_blocked",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anybody else is confused: wait_all_tasks_blocked is defined in _core/[_generated]_run.py but exported in testing/__init__.py and is removed from this file in #2733

trio/testing/_check_streams.py Show resolved Hide resolved
trio/testing/_check_streams.py Outdated Show resolved Hide resolved
trio/testing/_checkpoints.py Show resolved Hide resolved
trio/testing/_memory_streams.py Outdated Show resolved Hide resolved
trio/testing/_memory_streams.py Show resolved Hide resolved
Copy link
Member

@CoolCat467 CoolCat467 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good except noted, most of noted is that I think a lot of places might accept memoryview objects as well

async def do_send_all(data):
with assert_checkpoints():
assert await s.send_all(data) is None
async def do_send_all(data: bytes) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If s is a Stream object here, it actually accepts bytes | bytearray | memoryview apparently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might, but it doesn't really matter - these are internal nested functions, they're only being called with bytes objects.

trio/testing/_check_streams.py Outdated Show resolved Hide resolved
trio/testing/_check_streams.py Outdated Show resolved Hide resolved
trio/testing/_memory_streams.py Outdated Show resolved Hide resolved
trio/testing/_memory_streams.py Outdated Show resolved Hide resolved
trio/testing/_memory_streams.py Outdated Show resolved Hide resolved
docs/source/conf.py Outdated Show resolved Hide resolved
@jakkdl
Copy link
Member

jakkdl commented Aug 14, 2023

Merged in master, and while doing so found out that trio/_core/_run was incorrectly typed @TeamSpen210
see: d1a2c99

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, though I'm not really able to review this too well without spending way more time than I'd like :(

(as a sidenote we should probably go through a couple of the tests and type them to make sure the user-side types are sane...)

@@ -2152,7 +2152,7 @@ def setup_runner(


def run(
async_fn: Callable[..., RetT],
async_fn: Callable[..., Awaitable[RetT]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Geh. I assume there's no other things like this?

trio/testing/_check_streams.py Outdated Show resolved Hide resolved
trio/testing/_memory_streams.py Outdated Show resolved Hide resolved
@jakkdl
Copy link
Member

jakkdl commented Aug 16, 2023

yeah, once we're done with the public API we should

  1. ask users to clone master and run against their code & report problems
  • are there any typed open source trio repos currently using trio-typing we can test against?
  1. type tests
  2. run tests in trio-typing to see if any of those stubs disagree

I think there's diminishing returns in reviewing PRs extremely thoroughly and it's more or less guaranteed that some problems will slip through, but it's more efficient to just catch those later imho - esp as type errors don't affect functionality.

pyproject.toml Outdated Show resolved Hide resolved
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the ignores from pyproject.toml:

# 2747
"trio/testing/_network",
"trio/testing/_trio_test",
"trio/testing/_checkpoints",
"trio/testing/_check_streams",
"trio/testing/_memory_streams",

remove testing/* files from the ignorelist in pyproject.toml for mypy
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or I can just do it myself when there's no other problem :p

@jakkdl jakkdl enabled auto-merge (squash) August 18, 2023 15:41
@jakkdl jakkdl merged commit 5c85e67 into python-trio:master Aug 18, 2023
26 of 27 checks passed
@TeamSpen210 TeamSpen210 deleted the type-testing branch August 20, 2023 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing Adding static types to trio's interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants