python / python/cpython

Improving conciseness of ipaddress factory functions.

未關閉
#93,880 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Suggestion to refactor the 3 factory functions ip_address, ip_network, ip_interface.
In addition making a similar modification in the _prefix_from_ip_string function.

Currently the functions contain code which can be improved in terms of conciseness and extendability.

  • try/catch containing a pass statement can be substituted with contextlib.suppress making the code more concise
  • we can use a for loop which will iterate over the class objects to further increase conciseness

Current factory function implementations (docstrings not included for brevity)

def ip_address(address):
    try:
        return IPv4Address(address)
    except (AddressValueError, NetmaskValueError):
        pass

    try:
        return IPv6Address(address)
    except (AddressValueError, NetmaskValueError):
        pass

    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')


def ip_network(address, strict=True):
    try:
        return IPv4Network(address, strict)
    except (AddressValueError, NetmaskValueError):
        pass

    try:
        return IPv6Network(address, strict)
    except (AddressValueError, NetmaskValueError):
        pass

    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 network')


def ip_interface(address):
    try:
        return IPv4Interface(address)
    except (AddressValueError, NetmaskValueError):
        pass

    try:
        return IPv6Interface(address)
    except (AddressValueError, NetmaskValueError):
        pass

    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 interface')

Proposed function implementations

def ip_address(address):
    for ip_class in [IPv4Address, IPv6Address]:
        with contextlib.suppress(AddressValueError, NetmaskValueError):
            return ip_class(address)

    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')


def ip_network(address, strict=True):
    for network_class in [IPv4Network, IPv6Network]:
        with contextlib.suppress(AddressValueError, NetmaskValueError):
            return network_class(address, strict)

    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 network')


def ip_interface(address):
    for ip_interface_class in [IPv4Interface, IPv6Interface]:
        with contextlib.suppress(AddressValueError, NetmaskValueError):
            return ip_interface_class(address)

    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 interface')

current _prefix_from_ip_string function (docstrings and comments not included for brevity)

    @classmethod
    def _prefix_from_ip_string(cls, ip_str):
        try:
            ip_int = cls._ip_int_from_string(ip_str)
        except AddressValueError:
            cls._report_invalid_netmask(ip_str)

        try:
            return cls._prefix_from_ip_int(ip_int)
        except ValueError:
            pass

        ip_int ^= cls._ALL_ONES
        try:
            return cls._prefix_from_ip_int(ip_int)
        except ValueError:
            cls._report_invalid_netmask(ip_str)

Suggested improvement

    @classmethod
    def _prefix_from_ip_string(cls, ip_str):
        try:
            ip_int = cls._ip_int_from_string(ip_str)
        except AddressValueError:
            cls._report_invalid_netmask(ip_str)

        with contextlib.suppress(ValueError):
            return cls._prefix_from_ip_int(ip_int)

        ip_int ^= cls._ALL_ONES
        try:
            return cls._prefix_from_ip_int(ip_int)
        except ValueError:
            cls._report_invalid_netmask(ip_str)

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

在 ipaddress 實作中定位 ip_address、ip_network、ip_interface 和 _prefix_from_ip_string 進入點,然後閱讀其周圍的例外處理。將提議的迴圈和抑制變更與目前行為進行比較,包括現有的錯誤路徑,並驗證相關的 ipaddress 測試仍然通過。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
networking
Issue 類型
重構
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。