From ae2b15dde55a7682593c8c049751b953c4f3b388 Mon Sep 17 00:00:00 2001 From: Vlad Pronsky Date: Fri, 5 Jan 2024 18:26:36 +0200 Subject: [PATCH] add types checker; fix typing errors --- Makefile | 5 +++++ pyproject.toml | 5 ++--- tests/conftest.py | 4 ++-- tests/test_parser.py | 14 +++++++++++--- tests/test_queue_client.py | 4 ++++ twscrape/cli.py | 2 +- twscrape/queue_client.py | 6 ++++-- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 57b98bf..808b92a 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,15 @@ lint: @ruff check --select I --fix . @ruff format . @ruff check . + @pyright . test: @pytest -s --cov=twscrape tests/ +check: + @make lint + @make test + test-cov: @pytest -s --cov=twscrape tests/ @coverage html diff --git a/pyproject.toml b/pyproject.toml index f03a15f..a310f98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,11 +27,12 @@ dependencies = [ [project.optional-dependencies] dev = [ + "pyright>=1.1.344", "pytest-asyncio>=0.23.3", "pytest-cov>=4.1.0", "pytest-httpx>=0.28.0", "pytest>=7.4.4", - "ruff>=0.1.11" + "ruff>=0.1.11", ] [project.urls] @@ -58,5 +59,3 @@ line-length = 99 [tool.ruff.lint] ignore = ["E501"] - -[tool.ruff.format] \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 33bfac9..34aa97c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,9 +6,9 @@ from twscrape.queue_client import QueueClient @pytest.fixture -def pool_mock(tmp_path) -> AccountsPool: +def pool_mock(tmp_path): db_path = tmp_path / "test.db" - yield AccountsPool(db_path) # type: ignore + yield AccountsPool(db_path) @pytest.fixture diff --git a/tests/test_parser.py b/tests/test_parser.py index 4ff9f6a..416a51f 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -12,6 +12,16 @@ os.makedirs(DATA_DIR, exist_ok=True) set_log_level("DEBUG") +class FakeRep: + text: str + + def __init__(self, text: str): + self.text = text + + def json(self): + return json.loads(self.text) + + def load_mock(name: str): file = os.path.join(os.path.dirname(__file__), f"mocked-data/{name}.json") with open(file) as f: @@ -28,9 +38,7 @@ def fake_rep(fn: str, filename: str): with open(filename) as fp: data = fp.read() - rep = lambda: None # noqa: E731 - rep.text = data - rep.json = lambda: json.loads(data) + rep = FakeRep(data) return rep diff --git a/tests/test_queue_client.py b/tests/test_queue_client.py index 3e8385a..ae318a8 100644 --- a/tests/test_queue_client.py +++ b/tests/test_queue_client.py @@ -57,6 +57,7 @@ async def test_do_not_switch_account_on_200(httpx_mock: HTTPXMock, client_fixtur for x in range(1): httpx_mock.add_response(url=URL, json={"foo": x}, status_code=200) rep = await client.get(URL) + assert rep is not None assert rep.json() == {"foo": x} # account should not be switched @@ -82,6 +83,7 @@ async def test_switch_acc_on_http_error(httpx_mock: HTTPXMock, client_fixture: C httpx_mock.add_response(url=URL, json={"foo": "2"}, status_code=200) rep = await client.get(URL) + assert rep is not None assert rep.json() == {"foo": "2"} locked2 = await get_locked(pool) @@ -107,6 +109,7 @@ async def test_retry_with_same_acc_on_network_error(httpx_mock: HTTPXMock, clien httpx_mock.add_response(url=URL, json={"foo": "2"}, status_code=200) rep = await client.get(URL) + assert rep is not None assert rep.json() == {"foo": "2"} locked2 = await get_locked(pool) @@ -141,6 +144,7 @@ async def test_ctx_closed_on_break(httpx_mock: HTTPXMock, client_fixture: CF): elif before_ctx is not None: assert before_ctx == c.ctx + assert rep is not None assert rep.json() == {"counter": counter} yield rep.json()["counter"] diff --git a/twscrape/cli.py b/twscrape/cli.py index b841ca7..6870cca 100644 --- a/twscrape/cli.py +++ b/twscrape/cli.py @@ -53,7 +53,7 @@ async def main(args): api = API(pool, debug=args.debug) if args.command == "accounts": - print_table(await pool.accounts_info()) + print_table([dict(x) for x in await pool.accounts_info()]) return if args.command == "stats": diff --git a/twscrape/queue_client.py b/twscrape/queue_client.py index ef84544..c1c1a5b 100644 --- a/twscrape/queue_client.py +++ b/twscrape/queue_client.py @@ -96,11 +96,14 @@ class QueueClient: await self.pool.unlock(ctx.acc.username, self.queue, ctx.req_count) - async def _get_ctx(self) -> Ctx: + async def _get_ctx(self): if self.ctx: return self.ctx acc = await self.pool.get_for_queue_or_wait(self.queue) + if acc is None: + return None + clt = acc.make_client() self.ctx = Ctx(acc, clt) return self.ctx @@ -129,7 +132,6 @@ class QueueClient: err_msg = "; ".join(list(err_msg)) log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}" - print(log_msg) logger.trace(log_msg) # for dev: need to add some features in api.py