python / python/cpython

`ipaddress`: `subnet_of()` and `supernet_of()` raise confusing `TypeError` wrapping an `AttributeError` when passed an Address object

Offen
#153,769 8 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

When calling subnet_of() or supernet_of() on an IPv4Network or IPv6Network and passing an address object (e.g., IPv6Address) instead of a network object, the module attempts to access the network_address attribute on the address object. This raises an AttributeError, which is then caught by the broader exception handling in _is_subnet_of, resulting in a confusing TypeError that completely masks the original attribute access failure.

The method should explicitly check that the other argument is a network object (or at least raise a clear TypeError stating that a network object is required) rather than failing internally with an AttributeError that gets swallowed into a generic containment error.

Repro:

import ipaddress
ipaddress.IPv6Network('::/0').subnet_of(ipaddress.IPv6Address('::1'))

Traceback:

AttributeError: 'IPv6Address' object has no attribute 'network_address'

During handling of the above exception, another exception occurred:

TypeError: Unable to test subnet containment between ::/0 and ::1

ipaddress_repro.py

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-154147

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

Beginne im Python-Modul ipaddress bei _is_subnet_of, das subnet_of() und supernet_of() verarbeitet, und reproduziere den gemeldeten Aufruf mit einem IPv6Address. Bestätige, dass die Übergabe einer Adresse einen klaren TypeError ausgibt, der besagt, dass ein Netzwerkobjekt erforderlich ist, anstatt des umschlossenen Containment-Fehlers.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
networking
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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