python / python/cpython

Improving conciseness of ipaddress factory functions.

Offen
#93,880 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lokalisieren Sie die Einstiegspunkte ip_address, ip_network, ip_interface und _prefix_from_ip_string in der ipaddress-Implementierung und lesen Sie anschließend deren umgebende Ausnahmebehandlung. Vergleichen Sie die vorgeschlagenen Änderungen an Schleife und Unterdrückung mit dem aktuellen Verhalten, einschließlich der bestehenden Fehlerpfade, und überprüfen Sie, dass die relevanten ipaddress-Tests weiterhin erfolgreich sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
networking
Issue-Typ
Refactoring
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.