From: Bob Halley Date: Tue, 10 Sep 2024 15:10:36 +0000 (-0700) Subject: The return type of Rdata.to_wire() is Optional[bytes] not bytes. (#1129) X-Git-Tag: v2.7.0rc1~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2007e434299c693919ac988fca4ad9ef923276f6;p=thirdparty%2Fdnspython.git The return type of Rdata.to_wire() is Optional[bytes] not bytes. (#1129) * The return type of Rdata.to_wire() is Optional[bytes] not bytes. [#1128] --- diff --git a/dns/dnssec.py b/dns/dnssec.py index 9b164775..77edb202 100644 --- a/dns/dnssec.py +++ b/dns/dnssec.py @@ -118,6 +118,7 @@ def key_id(key: Union[DNSKEY, CDNSKEY]) -> int: """ rdata = key.to_wire() + assert rdata is not None # for mypy if key.algorithm == Algorithm.RSAMD5: return (rdata[-3] << 8) + rdata[-2] else: @@ -245,9 +246,10 @@ def make_ds( if isinstance(name, str): name = dns.name.from_text(name, origin) wire = name.canonicalize().to_wire() - assert wire is not None + kwire = key.to_wire(origin=origin) + assert wire is not None and kwire is not None # for mypy dshash.update(wire) - dshash.update(key.to_wire(origin=origin)) + dshash.update(kwire) digest = dshash.digest() dsrdata = struct.pack("!HBB", key_id(key), key.algorithm, algorithm) + digest @@ -634,7 +636,9 @@ def _make_rrsig_signature_data( rrname, rdataset = _get_rrname_rdataset(rrset) data = b"" - data += rrsig.to_wire(origin=signer)[:18] + wire = rrsig.to_wire(origin=signer) + assert wire is not None # for mypy + data += wire[:18] data += rrsig.signer.to_digestable(signer) # Derelativize the name before considering labels. diff --git a/dns/rdata.py b/dns/rdata.py index 5f9d4713..8099c26a 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -214,7 +214,7 @@ class Rdata: compress: Optional[dns.name.CompressType] = None, origin: Optional[dns.name.Name] = None, canonicalize: bool = False, - ) -> bytes: + ) -> None: raise NotImplementedError # pragma: no cover def to_wire( @@ -223,14 +223,19 @@ class Rdata: compress: Optional[dns.name.CompressType] = None, origin: Optional[dns.name.Name] = None, canonicalize: bool = False, - ) -> bytes: + ) -> Optional[bytes]: """Convert an rdata to wire format. - Returns a ``bytes`` or ``None``. + Returns a ``bytes`` if no output file was specified, or ``None`` otherwise. """ if file: - return self._to_wire(file, compress, origin, canonicalize) + # We call _to_wire() and then return None explicitly instead of + # of just returning the None from _to_wire() as mypy's func-returns-value + # unhelpfully errors out with "error: "_to_wire" of "Rdata" does not return + # a value (it only ever returns None)" + self._to_wire(file, compress, origin, canonicalize) + return None else: f = io.BytesIO() self._to_wire(f, compress, origin, canonicalize) @@ -253,8 +258,9 @@ class Rdata: Returns a ``bytes``. """ - - return self.to_wire(origin=origin, canonicalize=True) + wire = self.to_wire(origin=origin, canonicalize=True) + assert wire is not None # for mypy + return wire def __repr__(self): covers = self.covers()