From 562e33926247cbeca68be10555cac61a1a4be4b4 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Thu, 28 Nov 2019 23:21:19 +0200 Subject: [PATCH] Improve typing and reformat code --- Makefile | 3 ++- aiogram/api/client/base.py | 5 ++--- aiogram/api/client/session/aiohttp.py | 11 +++++++---- tests/test_utils/test_token.py | 18 +++++++++--------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 9fd2341f..e1375ec3 100644 --- a/Makefile +++ b/Makefile @@ -53,11 +53,12 @@ clean: rm -f `find . -type f -name '*.py[co]' ` rm -f `find . -type f -name '*~' ` rm -f `find . -type f -name '.*~' ` + rm -rf `find . -name .pytest_cache` rm -rf *.egg-info rm -f .coverage rm -f report.html rm -f .coverage.* - rm -rf {build,dist,site,.cache,.pytest_cache,.mypy_cache,reports} + rm -rf {build,dist,site,.cache,.mypy_cache,reports} # ================================================================================================= diff --git a/aiogram/api/client/base.py b/aiogram/api/client/base.py index 0e2516a0..22cd5183 100644 --- a/aiogram/api/client/base.py +++ b/aiogram/api/client/base.py @@ -1,7 +1,6 @@ from __future__ import annotations -import copy -from typing import TypeVar, Dict, Any +from typing import Any, TypeVar from ...utils.mixins import ContextInstanceMixin from ...utils.token import extract_bot_id, validate_token @@ -41,7 +40,7 @@ class BaseBot(ContextInstanceMixin): def __hash__(self): return hash(self.__token) - def __eq__(self, other: BaseBot): + def __eq__(self, other: Any) -> bool: if not isinstance(other, BaseBot): return False return hash(self) == hash(other) diff --git a/aiogram/api/client/session/aiohttp.py b/aiogram/api/client/session/aiohttp.py index 442a8bd8..2122d6e3 100644 --- a/aiogram/api/client/session/aiohttp.py +++ b/aiogram/api/client/session/aiohttp.py @@ -1,7 +1,7 @@ from __future__ import annotations import copy -from typing import Callable, Optional, TypeVar, cast, Dict, Any +from typing import Any, Callable, Dict, Optional, TypeVar, cast from aiohttp import ClientSession, FormData @@ -61,12 +61,15 @@ class AiohttpSession(BaseSession): await self.create_session() return self - def __deepcopy__(self, memodict: Dict[str, Any]): + def __deepcopy__(self: T, memo: Optional[Dict[int, Any]] = None) -> T: + if memo is None: + memo = {} + cls = self.__class__ result = cls.__new__(cls) - memodict[id(self)] = result + memo[id(self)] = result for key, value in self.__dict__.items(): # aiohttp ClientSession cannot be copied. - copied_value = copy.deepcopy(value, memo=memodict) if key != '_session' else None + copied_value = copy.deepcopy(value, memo=memo) if key != "_session" else None setattr(result, key, copied_value) return result diff --git a/tests/test_utils/test_token.py b/tests/test_utils/test_token.py index 9b4f419e..3cb55519 100644 --- a/tests/test_utils/test_token.py +++ b/tests/test_utils/test_token.py @@ -2,23 +2,23 @@ from unittest.mock import patch import pytest -from aiogram.utils.token import TokenValidationError, validate_token, extract_bot_id +from aiogram.utils.token import TokenValidationError, extract_bot_id, validate_token BOT_ID = 123456789 -VALID_TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' +VALID_TOKEN = "123456789:AABBCCDDEEFFaabbccddeeff-1234567890" INVALID_TOKENS = [ - '123456789:AABBCCDDEEFFaabbccddeeff 123456789', # space is exists - 'ABC:AABBCCDDEEFFaabbccddeeff123456789', # left part is not digit - ':AABBCCDDEEFFaabbccddeeff123456789', # there is no left part - '123456789:', # there is no right part - 'ABC AABBCCDDEEFFaabbccddeeff123456789', # there is no ':' separator + "123456789:AABBCCDDEEFFaabbccddeeff 123456789", # space is exists + "ABC:AABBCCDDEEFFaabbccddeeff123456789", # left part is not digit + ":AABBCCDDEEFFaabbccddeeff123456789", # there is no left part + "123456789:", # there is no right part + "ABC AABBCCDDEEFFaabbccddeeff123456789", # there is no ':' separator None, # is None 12345678, # is digit - (42, 'TEST'), # is tuple + (42, "TEST"), # is tuple ] -@pytest.fixture(params=INVALID_TOKENS, name='invalid_token') +@pytest.fixture(params=INVALID_TOKENS, name="invalid_token") def invalid_token_fixture(request): return request.param