From 97eed3866cab99b0eef04727b4219c0b220439f2 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Wed, 8 Apr 2020 01:22:21 +0300 Subject: [PATCH 1/3] bpo-39481: Generic alias port to ipaddress._BaseNetwork --- Lib/ipaddress.py | 2 ++ Lib/test/test_genericalias.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index ac1143acba426b..ac2ee1faa4ae75 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -12,6 +12,7 @@ import functools +import types IPV4LENGTH = 32 IPV6LENGTH = 128 @@ -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: diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 9c5b23e5e5b0fc..057586e593c3d0 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -7,6 +7,7 @@ ) from collections.abc import * from contextlib import AbstractContextManager, AbstractAsyncContextManager +from ipaddress import _BaseNetwork from os import DirEntry from re import Pattern, Match from types import GenericAlias, MappingProxyType @@ -35,7 +36,7 @@ def test_subscriptable(self): Mapping, MutableMapping, MappingView, KeysView, ItemsView, ValuesView, Sequence, MutableSequence, - MappingProxyType, DirEntry + MappingProxyType, DirEntry, _BaseNetwork ): tname = t.__name__ with self.subTest(f"Testing {tname}"): From 58ec3ee143dd19faea48cf93f69f5bf57f56fe00 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Wed, 8 Apr 2020 01:46:35 +0300 Subject: [PATCH 2/3] Support interfaces, split tests, test only public classes --- Lib/ipaddress.py | 4 ++++ Lib/test/test_genericalias.py | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index ac2ee1faa4ae75..439f2418174686 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1446,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): @@ -2154,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): diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 057586e593c3d0..ac6f1e050dc77d 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -7,7 +7,7 @@ ) from collections.abc import * from contextlib import AbstractContextManager, AbstractAsyncContextManager -from ipaddress import _BaseNetwork +from ipaddress import IPv4Network, IPv4Interface, IPv6Network, IPv6Interface from os import DirEntry from re import Pattern, Match from types import GenericAlias, MappingProxyType @@ -36,8 +36,11 @@ def test_subscriptable(self): Mapping, MutableMapping, MappingView, KeysView, ItemsView, ValuesView, Sequence, MutableSequence, - MappingProxyType, DirEntry, _BaseNetwork - ): + MappingProxyType, + DirEntry, + IPv4Network, IPv4Interface, + IPv6Network, IPv6Interface + ): tname = t.__name__ with self.subTest(f"Testing {tname}"): alias = t[int] From 7839a181bd02c72b540b1f83626dce0c98df0e42 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 9 Apr 2020 20:21:09 -0700 Subject: [PATCH 3/3] Add trailing comma --- Lib/test/test_genericalias.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index ac6f1e050dc77d..9e5d5f36969b5f 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -39,7 +39,7 @@ def test_subscriptable(self): MappingProxyType, DirEntry, IPv4Network, IPv4Interface, - IPv6Network, IPv6Interface + IPv6Network, IPv6Interface, ): tname = t.__name__ with self.subTest(f"Testing {tname}"):