Represent the use of AddressFamily in socket.getaddrinfo#3009
Merged
JelleZijlstra merged 1 commit intopython:masterfrom Jun 19, 2019
Merged
Represent the use of AddressFamily in socket.getaddrinfo#3009JelleZijlstra merged 1 commit intopython:masterfrom
JelleZijlstra merged 1 commit intopython:masterfrom
Conversation
Contributor
Author
|
The minimal version of the code I was trying to typecheck that caused me to discover this was: |
stdlib/2and3/socket.pyi
Outdated
| host: Optional[Union[bytearray, bytes, Text]], port: Union[str, int, None], family: int = ..., | ||
| socktype: int = ..., proto: int = ..., | ||
| flags: int = ...) -> List[Tuple[int, int, int, str, Tuple[Any, ...]]]: | ||
| flags: int = ...) -> List[Tuple[AddressFamily, int, int, str, Tuple[Any, ...]]]: |
Member
There was a problem hiding this comment.
This should be within an if sys.version >= (3, 4): block, because on 2.7 the first tuple member is still an int.
Also, it looks like the second tuple member is a SocketKind enum, at least on 3.6.
Contributor
Author
There was a problem hiding this comment.
There's already https://github.com/python/typeshed/blob/master/stdlib/2and3/socket.pyi#L454 which handles the < 3.4 case by defining AddressFamily to be int.
Good point about SocketKind, I'll add that and re-push. I was focused too much on the AddressFamily to remember the next argument...
3ef93ad to
af276db
Compare
The Pull Request python#1121 added the `AddressFamily` type to `socket.pyi` for Python 3.4+, so constants such as `AF_INET` are correctly represented as being an enum member rather than an int. The same is true of the `SocketKind` enums in the `SOCK_*` family. Various functions in the socket module can accept either an int or an `AF_*` enum member as arguments, which is allowed by the int argument type. However the `getaddrinfo` function returns an `AddressFamily` member rather than an int in the first position of its list members, so code that access enum specific members such as the `name` attribute causes a typing error to be found. This change corrects the return type of `getaddrinfo` but leaves the family parameters as int, given that `AddressFamily` members are `IntEnum` and only ever treated as `int`s internally.
af276db to
a07bf24
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Pull Request #1121 added the
AddressFamilytype tosocket.pyifor Python 3.4+, so constants such asAF_INETare correctly represented as being an enum member rather than an int.Various functions in the socket module can accept either an int or an
AF_*enum member as arguments, which is allowed by the int argument type. However thegetaddrinfofunction returns anAddressFamilymember rather than an int in the first position of its list members, so code that access enum specific members such as thenameattribute causes a typing error to be found.This change corrects the return type of
getaddrinfobut leaves the family parameters as int, given thatAddressFamilymembers areIntEnumand only ever treated asints internally.