Skip to content

Restore negative integer indexing of RRsets and resolver answers - #1361

Merged
rthalley merged 1 commit into
rthalley:mainfrom
AryanRG:fix/rrset-negative-integer-index-20261017-0244
Oct 17, 2026
Merged

rthalley merged 1 commit into
rthalley:mainfrom
AryanRG:fix/rrset-negative-integer-index-20261017-0244

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

RRset[-1] and Answer[-1] currently raise ValueError instead of returning the last record in their iteration order. Negative integer deletion fails through the same path, and out-of-range integer indices leak StopIteration or ValueError instead of IndexError.

For example, on main (ff7c868439ba8f8e27590bf6cc5dc5ff0d6a485a, 2.10.0dev0), this fails without any DNS query:

import dns.rrset

records = dns.rrset.from_text(
    "example.", 300, "IN", "A", "192.0.2.1", "192.0.2.2"
)
assert records[-1].address == "192.0.2.2"

Normalize negative integer indices and check bounds in Set.__getitem__ before calling islice. RRset and Answer indexing/deletion inherit the fix. This restores the integer behavior of the earlier list-backed implementation while retaining dictionary storage and streaming lookup. The tests cover positive/negative positions, empty/nonempty bounds, huge integers, deletion without mutation on invalid indices, and Answer forwarding through a deterministic wire round trip.

Validation on macOS ARM64, CPython 3.13.12:

  • Red before: the new regressions against the unchanged base runtime report 19 failures (18 failed subtests and one Answer test), with 4 passed methods and 3 passed subtests.
  • Green after: NO_INTERNET=1 python -m pytest -q --cov=dns.set --cov-branch --cov-report=term-missing reports 1,503 passed, 123 skipped, 98 subtests passed. dns/set.py has 100% statement and branch coverage.
  • Black passes on all three changed files; Ruff and Pyright pass on dns; Ty passes on dns/set.py; git diff --check passes.
  • Whole-tree Black still fails on the same two unchanged baseline files, dns/entropy.py and tests/test_entropy.py. Whole-tree Ty still reports the same three public_cls mutable-override errors in dnssecalgs and one unused-ignore warning in dns/rdtypes/rrsigbase.py; pristine and patched Ty output is byte-identical.

The 123 skips include external-network tests. This validation does not establish live DNS behavior, hosted CI success, or other-platform/Python-version results. The fix is scoped to integer scalar indexing and bounds; it makes no broader slice or index-protocol compatibility claim and adds no DNS preference-order guarantee.

AI assistance was used to prepare the implementation and regression tests.

Independent validation of the exact patch reproduced the failures against the base and ran the patched offline suite and additional scalar probes.

@rthalley
rthalley merged commit 1185be6 into rthalley:main Oct 17, 2026
rthalley pushed a commit that referenced this pull request Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants