Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
67 changes: 65 additions & 2 deletions aiodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Callable,
Literal,
Optional,
TypedDict,
TypeVar,
Union,
overload,
Expand Down Expand Up @@ -56,14 +57,76 @@
'ANY': pycares.QUERY_CLASS_ANY,
}

if sys.version_info >= (3, 11):
from typing import NotRequired, Unpack

class DNSResolverKwargs(TypedDict):
flags: NotRequired[int]
timeout: NotRequired[float]
tries: NotRequired[int]
ndots: NotRequired[int]
tcp_port: NotRequired[int]
udp_port: NotRequired[int]
domains: NotRequired[Sequence[str]]
lookups: NotRequired[str]
socket_send_buffer_size: NotRequired[int]
socket_receive_buffer_size: NotRequired[int]
rotate: NotRequired[bool]
local_ip: NotRequired[str]
local_dev: NotRequired[str]
resolvconf_path: NotRequired[str]


class DNSResolver:
@overload
def __init__(
self,
nameservers: Optional[Sequence[str]] = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
) -> None: ...
Comment on lines +81 to +86
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this overload for? The below ones are compatible with it, so this appears redundant.

Copy link
Contributor Author

@Vizonex Vizonex Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overload requires that it is used twice there was no way I could get around this one.

Copy link
Contributor Author

@Vizonex Vizonex Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dreamsorcerer I did have the idea of merging all the arguments together and merging it like this do you think that change might be better? This way we can get rid of some unwanted arguments like socket_state_cb and on the bonus-side of things type hints in this case are retained and nothing tedious has to be done the only thing would be passing these arguments in but then I could get rid of the overloads and Unpack variables all together.

class DNSResolver:
 
    def __init__(
        self,
        nameservers: Optional[Sequence[str]] = None,
        loop: Optional[asyncio.AbstractEventLoop] = None,
        *,
        flags: Optional[int] = None,
        timeout: Optional[float] = None,
        tries: Optional[int] = None,
        ndots: Optional[int] = None,
        tcp_port: Optional[int] = None,
        udp_port: Optional[int] = None,
        domains: Optional[Sequence[str]] = None,
        lookups: Optional[str] = None,
        socket_send_buffer_size: Optional[int] = None,
        socket_receive_buffer_size: Optional[int] = None,
        rotate: bool = False,
        local_ip: Optional[str] = None,
        local_dev: Optional[str] = None,
        resolvconf_path: Optional[str] = None,
    ) -> None:
        self._closed = True
        self.loop = loop or asyncio.get_event_loop()
        if TYPE_CHECKING:
            assert self.loop is not None
        
        
        self._timeout = timeout
        self._event_thread, self._channel = self._make_channel(
            flags=flags,
            tries=tries,
            ndots=ndots,
            tcp_port=tcp_port,
            udp_port=udp_port,
            domains=domains,
            lookups=lookups,
            socket_send_buffer_size=socket_send_buffer_size,
            socket_receive_buffer_size=socket_receive_buffer_size,
            rotate=rotate,
            local_ip=local_ip,
            local_dev=local_dev,
            resolvconf_path=resolvconf_path
        )


if sys.version_info >= (3, 11):

@overload
def __init__(
self,
nameservers: Optional[Sequence[str]] = ...,
loop: Optional[asyncio.AbstractEventLoop] = ...,
**kwargs: Unpack[DNSResolverKwargs],
) -> None: ...

# Reserve backwards compatability for older versions
# of Python
else:

@overload
def __init__(
self,
nameservers: Optional[Sequence[str]] = ...,
loop: Optional[asyncio.AbstractEventLoop] = ...,
*,
flags: Optional[int] = None,
timeout: Optional[float] = None,
tries: Optional[int] = None,
ndots: Optional[int] = None,
tcp_port: Optional[int] = None,
udp_port: Optional[int] = None,
domains: Optional[Sequence[str]] = None,
lookups: Optional[str] = None,
socket_send_buffer_size: Optional[int] = None,
socket_receive_buffer_size: Optional[int] = None,
rotate: bool = False,
local_ip: Optional[str] = None,
local_dev: Optional[str] = None,
resolvconf_path: Optional[str] = None,
) -> None: ...

def __init__(
self,
nameservers: Optional[Sequence[str]] = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs: Any,
) -> None: # TODO(PY311): Use Unpack for kwargs.
) -> None:
self._closed = True
self.loop = loop or asyncio.get_event_loop()
if TYPE_CHECKING:
Expand Down Expand Up @@ -143,7 +206,7 @@ def _callback(

def _get_future_callback(
self,
) -> tuple['asyncio.Future[_T]', Callable[[_T, int], None]]:
) -> tuple[asyncio.Future[_T], Callable[[_T, int], None]]:
"""Return a future and a callback to set the result of the future."""
cb: Callable[[_T, int], None]
future: asyncio.Future[_T] = self.loop.create_future()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_aiodns.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def setUp(self) -> None:
)
self.loop = asyncio.new_event_loop()
self.addCleanup(self.loop.close)
self.resolver = aiodns.DNSResolver(loop=self.loop, timeout=5.0)
self.resolver = aiodns.DNSResolver(
loop=self.loop, timeout=5.0
) # type[aiodns.DNSResolver | None]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Also, annotations shouldn't be defined with comments anymore.

self.resolver.nameservers = ['8.8.8.8']

def tearDown(self) -> None:
Expand Down
Loading