Improve typing and reformat code

This commit is contained in:
Alex Root Junior 2019-11-28 23:21:19 +02:00
parent c674b5547b
commit 562e339262
4 changed files with 20 additions and 17 deletions

View file

@ -53,11 +53,12 @@ clean:
rm -f `find . -type f -name '*.py[co]' ` rm -f `find . -type f -name '*.py[co]' `
rm -f `find . -type f -name '*~' ` rm -f `find . -type f -name '*~' `
rm -f `find . -type f -name '.*~' ` rm -f `find . -type f -name '.*~' `
rm -rf `find . -name .pytest_cache`
rm -rf *.egg-info rm -rf *.egg-info
rm -f .coverage rm -f .coverage
rm -f report.html rm -f report.html
rm -f .coverage.* rm -f .coverage.*
rm -rf {build,dist,site,.cache,.pytest_cache,.mypy_cache,reports} rm -rf {build,dist,site,.cache,.mypy_cache,reports}
# ================================================================================================= # =================================================================================================

View file

@ -1,7 +1,6 @@
from __future__ import annotations from __future__ import annotations
import copy from typing import Any, TypeVar
from typing import TypeVar, Dict, Any
from ...utils.mixins import ContextInstanceMixin from ...utils.mixins import ContextInstanceMixin
from ...utils.token import extract_bot_id, validate_token from ...utils.token import extract_bot_id, validate_token
@ -41,7 +40,7 @@ class BaseBot(ContextInstanceMixin):
def __hash__(self): def __hash__(self):
return hash(self.__token) return hash(self.__token)
def __eq__(self, other: BaseBot): def __eq__(self, other: Any) -> bool:
if not isinstance(other, BaseBot): if not isinstance(other, BaseBot):
return False return False
return hash(self) == hash(other) return hash(self) == hash(other)

View file

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import copy 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 from aiohttp import ClientSession, FormData
@ -61,12 +61,15 @@ class AiohttpSession(BaseSession):
await self.create_session() await self.create_session()
return self 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__ cls = self.__class__
result = cls.__new__(cls) result = cls.__new__(cls)
memodict[id(self)] = result memo[id(self)] = result
for key, value in self.__dict__.items(): for key, value in self.__dict__.items():
# aiohttp ClientSession cannot be copied. # 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) setattr(result, key, copied_value)
return result return result

View file

@ -2,23 +2,23 @@ from unittest.mock import patch
import pytest 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 BOT_ID = 123456789
VALID_TOKEN = '123456789:AABBCCDDEEFFaabbccddeeff-1234567890' VALID_TOKEN = "123456789:AABBCCDDEEFFaabbccddeeff-1234567890"
INVALID_TOKENS = [ INVALID_TOKENS = [
'123456789:AABBCCDDEEFFaabbccddeeff 123456789', # space is exists "123456789:AABBCCDDEEFFaabbccddeeff 123456789", # space is exists
'ABC:AABBCCDDEEFFaabbccddeeff123456789', # left part is not digit "ABC:AABBCCDDEEFFaabbccddeeff123456789", # left part is not digit
':AABBCCDDEEFFaabbccddeeff123456789', # there is no left part ":AABBCCDDEEFFaabbccddeeff123456789", # there is no left part
'123456789:', # there is no right part "123456789:", # there is no right part
'ABC AABBCCDDEEFFaabbccddeeff123456789', # there is no ':' separator "ABC AABBCCDDEEFFaabbccddeeff123456789", # there is no ':' separator
None, # is None None, # is None
12345678, # is digit 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): def invalid_token_fixture(request):
return request.param return request.param