11from __future__ import annotations
22
3- from typing import cast
3+ from typing import TYPE_CHECKING , cast
44
55import dns .asyncresolver
66import dns .resolver
77from dns .rdatatype import RdataType
8- from dns .rdtypes .IN .A import A as ARecordAnswer
9- from dns .rdtypes .IN .SRV import SRV as SRVRecordAnswer # noqa: N811 # constant imported as non constant (it's class)
8+
9+ if TYPE_CHECKING :
10+ from dns .rdtypes .IN .A import A as ARecordAnswer
11+ from dns .rdtypes .IN .SRV import SRV as SRVRecordAnswer # noqa: N811 # constant imported as non constant (it's class)
1012
1113
1214def resolve_a_record (hostname : str , lifetime : float | None = None ) -> str :
13- """Perform a DNS resolution for an A record to given hostname
15+ """Perform a DNS resolution for an A record to given hostname.
1416
1517 :param hostname: The address to resolve for.
1618 :return: The resolved IP address from the A record
@@ -22,7 +24,7 @@ def resolve_a_record(hostname: str, lifetime: float | None = None) -> str:
2224 answers = dns .resolver .resolve (hostname , RdataType .A , lifetime = lifetime , search = True )
2325 # There should only be one answer here, though in case the server
2426 # does actually point to multiple IPs, we just pick the first one
25- answer = cast (ARecordAnswer , answers [0 ])
27+ answer = cast (" ARecordAnswer" , answers [0 ])
2628 ip = str (answer ).rstrip ("." )
2729 return ip
2830
@@ -35,7 +37,7 @@ async def async_resolve_a_record(hostname: str, lifetime: float | None = None) -
3537 answers = await dns .asyncresolver .resolve (hostname , RdataType .A , lifetime = lifetime , search = True )
3638 # There should only be one answer here, though in case the server
3739 # does actually point to multiple IPs, we just pick the first one
38- answer = cast (ARecordAnswer , answers [0 ])
40+ answer = cast (" ARecordAnswer" , answers [0 ])
3941 ip = str (answer ).rstrip ("." )
4042 return ip
4143
@@ -53,7 +55,7 @@ def resolve_srv_record(query_name: str, lifetime: float | None = None) -> tuple[
5355 answers = dns .resolver .resolve (query_name , RdataType .SRV , lifetime = lifetime , search = True )
5456 # There should only be one answer here, though in case the server
5557 # does actually point to multiple IPs, we just pick the first one
56- answer = cast (SRVRecordAnswer , answers [0 ])
58+ answer = cast (" SRVRecordAnswer" , answers [0 ])
5759 host = str (answer .target ).rstrip ("." )
5860 port = int (answer .port )
5961 return host , port
@@ -67,7 +69,7 @@ async def async_resolve_srv_record(query_name: str, lifetime: float | None = Non
6769 answers = await dns .asyncresolver .resolve (query_name , RdataType .SRV , lifetime = lifetime , search = True )
6870 # There should only be one answer here, though in case the server
6971 # does actually point to multiple IPs, we just pick the first one
70- answer = cast (SRVRecordAnswer , answers [0 ])
72+ answer = cast (" SRVRecordAnswer" , answers [0 ])
7173 host = str (answer .target ).rstrip ("." )
7274 port = int (answer .port )
7375 return host , port
0 commit comments