зеркало из
https://github.com/viginum-datalab/twscrape.git
synced 2025-10-29 05:04:22 +02:00
add types checker; fix typing errors
Этот коммит содержится в:
родитель
d78e33d2cc
Коммит
ae2b15dde5
5
Makefile
5
Makefile
@ -12,10 +12,15 @@ lint:
|
|||||||
@ruff check --select I --fix .
|
@ruff check --select I --fix .
|
||||||
@ruff format .
|
@ruff format .
|
||||||
@ruff check .
|
@ruff check .
|
||||||
|
@pyright .
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@pytest -s --cov=twscrape tests/
|
@pytest -s --cov=twscrape tests/
|
||||||
|
|
||||||
|
check:
|
||||||
|
@make lint
|
||||||
|
@make test
|
||||||
|
|
||||||
test-cov:
|
test-cov:
|
||||||
@pytest -s --cov=twscrape tests/
|
@pytest -s --cov=twscrape tests/
|
||||||
@coverage html
|
@coverage html
|
||||||
|
|||||||
@ -27,11 +27,12 @@ dependencies = [
|
|||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
dev = [
|
dev = [
|
||||||
|
"pyright>=1.1.344",
|
||||||
"pytest-asyncio>=0.23.3",
|
"pytest-asyncio>=0.23.3",
|
||||||
"pytest-cov>=4.1.0",
|
"pytest-cov>=4.1.0",
|
||||||
"pytest-httpx>=0.28.0",
|
"pytest-httpx>=0.28.0",
|
||||||
"pytest>=7.4.4",
|
"pytest>=7.4.4",
|
||||||
"ruff>=0.1.11"
|
"ruff>=0.1.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
@ -58,5 +59,3 @@ line-length = 99
|
|||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
ignore = ["E501"]
|
ignore = ["E501"]
|
||||||
|
|
||||||
[tool.ruff.format]
|
|
||||||
@ -6,9 +6,9 @@ from twscrape.queue_client import QueueClient
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def pool_mock(tmp_path) -> AccountsPool:
|
def pool_mock(tmp_path):
|
||||||
db_path = tmp_path / "test.db"
|
db_path = tmp_path / "test.db"
|
||||||
yield AccountsPool(db_path) # type: ignore
|
yield AccountsPool(db_path)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@ -12,6 +12,16 @@ os.makedirs(DATA_DIR, exist_ok=True)
|
|||||||
set_log_level("DEBUG")
|
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):
|
def load_mock(name: str):
|
||||||
file = os.path.join(os.path.dirname(__file__), f"mocked-data/{name}.json")
|
file = os.path.join(os.path.dirname(__file__), f"mocked-data/{name}.json")
|
||||||
with open(file) as f:
|
with open(file) as f:
|
||||||
@ -28,9 +38,7 @@ def fake_rep(fn: str, filename: str):
|
|||||||
with open(filename) as fp:
|
with open(filename) as fp:
|
||||||
data = fp.read()
|
data = fp.read()
|
||||||
|
|
||||||
rep = lambda: None # noqa: E731
|
rep = FakeRep(data)
|
||||||
rep.text = data
|
|
||||||
rep.json = lambda: json.loads(data)
|
|
||||||
return rep
|
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):
|
for x in range(1):
|
||||||
httpx_mock.add_response(url=URL, json={"foo": x}, status_code=200)
|
httpx_mock.add_response(url=URL, json={"foo": x}, status_code=200)
|
||||||
rep = await client.get(URL)
|
rep = await client.get(URL)
|
||||||
|
assert rep is not None
|
||||||
assert rep.json() == {"foo": x}
|
assert rep.json() == {"foo": x}
|
||||||
|
|
||||||
# account should not be switched
|
# 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)
|
httpx_mock.add_response(url=URL, json={"foo": "2"}, status_code=200)
|
||||||
|
|
||||||
rep = await client.get(URL)
|
rep = await client.get(URL)
|
||||||
|
assert rep is not None
|
||||||
assert rep.json() == {"foo": "2"}
|
assert rep.json() == {"foo": "2"}
|
||||||
|
|
||||||
locked2 = await get_locked(pool)
|
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)
|
httpx_mock.add_response(url=URL, json={"foo": "2"}, status_code=200)
|
||||||
|
|
||||||
rep = await client.get(URL)
|
rep = await client.get(URL)
|
||||||
|
assert rep is not None
|
||||||
assert rep.json() == {"foo": "2"}
|
assert rep.json() == {"foo": "2"}
|
||||||
|
|
||||||
locked2 = await get_locked(pool)
|
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:
|
elif before_ctx is not None:
|
||||||
assert before_ctx == c.ctx
|
assert before_ctx == c.ctx
|
||||||
|
|
||||||
|
assert rep is not None
|
||||||
assert rep.json() == {"counter": counter}
|
assert rep.json() == {"counter": counter}
|
||||||
yield rep.json()["counter"]
|
yield rep.json()["counter"]
|
||||||
|
|
||||||
|
|||||||
@ -53,7 +53,7 @@ async def main(args):
|
|||||||
api = API(pool, debug=args.debug)
|
api = API(pool, debug=args.debug)
|
||||||
|
|
||||||
if args.command == "accounts":
|
if args.command == "accounts":
|
||||||
print_table(await pool.accounts_info())
|
print_table([dict(x) for x in await pool.accounts_info()])
|
||||||
return
|
return
|
||||||
|
|
||||||
if args.command == "stats":
|
if args.command == "stats":
|
||||||
|
|||||||
@ -96,11 +96,14 @@ class QueueClient:
|
|||||||
|
|
||||||
await self.pool.unlock(ctx.acc.username, self.queue, ctx.req_count)
|
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:
|
if self.ctx:
|
||||||
return self.ctx
|
return self.ctx
|
||||||
|
|
||||||
acc = await self.pool.get_for_queue_or_wait(self.queue)
|
acc = await self.pool.get_for_queue_or_wait(self.queue)
|
||||||
|
if acc is None:
|
||||||
|
return None
|
||||||
|
|
||||||
clt = acc.make_client()
|
clt = acc.make_client()
|
||||||
self.ctx = Ctx(acc, clt)
|
self.ctx = Ctx(acc, clt)
|
||||||
return self.ctx
|
return self.ctx
|
||||||
@ -129,7 +132,6 @@ class QueueClient:
|
|||||||
err_msg = "; ".join(list(err_msg))
|
err_msg = "; ".join(list(err_msg))
|
||||||
|
|
||||||
log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}"
|
log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}"
|
||||||
print(log_msg)
|
|
||||||
logger.trace(log_msg)
|
logger.trace(log_msg)
|
||||||
|
|
||||||
# for dev: need to add some features in api.py
|
# for dev: need to add some features in api.py
|
||||||
|
|||||||
Загрузка…
x
Ссылка в новой задаче
Block a user