Skip to content

bpo-39481: Add GenericAlias support to ipaddress.py #19418

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

Merged
merged 4 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


import functools
import types

IPV4LENGTH = 32
IPV6LENGTH = 128
Expand Down Expand Up @@ -1124,6 +1125,7 @@ def is_loopback(self):
return (self.network_address.is_loopback and
self.broadcast_address.is_loopback)

__class_getitem__ = classmethod(types.GenericAlias)

class _BaseV4:

Expand Down Expand Up @@ -1444,6 +1446,8 @@ def with_hostmask(self):
return '%s/%s' % (self._string_from_ip_int(self._ip),
self.hostmask)

__class_getitem__ = classmethod(types.GenericAlias)


class IPv4Network(_BaseV4, _BaseNetwork):

Expand Down Expand Up @@ -2152,6 +2156,8 @@ def is_unspecified(self):
def is_loopback(self):
return self._ip == 1 and self.network.is_loopback

__class_getitem__ = classmethod(types.GenericAlias)


class IPv6Network(_BaseV6, _BaseNetwork):

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
from collections.abc import *
from contextlib import AbstractContextManager, AbstractAsyncContextManager
from ipaddress import IPv4Network, IPv4Interface, IPv6Network, IPv6Interface
from itertools import chain
from os import DirEntry
from re import Pattern, Match
Expand Down Expand Up @@ -36,6 +37,9 @@ def test_subscriptable(self):
Mapping, MutableMapping, MappingView,
KeysView, ItemsView, ValuesView,
Sequence, MutableSequence,
MappingProxyType,
DirEntry,
IPv4Network, IPv4Interface, IPv6Network, IPv6Interface,
MappingProxyType, DirEntry,
chain,
):
Expand Down