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 type annotations for _ssl.py #2745

Merged
merged 30 commits into from
Aug 21, 2023
Merged

Conversation

CoolCat467
Copy link
Member

This PR adds type annotations to _ssl.py

Three interesting thins I noticed when making sure the type annotations were good:

  1. There was a type annotation bug in trio.abc.Listener not using its T_resource generic in accept, now fixed
  2. Mypy says SSLStream.receive_some could have potentially returned None if my typing for SSLStream._retry is correct. I don't think this is entirely true, I think everything is supposed to raise a trio.BrokenResourceError exception, but if it raised something else it could have returned None. There is a type-narrowing assert not None there now, but I am curious if anyone has more information.
  3. SSLStream.unwrap sets self.transport_stream to None, and nothing else checks to make sure it's not None anywhere. I assume this is ok since it also sets the internal state to CLOSED, so I put a type ignore there to silence the error. The reason I hesitate to allow self.transport_stream to be None is because the docstring says it's required to be a Stream object.

I would love feedback and any comments on how this could be improved to be more accurate.

@codecov
Copy link

codecov bot commented Aug 7, 2023

Codecov Report

Merging #2745 (3020001) into master (3bdab93) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2745   +/-   ##
=======================================
  Coverage   98.94%   98.94%           
=======================================
  Files         113      113           
  Lines       16837    16843    +6     
  Branches     3036     3036           
=======================================
+ Hits        16659    16665    +6     
  Misses        123      123           
  Partials       55       55           
Files Changed Coverage Δ
trio/_abc.py 100.00% <100.00%> (ø)
trio/_ssl.py 100.00% <100.00%> (ø)

@CoolCat467 CoolCat467 marked this pull request as ready for review August 7, 2023 03:38
@CoolCat467 CoolCat467 added the typing Adding static types to trio's interface label Aug 7, 2023
trio/_ssl.py Outdated Show resolved Hide resolved
@A5rocks
Copy link
Contributor

A5rocks commented Aug 8, 2023

Oops, didn't mean to click single comment. I think that + moving the __slots__ to your specialized PR for that would be neat.

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.

Guess which file needs to be updated...? pyproject.toml!

Does your setup not use it for enabling checks? In my workflow I first need to update pyproject.toml in order to be able to get the errors and see what needs fixing.

Otherwise mostly just nits

server_hostname: str | None = None,
server_side: bool = False,
https_compatible: bool = False,
) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

nit: inconsequential -> None. We ought to create a style guide somewhere for typing, and/or add a check to #2744 that removes/adds it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not inconsequential, I am fairly certain that verify_types.json changes for the better when it's there

Copy link
Member

Choose a reason for hiding this comment

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

Are you referring to #2740 (comment) ?
I removed the -> None without any impact on verify_types.json

Copy link
Contributor

Choose a reason for hiding this comment

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

Just bumping on this!

trio/_ssl.py Outdated Show resolved Hide resolved
trio/_tests/verify_types.json Outdated Show resolved Hide resolved
trio/_ssl.py Outdated Show resolved Hide resolved
@@ -399,7 +410,7 @@ def __init__(
"version",
}

def __getattr__(self, name):
def __getattr__(self, name: str) -> Any:
Copy link
Member

Choose a reason for hiding this comment

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

I suppose there's a case for returning object here to tell the user they need to do isinstance. But they can also enable disallow_any_expression I suppose.
I tried finding an authoritative best-practice of what to do in this case, but no dice

@CoolCat467
Copy link
Member Author

How should this issue with readthedocs be addressed?

/home/docs/checkouts/readthedocs.org/user_builds/trio/checkouts/2745/trio/abc.py:docstring of trio.abc.Listener.accept:1: WARNING: py:class reference target not found: trio._abc.T_resource

@TeamSpen210
Copy link
Contributor

How should this issue with readthedocs be addressed?

/home/docs/checkouts/readthedocs.org/user_builds/trio/checkouts/2745/trio/abc.py:docstring of trio.abc.Listener.accept:1: WARNING: py:class reference target not found: trio._abc.T_resource

You've got to add it to the ignore list in docs/conf.py, rather annoying.

@CoolCat467 CoolCat467 requested a review from jakkdl August 9, 2023 02:36
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.

oops nvm, the ambiguous type is still not resolved

@CoolCat467 CoolCat467 requested a review from jakkdl August 10, 2023 03:37
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.

Seems mostly good, just a couple things that jumped out at me (most aren't even typing-related).

pyproject.toml Outdated Show resolved Hide resolved
trio/_ssl.py Show resolved Hide resolved
trio/_ssl.py Outdated Show resolved Hide resolved
trio/_ssl.py 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.

+1 to a5rock on leaving __slots__ to separate PR as they've been controversial, otherwise seems ready to merge.

@CoolCat467 CoolCat467 requested review from jakkdl and A5rocks August 14, 2023 17:39
trio/_ssl.py Outdated Show resolved Hide resolved
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.

Just a couple comments.

trio/_ssl.py Outdated Show resolved Hide resolved
trio/_ssl.py Outdated Show resolved Hide resolved
@CoolCat467 CoolCat467 requested a review from A5rocks August 16, 2023 05:21
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.

Just some comments!

trio/_ssl.py Outdated Show resolved Hide resolved
server_hostname: str | None = None,
server_side: bool = False,
https_compatible: bool = False,
) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Just bumping on this!

self._incoming = _stdlib_ssl.MemoryBIO()
self._ssl_object = ssl_context.wrap_bio(
self._incoming,
self._outgoing,
server_side=server_side,
server_hostname=server_hostname,
server_hostname=server_hostname, # type: ignore[arg-type] # Typeshed bug, does accept bytes as well (typeshed#10590)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like that PR was merged, meaning next mypy release should support this! Nice!

https_compatible=False,
):
https_compatible: bool = False,
) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW this -> None is probably also unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I haven't seen any reason why it would change anything.

@jakkdl
Copy link
Member

jakkdl commented Aug 21, 2023

merging main ...

$ tox -e verifytypes
******************** 
Checking type completeness hasn't gone down...
creating py.typed
completenessScore has gone up from 0.9904458598726115 to 1
withUnknownType has gone down from 6 to 0
withKnownType has gone up from 622 to 628
Traceback (most recent call last):
  File "trio/_tests/check_type_completeness.py", line 190, in <module>
    sys.exit(main(args))
  File "trio/_tests/check_type_completeness.py", line 131, in main
    assert (
AssertionError: Fully type complete! Delete this script and instead directly run `pyright --verifytypes=trio` (consider `--ignoreexternal`) in CI and checking exit code.

🚀 🚀

although it seems to think some symbols that are in fact public aren't, so it's partly a lie

# Conflicts:
#	pyproject.toml
#	trio/_tests/verify_types.json
@jakkdl jakkdl enabled auto-merge (squash) August 21, 2023 11:28
@jakkdl jakkdl merged commit f9fdeb7 into python-trio:master Aug 21, 2023
@CoolCat467 CoolCat467 deleted the typing-ssl branch August 22, 2023 21:03
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.

4 participants