Skip to content

Commit

Permalink
harmonize with mypy, fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rthalley committed Oct 14, 2024
1 parent f17a15f commit c3543f5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
7 changes: 2 additions & 5 deletions dns/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ def from_text(cls: Type[TIntEnum], text: str) -> TIntEnum:
if text.startswith(prefix) and text[len(prefix) :].isdigit():
value = int(text[len(prefix) :])
cls._check_value(value)
try:
return cls(value)
except ValueError:
return value # pyright: ignore
return cls(value)
raise cls._unknown_exception_class()

@classmethod
Expand Down Expand Up @@ -106,7 +103,7 @@ def _prefix(cls) -> str:
return ""

@classmethod
def _extra_from_text(cls, text) -> Optional[Any]: # pylint: disable=W0613
def _extra_from_text(cls, text: str) -> Optional[Any]: # pylint: disable=W0613
return None

@classmethod
Expand Down
14 changes: 7 additions & 7 deletions dns/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,14 +1427,14 @@ class _TextReader:

def __init__(
self,
text,
idna_codec,
one_rr_per_rrset=False,
origin=None,
relativize=True,
relativize_to=None,
text: str,
idna_codec: Optional[dns.name.IDNACodec],
one_rr_per_rrset: bool = False,
origin: Optional[dns.name.Name] = None,
relativize: bool = True,
relativize_to: Optional[dns.name.Name] = None,
):
self.message: Optional[Message] = None
self.message: Optional[Message] = None # mypy: ignore
self.tok = dns.tokenizer.Tokenizer(text, idna_codec=idna_codec)
self.last_name = None
self.one_rr_per_rrset = one_rr_per_rrset
Expand Down
4 changes: 2 additions & 2 deletions dns/rdtypes/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Gateway:

name = ""

def __init__(self, type, gateway: Optional[Union[str, dns.name.Name]] = None):
def __init__(self, type: Any, gateway: Optional[Union[str, dns.name.Name]] = None):
self.type = dns.rdata.Rdata._as_uint8(type)
self.gateway = gateway
self._check()
Expand Down Expand Up @@ -130,7 +130,7 @@ def __init__(self, windows: Optional[Iterable[Tuple[int, bytes]]] = None):
last_window = -1
if windows is None:
windows = []
self.windows = tuple(windows)
self.windows = windows
for window, bitmap in self.windows:
if not isinstance(window, int):
raise ValueError(f"bad {self.type_name} window type")
Expand Down
15 changes: 10 additions & 5 deletions dns/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

import dns.edns
import dns.exception
import dns.message
import dns.rdataclass
import dns.rdatatype
import dns.tsig

# Note we can't import dns.message for cicularity reasons

QUESTION = 0
ANSWER = 1
AUTHORITY = 2
Expand Down Expand Up @@ -218,7 +219,9 @@ def add_opt(self, opt, pad=0, opt_size=0, tsig_size=0):
pad = b""
options = list(opt_rdata.options)
options.append(dns.edns.GenericOption(dns.edns.OptionType.PADDING, pad))
opt = dns.message.Message._make_opt(ttl, opt_rdata.rdclass, options)
opt = dns.message.Message._make_opt( # pyright: ignore
ttl, opt_rdata.rdclass, options
)
self.was_padded = True
self.add_rrset(ADDITIONAL, opt)

Expand All @@ -228,7 +231,9 @@ def add_edns(self, edns, ednsflags, payload, options=None):
# make sure the EDNS version in ednsflags agrees with edns
ednsflags &= 0xFF00FFFF
ednsflags |= edns << 16
opt = dns.message.Message._make_opt(ednsflags, payload, options)
opt = dns.message.Message._make_opt( # pyright: ignore
ednsflags, payload, options
)
self.add_opt(opt)

def add_tsig(
Expand All @@ -250,7 +255,7 @@ def add_tsig(
key = secret
else:
key = dns.tsig.Key(keyname, secret, algorithm)
tsig = dns.message.Message._make_tsig(
tsig = dns.message.Message._make_tsig( # pyright: ignore
keyname, algorithm, 0, fudge, b"", id, tsig_error, other_data
)
(tsig, _) = dns.tsig.sign(s, key, tsig[0], int(time.time()), request_mac)
Expand Down Expand Up @@ -282,7 +287,7 @@ def add_multi_tsig(
key = secret
else:
key = dns.tsig.Key(keyname, secret, algorithm)
tsig = dns.message.Message._make_tsig(
tsig = dns.message.Message._make_tsig( # pyright: ignore
keyname, algorithm, 0, fudge, b"", id, tsig_error, other_data
)
(tsig, ctx) = dns.tsig.sign(
Expand Down
4 changes: 2 additions & 2 deletions dns/tsigkeyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import dns.tsig


def from_text(textring: Dict[str, Any]) -> Dict[dns.name.Name, dns.tsig.Key]:
def from_text(textring: Dict[str, Any]) -> Dict[dns.name.Name, Any]:
"""Convert a dictionary containing (textual DNS name, base64 secret)
pairs into a binary keyring which has (dns.name.Name, bytes) pairs, or
a dictionary containing (textual DNS name, (algorithm, base64 secret))
pairs into a binary keyring which has (dns.name.Name, dns.tsig.Key) pairs.
@rtype: dict"""

keyring = {}
keyring: Dict[dns.name.Name, Any] = {}
for name, value in textring.items():
kname = dns.name.from_text(name)
if isinstance(value, str):
Expand Down
8 changes: 4 additions & 4 deletions dns/zonefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,13 @@ class RRSetsReaderManager(dns.transaction.TransactionManager):
def __init__(
self,
origin: Optional[dns.name.Name] = dns.name.root,
relativize=False,
rdclass=dns.rdataclass.IN,
relativize: bool = False,
rdclass: dns.rdataclass.RdataClass = dns.rdataclass.IN,
):
self.origin = origin
self.relativize = relativize
self.rdclass = rdclass
self.rrsets = []
self.rrsets: List[dns.rrset.RRset] = []

def reader(self): # pragma: no cover
raise NotImplementedError
Expand All @@ -658,7 +658,7 @@ def origin_information(self):
effective = self.origin
return (self.origin, self.relativize, effective)

def set_rrsets(self, rrsets):
def set_rrsets(self, rrsets: List[dns.rrset.RRset]) -> None:
self.rrsets = rrsets


Expand Down

0 comments on commit c3543f5

Please sign in to comment.