-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
Typing SocketType, test_socket, test_highlevel_[socket, open_tcp_stream, open_tcp_listeners] #2774
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #2774 +/- ##
==========================================
+ Coverage 98.97% 99.10% +0.12%
==========================================
Files 115 115
Lines 17117 17208 +91
Branches 3079 3084 +5
==========================================
+ Hits 16942 17054 +112
+ Misses 121 106 -15
+ Partials 54 48 -6
|
Hm, looking at https://docs.python.org/3/library/socket.html#socket-families I now understand why methods like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good other than a few formatting things and a question about Any
vs object
usage. I also think it would be good to annotate the __init__
return types while we are at it so we don't have to do it later.
I have repeatedly asked you why you think this, while giving you explanations why it doesn't. It does not matter for |
There was a problem hiding this 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, but I would replace the Address
-> Any
change with Address
-> AddressFormat
alias. One of the review comments has more details why I think this should be done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uh, turns out I had a very old review-in-progress and it's a mess to find where I commented on what, so I'll submit this now and it might be outdated.
@abstractmethod | ||
def socket( | ||
self, | ||
family: socket.AddressFamily | int | None = None, | ||
type: socket.SocketKind | int | None = None, | ||
proto: int | None = None, | ||
) -> _SocketType: | ||
family: socket.AddressFamily | int = socket.AF_INET, | ||
type: socket.SocketKind | int = socket.SOCK_STREAM, | ||
proto: int = 0, | ||
) -> SocketType: | ||
"""Create and return a socket object. | ||
|
||
Your socket object must inherit from :class:`trio.socket.SocketType`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want these types to be | None
, as that affects the signature of all subclasses of SocketFactory. But this is a change in behaviour, so it might be better to just do a type: ignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having them default to the values they would have became originally is a good thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine. I approach type hints as not bound by semver (i.e. can narrow them or expand them as long as we're not passing something with a new type in) and subclasses can still default to None
. Hopefully that approach is sane...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the issue is not the type hint, but the default value being changed from None -> socket.AF_INET
etc etc.
although thinking about it a bit more, I'm having trouble coming up with any code that actually changes in behaviour from this. If you're subclassing SocketFactory
and defining your own SocketFactory.socket
, the default values in SocketFactory.socket
shouldn't modify the behaviour of MyClass.socket
in any way .. I think?
|
(It might just be cause I haven't fully woken up yet, but quoted message feels a little bit too aggressive... it's alright and can stay since it's obviously not meant to be such: just for CoolCat, don't take it in an aggressive way ^^) I'll make sure to actually review this in a while!! Sorry about the delay. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the issues with AddressFormat
explained, I think this looks good!
# example.py
class ClassName:
def __init__(self):
pass dmypy run "example.py" -- --disallow-untyped-defs yields the following:
|
That's a special case where |
It indeed does not raise errors when it has arguments in dmypy 1.5.1, but I feel like this was not always the case. I always make sure it has the return type annotated mostly because I don't like the idea of special cases. Everything else has the return types annotated, so why shouldn't this? And plus, then any code that looks at the Confirming this is one of the comments in PEP-484:
|
I am sorry for the overly aggressive message, but I did get a bit frustrated about it having come up several times :) 1 2 3 4
mypy: https://mypy.readthedocs.io/en/stable/class_basics.html#annotating-init-methods
The comment in PEP-484 is specifically about "whether a type checker should check the function, or treat it as an untyped function and ignore the content", and not any confusion about what the return type actually is. (returning non-None will also raise a TypeError: https://docs.python.org/3/reference/datamodel.html?highlight=init#object.__init__ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't review the tests yet but generally I'm just gonna trust you're doing the right thing 😅
@abstractmethod | ||
def socket( | ||
self, | ||
family: socket.AddressFamily | int | None = None, | ||
type: socket.SocketKind | int | None = None, | ||
proto: int | None = None, | ||
) -> _SocketType: | ||
family: socket.AddressFamily | int = socket.AF_INET, | ||
type: socket.SocketKind | int = socket.SOCK_STREAM, | ||
proto: int = 0, | ||
) -> SocketType: | ||
"""Create and return a socket object. | ||
|
||
Your socket object must inherit from :class:`trio.socket.SocketType`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine. I approach type hints as not bound by semver (i.e. can narrow them or expand them as long as we're not passing something with a new type in) and subclasses can still default to None
. Hopefully that approach is sane...
not TYPE_CHECKING and hasattr(_stdlib_socket.socket, "share") | ||
): | ||
|
||
def share(self, /, process_id: int) -> bytes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This coverage warning here also doesn't make sense given we're running the tests on Windows...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah no clue :<
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on a rerun there were no coverage warning, so seems like a temporary glitch.
This reverts commit 7335184.
uh, thought for a minute that |
Resolves #2720
Quite messy, and looks like there's some signatures in
_socket.py
that might warrant changing. Will write a summary when I've finished uptest_highlevel_open_tcp_listeners
TODO: