From 39d80365637365493746421786a376a465257617 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 6 Jun 2023 22:16:26 -0400 Subject: [PATCH] fix mypy errors and remove typing --- mail4one/pop3.py | 13 ++++++------- mail4one/poputils.py | 18 +++++++----------- mail4one/smtp.py | 6 +++--- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/mail4one/pop3.py b/mail4one/pop3.py index 74b3e34..d391a29 100644 --- a/mail4one/pop3.py +++ b/mail4one/pop3.py @@ -6,7 +6,6 @@ import contextvars from dataclasses import dataclass from hashlib import sha256 from pathlib import Path -from typing import ClassVar, List, Set from .config import User from .pwhash import parse_hash, check_pass, PWInfo from asyncio import StreamReader, StreamWriter @@ -160,7 +159,7 @@ def trans_command_noop(_, __): write(ok("Hmm")) -async def process_transactions(mails_list: List[MailEntry]): +async def process_transactions(mails_list: list[MailEntry]): mails = MailList(mails_list) def reset(_, __): @@ -202,7 +201,7 @@ def get_deleted_items(deleted_items_path: Path): return set() -def save_deleted_items(deleted_items_path: Path, deleted_items: Set): +def save_deleted_items(deleted_items_path: Path, deleted_items: set[str]): with deleted_items_path.open(mode="w") as f: f.writelines(f"{did}\n" for did in deleted_items) @@ -271,14 +270,14 @@ class Config: self.loggedin_users: set[str] = set() -c_config = contextvars.ContextVar('config') +c_config: contextvars.ContextVar = contextvars.ContextVar('config') def config() -> Config: return c_config.get() -c_state = contextvars.ContextVar('state') +c_state: contextvars.ContextVar = contextvars.ContextVar('state') def state() -> State: @@ -295,7 +294,7 @@ def make_pop_server_callback(mails_path: Path, users: list[User], try: return await asyncio.wait_for(start_session(), timeout_seconds) finally: - stream_writer.close() + writer.close() return session_cb @@ -304,7 +303,7 @@ async def create_pop_server(host: str, port: int, mails_path: Path, users: list[User], - ssl_context: ssl.SSLContext = None, + ssl_context: ssl.SSLContext | None = None, timeout_seconds: int = 60): logging.info( f"Starting POP3 server {host=}, {port=}, {mails_path=}, {len(users)=}, {ssl_context != None=}, {timeout_seconds=}" diff --git a/mail4one/poputils.py b/mail4one/poputils.py index 22e55e8..9783819 100644 --- a/mail4one/poputils.py +++ b/mail4one/poputils.py @@ -2,7 +2,6 @@ import os from dataclasses import dataclass from enum import Enum, auto from pathlib import Path -from typing import NewType, List class ClientError(Exception): @@ -24,9 +23,6 @@ class AuthError(ClientError): pass -User = NewType('User', str) - - class Command(Enum): CAPA = auto() USER = auto() @@ -65,8 +61,8 @@ def err(arg): return f"-ERR {arg}\r\n".encode() -def parse_command(line: bytes) -> Request: - line = line.decode() +def parse_command(bline: bytes) -> Request: + line = bline.decode() if not line.endswith("\r\n"): raise ClientError("Invalid line ending") @@ -112,13 +108,13 @@ def files_in_path(path): return [] -def get_mails_list(dirpath: Path) -> List[MailEntry]: +def get_mails_list(dirpath: Path) -> list[MailEntry]: files = files_in_path(dirpath) entries = [MailEntry(filename, path) for filename, path in files] return entries -def set_nid(entries: List[MailEntry]): +def set_nid(entries: list[MailEntry]): entries.sort(reverse=True, key=lambda e: e.c_time) entries = sorted(entries, reverse=True, key=lambda e: e.c_time) for i, entry in enumerate(entries, start=1): @@ -131,11 +127,12 @@ def get_mail(entry: MailEntry) -> bytes: class MailList: - def __init__(self, entries: List[MailEntry]): + + def __init__(self, entries: list[MailEntry]): self.entries = entries set_nid(self.entries) self.mails_map = {str(e.nid): e for e in entries} - self.deleted_uids = set() + self.deleted_uids: set[str] = set() def delete(self, nid: str): self.deleted_uids.add(self.mails_map.pop(nid).uid) @@ -149,4 +146,3 @@ class MailList: def compute_stat(self): entries = self.get_all() return len(entries), sum(entry.size for entry in entries) - diff --git a/mail4one/smtp.py b/mail4one/smtp.py index c05986f..b16418e 100644 --- a/mail4one/smtp.py +++ b/mail4one/smtp.py @@ -32,7 +32,7 @@ class MailboxCRLF(Mailbox): self.mailbox = MaildirCRLF(mail_dir) -def protocol_factory_starttls(dirpath: Path, context: ssl.SSLContext = None): +def protocol_factory_starttls(dirpath: Path, context: ssl.SSLContext | None = None): logging.info("Got smtp client cb") try: handler = MailboxCRLF(dirpath) @@ -63,7 +63,7 @@ def protocol_factory(dirpath: Path): async def create_smtp_server_starttls(dirpath: Path, port: int, host="", - context: ssl.SSLContext = None): + context: ssl.SSLContext | None= None): loop = asyncio.get_event_loop() return await loop.create_server(partial(protocol_factory_starttls, dirpath, context), @@ -75,7 +75,7 @@ async def create_smtp_server_starttls(dirpath: Path, async def create_smtp_server_tls(dirpath: Path, port: int, host="", - context: ssl.SSLContext = None): + context: ssl.SSLContext | None= None): loop = asyncio.get_event_loop() return await loop.create_server(partial(protocol_factory, dirpath), host=host,