add types checker; fix typing errors

Этот коммит содержится в:
Vlad Pronsky 2024-01-05 18:26:36 +02:00
родитель d78e33d2cc
Коммит ae2b15dde5
7 изменённых файлов: 29 добавлений и 11 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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]

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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"]

Просмотреть файл

@ -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":

Просмотреть файл

@ -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