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

lots of typing improvements #2682

Merged
merged 25 commits into from
Jul 13, 2023
Merged

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Jul 3, 2023

Lots and lots of trivial typehints, but a couple tricky ones hidden inside the huge diff >.>
I'll point some of them out in a review of my own.

@codecov
Copy link

codecov bot commented Jul 3, 2023

Codecov Report

Merging #2682 (2536843) into master (6d71047) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2682   +/-   ##
=======================================
  Coverage   98.84%   98.84%           
=======================================
  Files         113      113           
  Lines       16474    16507   +33     
  Branches     3004     3010    +6     
=======================================
+ Hits        16283    16316   +33     
  Misses        134      134           
  Partials       57       57           
Impacted Files Coverage Δ
trio/__init__.py 100.00% <ø> (ø)
trio/_channel.py 100.00% <ø> (ø)
trio/lowlevel.py 100.00% <ø> (ø)
trio/_abc.py 100.00% <100.00%> (ø)
trio/_core/__init__.py 100.00% <100.00%> (ø)
trio/_core/_mock_clock.py 100.00% <100.00%> (ø)
trio/_core/_multierror.py 100.00% <100.00%> (ø)
trio/_core/_parking_lot.py 100.00% <100.00%> (ø)
trio/_core/_run.py 99.24% <100.00%> (+<0.01%) ⬆️
trio/_core/_tests/test_parking_lot.py 100.00% <100.00%> (ø)
... and 9 more

trio/_abc.py Outdated Show resolved Hide resolved
trio/_abc.py Outdated Show resolved Hide resolved
trio/_core/_parking_lot.py Outdated Show resolved Hide resolved
trio/_core/_parking_lot.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
trio/_sync.py Outdated
Comment on lines 92 to 104
async def __aenter__(self):
await self.acquire()
async def __aenter__(self) -> None:
await self.acquire() # type: ignore[attr-defined]

@enable_ki_protection
async def __aexit__(self, *args):
self.release()
async def __aexit__(self, *args: object) -> None:
self.release() # type: ignore[attr-defined]
Copy link
Member Author

Choose a reason for hiding this comment

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

slightly illegal type: ignores, but would probably require some significant refactoring to get rid of.

trio/_sync.py Show resolved Hide resolved
@jakkdl
Copy link
Member Author

jakkdl commented Jul 3, 2023

whoa, nasty CI fails. Will look at that tomorrow or so

trio/_core/_parking_lot.py Outdated Show resolved Hide resolved
trio/_highlevel_socket.py Outdated Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
trio/_sync.py Outdated Show resolved Hide resolved
trio/_sync.py Outdated Show resolved Hide resolved
Fuyukai
Fuyukai previously requested changes Jul 3, 2023
Copy link
Member

@Fuyukai Fuyukai left a comment

Choose a reason for hiding this comment

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

too many individual ones to comment on, but __exit__ and __aexit__ returns bool, not None. (or, well, it returns either True or anything else).

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.

too many individual ones to comment on, but exit and aexit returns bool, not None. (or, well, it returns either True or anything else).

FWIW, in typeshed these are sometimes typed as None also, just as precedent. These return types matter though: if one of these is typed as returning None or Literal[False], then mypy at least can infer some special stuff about the context managers as it knows they won't swallow errors. (refs: they reverted python/mypy#7577 but reachability is changed by this: python/mypy#7666)

This was just a quick review though!

trio/_abc.py Outdated Show resolved Hide resolved
trio/_abc.py Outdated Show resolved Hide resolved
trio/_highlevel_socket.py Show resolved Hide resolved
trio/_highlevel_socket.py Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
trio/_abc.py Outdated Show resolved Hide resolved
@@ -278,7 +280,7 @@ class SendStream(AsyncResource):
__slots__ = ()

@abstractmethod
async def send_all(self, data):
async def send_all(self, data: bytes | bytearray | memoryview) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably should be typing_extensions.Buffer, perhaps? Might be worth discussing in an issue on its own though, since it changes this core interface, widening it to any buffer-supporting class.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're very welcome to open it for discussion! I'm not aware of the nuances here at all, just copy-pasting types.

@jakkdl
Copy link
Member Author

jakkdl commented Jul 4, 2023

too many individual ones to comment on, but exit and aexit returns bool, not None. (or, well, it returns either True or anything else).

FWIW, in typeshed these are sometimes typed as None also, just as precedent. These return types matter though: if one of these is typed as returning None or Literal[False], then mypy at least can infer some special stuff about the context managers as it knows they won't swallow errors. (refs: they reverted python/mypy#7577 but reachability is changed by this: python/mypy#7666)

typing them to be returning bool gives type issues unless you add explicit return statements - and those can't just return False. Adding extraneous return statements does not seem great, so I left a bunch of them as -> None - but I updated several to do -> bool|None or -> bool.

@jakkdl
Copy link
Member Author

jakkdl commented Jul 4, 2023

