Skip to content

Add socket enum classes from py3.4+#1121

Merged
JelleZijlstra merged 3 commits intopython:masterfrom
amyreese:socket-enums
Apr 5, 2017
Merged

Add socket enum classes from py3.4+#1121
JelleZijlstra merged 3 commits intopython:masterfrom
amyreese:socket-enums

Conversation

@amyreese
Copy link
Contributor

Adds four IntEnum classes in the socket module that mirror the
AF_, AI_, MSG_, and SOCK_ sets of constants.

Adds four IntEnum classes in the socket module that mirror the
AF_, AI_, MSG_, and SOCK_ sets of constants.
@amyreese
Copy link
Contributor Author

Running python tests/mypy_test.py on my mac with Py3.6.1 failed with the following errors, even from a fresh clone: https://gist.github.com/jreese/d95da7c166a81c8161b02c4f3bff98d8

@gvanrossum
Copy link
Member

What version of mypy did you have installed for that test?

@amyreese
Copy link
Contributor Author

I used pip install -r requirements-tests-py3.txt in a 3.6.1 virtualenv following the readme:

(.venv3) jreese@jreese-mbp ~/workspace/typeshed +socket-enums⚡ » pip list
attrs (16.3.0)
flake8 (3.3.0)
flake8-bugbear (17.3.0)
flake8-pyi (17.3.0)
mccabe (0.6.1)
mypy (0.501)
pip (9.0.1)
pycodestyle (2.3.1)
pyflakes (1.5.0)
setuptools (28.8.0)
typed-ast (1.0.2)

@gvanrossum
Copy link
Member

Can you try again with mypy master?

@amyreese
Copy link
Contributor Author

Yep, that resolved the errors!


# enum versions of above flags py 3.4+
if sys.version_info >= (3, 4):
class AddressInfo(IntEnum):
Copy link
Member

Choose a reason for hiding this comment

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

This one and MsgFlag actually inherit from IntFlags.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'll update the pull request.

# enum versions of above flags py 3.4+
if sys.version_info >= (3, 4):
class AddressInfo(IntEnum):
AI_ADDRCONFIG = ...
Copy link
Member

Choose a reason for hiding this comment

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

Confirmed locally that you don't need types for the values, at least with mypy. (So no action required from you.)

@gvanrossum
Copy link
Member

Please wait with merging until I've tested this against our internal codebase.

@amyreese
Copy link
Contributor Author

amyreese commented Apr 3, 2017

Is there any progress on testing? I updated the PR with fixes for @JelleZijlstra's comments and only include AddressInfo and MsgFlag for 3.6+.

@gvanrossum
Copy link
Member

The tests with internal codebase pass, so +1 from me. I'll let Jelle decide whether he's happy with the rest.

@JelleZijlstra
Copy link
Member

I'll take another look within a few days (tonight if I have time) and then merge.

@JelleZijlstra JelleZijlstra merged commit 8510adf into python:master Apr 5, 2017
MatthewWilkes added a commit to MatthewWilkes/typeshed that referenced this pull request May 25, 2019
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.

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.
MatthewWilkes added a commit to MatthewWilkes/typeshed that referenced this pull request May 25, 2019
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.

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.
MatthewWilkes added a commit to MatthewWilkes/typeshed that referenced this pull request May 25, 2019
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 pushed a commit that referenced this pull request Jun 19, 2019
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. 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.
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.

3 participants