Skip to content

Commit

Permalink
fix: Fix the QA job by skipping some checks and fix others. (#308)
Browse files Browse the repository at this point in the history
* fix: Fix the QA job by skipping some checks and fix others.

* Remove the unit test failures

* Fix/ignore typing failures
  • Loading branch information
maxking committed Oct 31, 2022
1 parent 215b854 commit e0fb197
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 63 deletions.
16 changes: 6 additions & 10 deletions aiosmtpd/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class BaseController(metaclass=ABCMeta):
def __init__(
self,
handler: Any,
loop: asyncio.AbstractEventLoop = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
*,
ssl_context: Optional[ssl.SSLContext] = None,
# SMTP parameters
Expand Down Expand Up @@ -184,7 +184,7 @@ def cancel_tasks(self, stop_loop: bool = True):
try:
_all_tasks = asyncio.all_tasks # pytype: disable=module-attr
except AttributeError: # pragma: py-gt-36
_all_tasks = asyncio.Task.all_tasks
_all_tasks = asyncio.Task.all_tasks # pytype: disable=attribute-error
for task in _all_tasks(self.loop):
# This needs to be invoked in a thread-safe way
task.cancel()
Expand All @@ -198,7 +198,7 @@ class BaseThreadedController(BaseController, metaclass=ABCMeta):
def __init__(
self,
handler: Any,
loop: asyncio.AbstractEventLoop = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
*,
ready_timeout: float = DEFAULT_READY_TIMEOUT,
ssl_context: Optional[ssl.SSLContext] = None,
Expand Down Expand Up @@ -322,7 +322,7 @@ class BaseUnthreadedController(BaseController, metaclass=ABCMeta):
def __init__(
self,
handler: Any,
loop: asyncio.AbstractEventLoop = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
*,
ssl_context: Optional[ssl.SSLContext] = None,
# SMTP parameters
Expand Down Expand Up @@ -388,7 +388,7 @@ def __init__(
handler: Any,
hostname: Optional[str] = None,
port: int = 8025,
loop: asyncio.AbstractEventLoop = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs,
):
super().__init__(
Expand Down Expand Up @@ -435,7 +435,7 @@ def __init__(
self,
handler: Any,
unix_socket: Union[str, Path],
loop: asyncio.AbstractEventLoop = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
**kwargs,
):
super().__init__(
Expand Down Expand Up @@ -497,13 +497,9 @@ def _trigger_server(self): # pragma: no-unixsock
class UnthreadedController(InetMixin, BaseUnthreadedController):
"""Provides an unthreaded controller that listens on an INET endpoint"""

pass


@public
class UnixSocketUnthreadedController( # pragma: no-unixsock
UnixSocketMixin, BaseUnthreadedController
):
"""Provides an unthreaded controller that listens on a Unix Socket file"""

pass
22 changes: 13 additions & 9 deletions aiosmtpd/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from argparse import ArgumentParser
from email.message import Message as Em_Message
from email.parser import BytesParser, Parser
from typing import AnyStr, Dict, List, Tuple, Type, TypeVar
from typing import Any, AnyStr, List, Type, TypeVar, Optional

from public import public

Expand Down Expand Up @@ -54,7 +54,7 @@ def message_from_string(s, *args, **kws):

@public
class Debugging:
def __init__(self, stream: io.TextIOBase = None):
def __init__(self, stream: Optional[io.TextIOBase] = None):
self.stream = sys.stdout if stream is None else stream

@classmethod
Expand Down Expand Up @@ -140,13 +140,13 @@ async def handle_DATA(

def _deliver(
self, mail_from: AnyStr, rcpt_tos: List[AnyStr], data: AnyStr
) -> Dict[str, Tuple[int, bytes]]:
) -> Any :
refused = {}
try:
s = smtplib.SMTP()
s.connect(self._hostname, self._port)
try:
refused = s.sendmail(mail_from, rcpt_tos, data)
refused = s.sendmail(mail_from, rcpt_tos, data) # pytype: disable=wrong-arg-types # noqa: E501
finally:
s.quit()
except smtplib.SMTPRecipientsRefused as e:
Expand All @@ -158,7 +158,7 @@ def _deliver(
# error code, use it. Otherwise, fake it with a non-triggering
# exception code.
errcode = getattr(e, "smtp_code", -1)
errmsg = getattr(e, "smtp_error", "ignore")
errmsg = getattr(e, "smtp_error", b"ignore")
for r in rcpt_tos:
refused[r] = (errcode, errmsg)
return refused
Expand All @@ -175,7 +175,7 @@ def from_cli(cls: Type[T], parser: ArgumentParser, *args) -> T:

@public
class Message(metaclass=ABCMeta):
def __init__(self, message_class: Type[Em_Message] = None):
def __init__(self, message_class: Optional[Type[Em_Message]] = None):
self.message_class = message_class

async def handle_DATA(
Expand Down Expand Up @@ -213,9 +213,9 @@ def handle_message(self, message: Em_Message) -> None:
class AsyncMessage(Message, metaclass=ABCMeta):
def __init__(
self,
message_class: Type[Em_Message] = None,
message_class: Optional[Type[Em_Message]] = None,
*,
loop: asyncio.AbstractEventLoop = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
):
super().__init__(message_class)
self.loop = loop or asyncio.get_event_loop()
Expand All @@ -234,7 +234,11 @@ async def handle_message(self, message: Em_Message) -> None:

@public
class Mailbox(Message):
def __init__(self, mail_dir: os.PathLike, message_class: Type[Em_Message] = None):
def __init__(
self,
mail_dir: os.PathLike,
message_class: Optional[Type[Em_Message]] = None,
):
self.mailbox = mailbox.Maildir(mail_dir)
self.mail_dir = mail_dir
super().__init__(message_class)
Expand Down
4 changes: 2 additions & 2 deletions aiosmtpd/proxy_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from enum import IntEnum
from functools import partial
from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import Any, AnyStr, ByteString, Dict, Optional, Tuple, Union
from typing import Any, ByteString, Dict, Optional, Tuple, Union

import attr
from public import public
Expand Down Expand Up @@ -87,7 +87,7 @@ class PROTO(IntEnum):

# region #### Custom Types ############################################################

EndpointAddress = Union[IPv4Address, IPv6Address, AnyStr]
EndpointAddress = Union[IPv4Address, IPv6Address, Union[str, bytes]]


@public
Expand Down
33 changes: 19 additions & 14 deletions aiosmtpd/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def make_loop() -> asyncio.AbstractEventLoop:

@public
def syntax(
text: str, extended: str = None, when: Optional[str] = None
text: str, extended: Optional[str] = None, when: Optional[str] = None
) -> DecoratorType:
"""
A @decorator that provides helptext for (E)SMTP HELP.
Expand Down Expand Up @@ -318,19 +318,19 @@ def __init__(
data_size_limit: int = DATA_SIZE_DEFAULT,
enable_SMTPUTF8: bool = False,
decode_data: bool = False,
hostname: str = None,
ident: str = None,
hostname: Optional[str] = None,
ident: Optional[str] = None,
tls_context: Optional[ssl.SSLContext] = None,
require_starttls: bool = False,
timeout: float = 300,
auth_required: bool = False,
auth_require_tls: bool = True,
auth_exclude_mechanism: Optional[Iterable[str]] = None,
auth_callback: AuthCallbackType = None,
auth_callback: Optional[AuthCallbackType] = None,
command_call_limit: Union[int, Dict[str, int], None] = None,
authenticator: AuthenticatorType = None,
authenticator: Optional[AuthenticatorType] = None,
proxy_protocol_timeout: Optional[Union[int, float]] = None,
loop: asyncio.AbstractEventLoop = None
loop: Optional[asyncio.AbstractEventLoop] = None
):
self.__ident__ = ident or __ident__
self.loop = loop if loop else make_loop()
Expand Down Expand Up @@ -492,7 +492,7 @@ def __del__(self): # pragma: nocover
if closed.done() and not closed.cancelled():
closed.exception()

def connection_made(self, transport: asyncio.transports.Transport) -> None:
def connection_made(self, transport: asyncio.transports.BaseTransport) -> None:
# Reset state due to rfc3207 part 4.2.
self._set_rset_state()
self.session = self._create_session()
Expand Down Expand Up @@ -545,7 +545,7 @@ def eof_received(self) -> bool:
return False
return super().eof_received()

def _reset_timeout(self, duration: float = None) -> None:
def _reset_timeout(self, duration: Optional[float] = None) -> None:
if self._timeout_handle is not None:
self._timeout_handle.cancel()
self._timeout_handle = self.loop.call_later(
Expand Down Expand Up @@ -766,10 +766,16 @@ async def _handle_client(self):
status = '500 Error: Cannot describe error'
finally:
if isinstance(error, TLSSetupException):
self.transport.close()
# This code branch is inside None check for self.transport
# so there shouldn't be a None self.transport but pytype
# still complains, so silence that error.
self.transport.close() # pytype: disable=attribute-error
self.connection_lost(error)
else:
await self.push(status)
# The value of status is being set with ex-except and it
# shouldn't be None, but pytype isn't able to infer that
# so ignore the error related to wrong argument types.
await self.push(status) # pytype: disable=wrong-arg-types

async def check_helo_needed(self, helo: str = "HELO") -> bool:
"""
Expand Down Expand Up @@ -816,10 +822,9 @@ async def smtp_EHLO(self, hostname: str):
await self.push('501 Syntax: EHLO hostname')
return

response = []
response = ['250-' + self.hostname, ]
self._set_rset_state()
self.session.extended_smtp = True
response.append('250-' + self.hostname)
if self.data_size_limit:
response.append(f'250-SIZE {self.data_size_limit}')
self.command_size_limits['MAIL'] += 26
Expand Down Expand Up @@ -1010,7 +1015,7 @@ async def challenge_auth(
challenge = b"334 " + (b64encode(challenge) if encode_to_b64 else challenge)
log.debug("%r << challenge: %r", self.session.peer, challenge)
await self.push(challenge)
line = await self._reader.readline()
line = await self._reader.readline() # pytype: disable=attribute-error
if log_client_response:
warn("AUTH interaction logging is enabled!")
warn("Sensitive information might be leaked!")
Expand Down Expand Up @@ -1097,7 +1102,7 @@ async def auth_PLAIN(self, _, args: List[str]) -> AuthResult:
# login data is "{authz_id}\x00{login_id}\x00{password}"
# authz_id can be null, and currently ignored
# See https://tools.ietf.org/html/rfc4616#page-3
_, login, password = login_and_password.split(b"\x00")
_, login, password = login_and_password.split(b"\x00") # pytype: disable=attribute-error # noqa: E501
except ValueError: # not enough args
await self.push("501 5.5.2 Can't split auth value")
return AuthResult(success=False, handled=True)
Expand Down

0 comments on commit e0fb197

Please sign in to comment.