ugh, ofc the docs build now requires docstrings for all .*Statistics.

Fixed the tests, I'd accidentally deleted a default value for a parameter

trio/_core/_run.py Outdated Show resolved Hide resolved
@jakkdl jakkdl requested a review from TeamSpen210 July 5, 2023 15:59
Copy link
Contributor

@TeamSpen210 TeamSpen210 left a comment

Choose a reason for hiding this comment

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

Few minor things, otherwise looks good.

trio/_core/_parking_lot.py Show resolved Hide resolved
trio/_core/_parking_lot.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_dtls.py Show resolved Hide resolved
trio/_dtls.py Show resolved Hide resolved
@jakkdl jakkdl requested a review from A5rocks July 6, 2023 13:52
@jakkdl jakkdl requested a review from TeamSpen210 July 6, 2023 14:20
@TeamSpen210
Copy link
Contributor

I think the issue there is that they need to have a definition in the documentation, so there's somewhere for them to link to.

Copy link
Contributor

@TeamSpen210 TeamSpen210 left a comment

Choose a reason for hiding this comment

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

A few minor things, but otherwise seems good.

@@ -492,4 +492,8 @@ def test_classes_are_final():
continue
# ... insert other special cases here ...

# don't care about the *Statistics classes
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of opting these out, wouldn't it be better to make them final? People shouldn't be inheriting from them.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really see any strong reason to forbid people from inheriting them either? I could plausibly see somebody writing a wrapper around one of the locks and them deeming it useful to inherit from the Statistics class.

@@ -1096,6 +1096,8 @@ Broadcasting an event with :class:`Event`
.. autoclass:: Event
:members:

.. autoclass:: EventStatistics
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of documenting the class itself here, it might be possible to just put an index directive in the statistics() method, so the name links back there. That way we don't have the same information repeated twice.

Copy link
Member Author

Choose a reason for hiding this comment

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

After trying a bunch of different variants I can't get index to create a class-type reference. I can create an index named trio.EventStatistics that I can link to with :ref: - but I failed to create one I can link to with :class: which is what autodoc generates the docstring to do.

trio/_core/_run.py Outdated Show resolved Hide resolved
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Small notes below, but this looks fantastic overall and I'm really looking forward to using Trio with annotations.

docs/source/conf.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_highlevel_generic.py Outdated Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
trio/_socket.py Outdated Show resolved Hide resolved
@jakkdl jakkdl mentioned this pull request Jul 10, 2023
@jakkdl
Copy link
Member Author

jakkdl commented Jul 10, 2023

I'll go ahead and merge this unless @A5rocks or @Fuyukai wants to re-review

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 mostly fine. I've been assuming that the types are valid and from a couple checks early on it looks like they are.

I held back extraneous commentary!!

trio/_abc.py Outdated Show resolved Hide resolved
trio/_abc.py Show resolved Hide resolved
trio/_core/_parking_lot.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_core/_run.py Outdated Show resolved Hide resolved
trio/_highlevel_generic.py Show resolved Hide resolved
trio/lowlevel.py Outdated Show resolved Hide resolved
trio/_sync.py Show resolved Hide resolved
async def __aenter__(self):
await self.acquire()
async def __aenter__(self) -> None:
await self.acquire() # type: ignore[attr-defined]
Copy link
Contributor

Choose a reason for hiding this comment

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

We can drop this attr-defined through fancy self-type schenanigans:

import typing

class ACM(typing.Protocol):
    async def acquire(self) -> None:
        pass

class AsyncContextManagerMixinWithError:
    async def __aenter__(self) -> None:
        await self.acquire()

class AsyncContextManagerMixinWithoutError:
    async def __aenter__(self: ACM) -> None:
        await self.acquire()

Feel free to check (I find https://mypy-play.net nice for testing small things like this) :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooooh, fancy! Leaving it for a future PR, adding a comment with a #TODO, as I'm unused to Protocols.

trio/_sync.py Show resolved Hide resolved
@jakkdl jakkdl dismissed Fuyukai’s stale review July 11, 2023 11:24

No followup review after changes have been adressed

Copy link
Member

@Fuyukai Fuyukai 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 to me after a look-over.

@jakkdl jakkdl requested a review from A5rocks July 11, 2023 13:15
@jakkdl
Copy link
Member Author

jakkdl commented Jul 11, 2023

I have started on a follow-up typing PR, so I'll merge this one tomorrow unless you have any final objections @A5rocks (or you can go ahead and merge it) and any more changes can be addressed in it.

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 now

@A5rocks A5rocks merged commit 4cd3164 into python-trio:master Jul 13, 2023
@jakkdl jakkdl deleted the typing_improvements branch July 13, 2023 13:08
@A5rocks A5rocks added the typing Adding static types to trio's interface label Jul 18, 2023
@Zac-HD Zac-HD mentioned this pull request Jul 21, 2023
11 tasks
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