-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[socket] Use Final
for constants
#14503
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
base: main
Are you sure you want to change the base?
Conversation
* Docs: https://docs.python.org/dev/library/socket.html * Source: https://github.com/python/cpython/blob/main/Modules/socketmodule.c PS: I added value only to those `Final` constants that are not bindings to libc, such as [`SO_VM_SOCKETS_BUFFER_MIN_SIZE`, `VMADDR_PORT_ANY`](https://github.com/python/cpython/blob/2a87af062b79d914ce0120f1f1763213c1ebe8c4/Modules/socketmodule.c#L7775), [`HV_GUID_ZERO`, `HV_GUID_WILDCARD`](https://github.com/python/cpython/blob/2a87af062b79d914ce0120f1f1763213c1ebe8c4/Modules/socketmodule.c#L7836), etc.
This comment has been minimized.
This comment has been minimized.
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! Just two remarks from spot checks on the default values
@@ -743,10 +743,10 @@ class socket: | |||
def timeout(self) -> float | None: ... # noqa: F811 | |||
if sys.platform == "win32": | |||
def __init__( | |||
self, family: int = ..., type: int = ..., proto: int = ..., fileno: SupportsIndex | bytes | None = ... | |||
self, family: int = ..., type: int = ..., proto: int = 0, fileno: SupportsIndex | bytes | None = None |
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.
Curiously, while the docs say that the default for proto
is 0
, the runtime seems to use a default of -1
:
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.
in this Python implementation if fileno
is not passed then family = AF_INET
, type = SOCK_STREAM
, proto = 0
. so I'm not sure that it is possible to say for sure that these values are -1
by default
In C it is also more similar to the init value than the default value. and C implementation has docstring which shows two options with fileno
and without:
"socket(family=AF_INET, type=SOCK_STREAM, proto=0) -> socket object\n\
socket(family=-1, type=-1, proto=-1, fileno=None) -> socket object\n\
I don't like to use family = -1, type = -1, proto = -1
because it doesn't reflect logic, that's why real values are written in documentation
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
PS: I added value only to those
Final
constants that are not bindings to libc, such asSO_VM_SOCKETS_BUFFER_MIN_SIZE
,VMADDR_PORT_ANY
,HV_GUID_ZERO
,HV_GUID_WILDCARD
, etc.