Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dns/dnssec.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def _sign(

signature = signing_key.sign(data, verify, deterministic)

return cast(RRSIG, rrsig_template.replace(signature=signature))
return rrsig_template.replace(signature=signature)


def _make_rrsig_signature_data(
Expand Down
12 changes: 5 additions & 7 deletions dns/dnssecalgs/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod # pylint: disable=no-name-in-module
from typing import Any
from typing import Any, Self

import dns.rdataclass
import dns.rdatatype
Expand Down Expand Up @@ -42,12 +42,12 @@ def to_dnskey(self, flags: int = Flag.ZONE, protocol: int = 3) -> DNSKEY:

@classmethod
@abstractmethod
def from_dnskey(cls, key: DNSKEY) -> "GenericPublicKey":
def from_dnskey(cls, key: DNSKEY) -> Self:
"""Create public key from DNSKEY"""

@classmethod
@abstractmethod
def from_pem(cls, public_pem: bytes) -> "GenericPublicKey":
def from_pem(cls, public_pem: bytes) -> Self:
"""Create public key from PEM-encoded SubjectPublicKeyInfo as specified
in RFC 5280"""

Expand All @@ -74,14 +74,12 @@ def sign(
"""Sign DNSSEC data"""

@abstractmethod
def public_key(self) -> "GenericPublicKey":
def public_key(self) -> GenericPublicKey:
"""Return public key instance"""

@classmethod
@abstractmethod
def from_pem(
cls, private_pem: bytes, password: bytes | None = None
) -> "GenericPrivateKey":
def from_pem(cls, private_pem: bytes, password: bytes | None = None) -> Self:
"""Create private key from PEM-encoded PKCS#8"""

@abstractmethod
Expand Down
8 changes: 3 additions & 5 deletions dns/dnssecalgs/cryptography.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Self

from cryptography.hazmat.primitives import serialization

Expand All @@ -20,7 +20,7 @@ def __init__(self, key: Any) -> None: # pylint: disable=super-init-not-called
self.key = key

@classmethod
def from_pem(cls, public_pem: bytes) -> GenericPublicKey:
def from_pem(cls, public_pem: bytes) -> Self:
key = serialization.load_pem_public_key(public_pem)
return cls(key=key)

Expand Down Expand Up @@ -48,9 +48,7 @@ def public_key(self) -> GenericPublicKey:
return self.public_cls(key=self.key.public_key())

@classmethod
def from_pem(
cls, private_pem: bytes, password: bytes | None = None
) -> GenericPrivateKey:
def from_pem(cls, private_pem: bytes, password: bytes | None = None) -> Self:
key = serialization.load_pem_private_key(private_pem, password=password)
return cls(key=key)

Expand Down
5 changes: 3 additions & 2 deletions dns/dnssecalgs/dsa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import struct
from typing import Self

from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -44,7 +45,7 @@ def encode_key_bytes(self) -> bytes:
return res

@classmethod
def from_dnskey(cls, key: DNSKEY) -> "PublicDSA":
def from_dnskey(cls, key: DNSKEY) -> Self:
cls._ensure_algorithm_key_combination(key)
keyptr = key.key
if len(keyptr) < 1:
Expand Down Expand Up @@ -102,7 +103,7 @@ def sign(
return signature

@classmethod
def generate(cls, key_size: int) -> "PrivateDSA":
def generate(cls, key_size: int) -> Self:
return cls(
key=dsa.generate_private_key(key_size=key_size),
)
Expand Down
6 changes: 4 additions & 2 deletions dns/dnssecalgs/ecdsa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Self

from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
Expand Down Expand Up @@ -34,7 +36,7 @@ def encode_key_bytes(self) -> bytes:
return pn.x.to_bytes(self.octets, "big") + pn.y.to_bytes(self.octets, "big")

@classmethod
def from_dnskey(cls, key: DNSKEY) -> "PublicECDSA":
def from_dnskey(cls, key: DNSKEY) -> Self:
cls._ensure_algorithm_key_combination(key)
if len(key.key) < cls.octets * 2:
raise ValueError("truncated ECDSA key material")
Expand Down Expand Up @@ -77,7 +79,7 @@ def sign(
return signature

@classmethod
def generate(cls) -> "PrivateECDSA":
def generate(cls) -> Self:
return cls(
key=ec.generate_private_key(
curve=cls.public_cls.curve, backend=default_backend() # type: ignore
Expand Down
6 changes: 4 additions & 2 deletions dns/dnssecalgs/eddsa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Self

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ed448, ed25519

Expand All @@ -17,7 +19,7 @@ def encode_key_bytes(self) -> bytes:
)

@classmethod
def from_dnskey(cls, key: DNSKEY) -> "PublicEDDSA":
def from_dnskey(cls, key: DNSKEY) -> Self:
cls._ensure_algorithm_key_combination(key)
return cls(
key=cls.key_cls.from_public_bytes(key.key),
Expand All @@ -39,7 +41,7 @@ def sign(
return signature

@classmethod
def generate(cls) -> "PrivateEDDSA":
def generate(cls) -> Self:
return cls(key=cls.key_cls.generate())


Expand Down
6 changes: 4 additions & 2 deletions dns/dnssecalgs/mldsa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Self

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import mldsa

Expand All @@ -17,7 +19,7 @@ def encode_key_bytes(self) -> bytes:
)

@classmethod
def from_dnskey(cls, key: DNSKEY) -> "PublicMLDSA":
def from_dnskey(cls, key: DNSKEY) -> Self:
cls._ensure_algorithm_key_combination(key)
return cls(
key=cls.key_cls.from_public_bytes(key.key),
Expand All @@ -39,7 +41,7 @@ def sign(
return signature

@classmethod
def generate(cls) -> "PrivateMLDSA":
def generate(cls) -> Self:
return cls(key=cls.key_cls.generate())


Expand Down
5 changes: 3 additions & 2 deletions dns/dnssecalgs/rsa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import struct
from typing import Self

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
Expand Down Expand Up @@ -33,7 +34,7 @@ def encode_key_bytes(self) -> bytes:
return exp_header + exp + pn.n.to_bytes((pn.n.bit_length() + 7) // 8, "big")

@classmethod
def from_dnskey(cls, key: DNSKEY) -> "PublicRSA":
def from_dnskey(cls, key: DNSKEY) -> Self:
cls._ensure_algorithm_key_combination(key)
keyptr = key.key
if len(keyptr) < 1:
Expand Down Expand Up @@ -77,7 +78,7 @@ def sign(
return signature

@classmethod
def generate(cls, key_size: int) -> "PrivateRSA":
def generate(cls, key_size: int) -> Self:
return cls(
key=rsa.generate_private_key(
public_exponent=cls.default_public_exponent,
Expand Down
4 changes: 2 additions & 2 deletions dns/edns.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import math
import socket
import struct
from typing import Any
from typing import Any, Self

import dns.enum
import dns.inet
Expand Down Expand Up @@ -203,7 +203,7 @@ def to_wire(self, file: Any | None = None) -> bytes | None:
def to_text(self) -> str:
return f"Generic {self.otype}"

def to_generic(self) -> "GenericOption":
def to_generic(self) -> Self:
return self

@classmethod
Expand Down
10 changes: 4 additions & 6 deletions dns/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import enum
from typing import Any, TypeVar
from typing import Any, Self

import dns._text_util

TIntEnum = TypeVar("TIntEnum", bound="IntEnum")


class IntEnum(enum.IntEnum):
@classmethod
Expand All @@ -42,7 +40,7 @@ def _check_value(cls, value):
raise ValueError(f"{name} must be an int between >= 0 and <= {max}")

@classmethod
def from_text(cls: type[TIntEnum], text: str) -> TIntEnum:
def from_text(cls: type[Self], text: str) -> Self:
text = text.upper()
try:
return cls[text]
Expand All @@ -61,7 +59,7 @@ def from_text(cls: type[TIntEnum], text: str) -> TIntEnum:
raise cls._unknown_exception_class()

@classmethod
def to_text(cls: type[TIntEnum], value: int) -> str:
def to_text(cls: type[Self], value: int) -> str:
cls._check_value(value)
try:
text = cls(value).name
Expand All @@ -73,7 +71,7 @@ def to_text(cls: type[TIntEnum], value: int) -> str:
return text

@classmethod
def make(cls: type[TIntEnum], value: int | str) -> TIntEnum:
def make(cls: type[Self], value: int | str) -> Self:
"""Convert text or a value into an enumerated type, if possible.

:param value: The value to convert.
Expand Down
10 changes: 5 additions & 5 deletions dns/rdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import itertools
import random
from importlib import import_module
from typing import Any
from typing import Any, Self

import dns.exception
import dns.immutable
Expand Down Expand Up @@ -541,7 +541,7 @@ def from_text(
origin: dns.name.Name | None = None,
relativize: bool = True,
relativize_to: dns.name.Name | None = None,
) -> "Rdata":
) -> Self:
raise NotImplementedError # pragma: no cover

@classmethod
Expand All @@ -551,10 +551,10 @@ def from_wire_parser(
rdtype: dns.rdatatype.RdataType,
parser: dns.wire.Parser,
origin: dns.name.Name | None = None,
) -> "Rdata":
) -> Self:
raise NotImplementedError # pragma: no cover

def replace(self, **kwargs: Any) -> "Rdata":
def replace(self, **kwargs: Any) -> Self:
"""Create a new Rdata instance based on the instance replace was
invoked on. It is possible to pass different parameters to
override the corresponding properties of the base Rdata.
Expand Down Expand Up @@ -782,7 +782,7 @@ def from_text(
def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
file.write(self.data)

def to_generic(self, origin: dns.name.Name | None = None) -> "GenericRdata":
def to_generic(self, origin: dns.name.Name | None = None) -> Self:
return self

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions dns/rdtypes/ANY/AMTRELAY.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import struct
from typing import Self

import dns.exception
import dns.immutable
Expand Down Expand Up @@ -59,7 +60,7 @@ def to_styled_text(self, style: dns.rdata.RdataStyle) -> str:
@classmethod
def from_text(
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
) -> "AMTRELAY":
) -> Self:
precedence = tok.get_uint8()
discovery_optional = tok.get_uint8()
if discovery_optional > 1:
Expand All @@ -80,7 +81,7 @@ def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
Relay(self.relay_type, self.relay).to_wire(file, compress, origin, canonicalize)

@classmethod
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None) -> "AMTRELAY":
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None) -> Self:
precedence, relay_type = parser.get_struct("!BB")
discovery_optional = bool(relay_type >> 7)
relay_type &= 0x7F
Expand Down
5 changes: 3 additions & 2 deletions dns/rdtypes/ANY/CAA.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import struct
from typing import Self

import dns.exception
import dns.immutable
Expand Down Expand Up @@ -45,7 +46,7 @@ def to_styled_text(self, style: dns.rdata.RdataStyle) -> str:
@classmethod
def from_text(
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
) -> "CAA":
) -> Self:
flags = tok.get_uint8()
tag = tok.get_bytes(max_length=255)
value = tok.get_bytes()
Expand All @@ -60,7 +61,7 @@ def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
file.write(self.value)

@classmethod
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None) -> "CAA":
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None) -> Self:
flags = parser.get_uint8()
tag = parser.get_counted_bytes()
value = parser.get_remaining()
Expand Down
5 changes: 3 additions & 2 deletions dns/rdtypes/ANY/SOA.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import struct
from typing import Self

import dns.exception
import dns.immutable
Expand Down Expand Up @@ -51,7 +52,7 @@ def to_styled_text(self, style: dns.rdata.RdataStyle) -> str:
@classmethod
def from_text(
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
) -> "SOA":
) -> Self:
mname = tok.get_name(origin, relativize, relativize_to)
rname = tok.get_name(origin, relativize, relativize_to)
serial = tok.get_uint32()
Expand All @@ -72,7 +73,7 @@ def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
file.write(five_ints)

@classmethod
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None) -> "SOA":
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None) -> Self:
mname = parser.get_name(origin, True)
rname = parser.get_name(origin, True)
return cls(rdclass, rdtype, mname, rname, *parser.get_struct("!IIIII"))
Loading
Loading