Skip to content

Represent the use of AddressFamily in socket.getaddrinfo#3009

Merged
JelleZijlstra merged 1 commit intopython:masterfrom
MatthewWilkes:getaddrinfo-type
Jun 19, 2019
Merged

Represent the use of AddressFamily in socket.getaddrinfo#3009
JelleZijlstra merged 1 commit intopython:masterfrom
MatthewWilkes:getaddrinfo-type

Conversation

@MatthewWilkes
Copy link
Contributor

The Pull Request #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.

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 ints internally.

@MatthewWilkes MatthewWilkes changed the title Represent the use of AddressFamily in functions in socket.py. Represent the use of AddressFamily in socket.getaddrinfo May 25, 2019
@MatthewWilkes
Copy link
Contributor Author

The minimal version of the code I was trying to typecheck that caused me to discover this was:

import socket

def foo() -> str:
  return socket.getaddrinfo("localhost", 0)[0][0].name

print(foo())

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, ...]]]:
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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...

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.
@JelleZijlstra JelleZijlstra merged commit d149fe4 into python:master Jun 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants