Skip to content

Commit

Permalink
fix mypy issues caused by motor type hints, bump dev dependencies (#40)
Browse files Browse the repository at this point in the history
* fix mypy issues caused by motor type hints

* drop py3.7

* add py3.12

* bump flake8
  • Loading branch information
ldruschk authored Oct 15, 2023
1 parent 282eaac commit 453917c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
name: python test
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True

plugins = pydantic.mypy

[mypy-tests.*]
disallow_untyped_calls = False
disallow_untyped_defs = False
Expand Down
4 changes: 2 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mypy==0.942
mypy==1.6.0
black==22.3.0
isort==5.10.1
flake8==4.0.1
flake8==6.1.0
coverage==6.3.2
pytest==7.1.2
pytest-asyncio==0.18.3
12 changes: 6 additions & 6 deletions enochecker3/chaindb.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from typing import Any
from typing import Any, Optional

from motor.motor_asyncio import AsyncIOMotorCollection
from motor.core import AgnosticCollection


class ChainDB:
def __init__(self, collection: AsyncIOMotorCollection, task_chain_id: str):
self.collection: AsyncIOMotorCollection = collection
def __init__(self, collection: AgnosticCollection, task_chain_id: str):
self.collection: AgnosticCollection = collection
self.task_chain_id: str = task_chain_id

async def get(self, key: str) -> Any:
val = await self.collection.find_one(
val: Optional[Any] = await self.collection.find_one(
{
"task_chain_id": self.task_chain_id,
"key": key,
}
)
) # type: ignore
if val is None:
raise KeyError(f"Key {key} not found")
return val["value"]
Expand Down
19 changes: 7 additions & 12 deletions enochecker3/enochecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
import pymongo
import uvicorn
from fastapi import FastAPI
from motor.motor_asyncio import (
AsyncIOMotorClient,
AsyncIOMotorCollection,
AsyncIOMotorDatabase,
)
from motor.core import AgnosticClient, AgnosticCollection, AgnosticDatabase
from motor.motor_asyncio import AsyncIOMotorClient

from enochecker3.logging import DebugFormatter, ELKFormatter
from enochecker3.utils import FlagSearcher
Expand Down Expand Up @@ -173,10 +170,10 @@ async def _init(self) -> None:
else:
connection_string = f"mongodb://{mongo_host}:{mongo_port}"

self._mongo: AsyncIOMotorClient = AsyncIOMotorClient(connection_string)
self._mongodb: AsyncIOMotorDatabase = self._mongo[self.checker_name]
self._mongo: AgnosticClient = AsyncIOMotorClient(connection_string)
self._mongodb: AgnosticDatabase = self._mongo[self.checker_name]

self._chain_collection: AsyncIOMotorCollection = self._mongodb["chain_db"]
self._chain_collection: AgnosticCollection = self._mongodb["chain_db"]

await self._chain_collection.create_index(
[("task_chain_id", pymongo.ASCENDING), ("key", pymongo.ASCENDING)],
Expand Down Expand Up @@ -409,12 +406,10 @@ def _get_http_client(self, task: BaseCheckerTaskMessage) -> httpx.AsyncClient:
def _get_chaindb(self, task: BaseCheckerTaskMessage) -> ChainDB:
return ChainDB(self._chain_collection, task.task_chain_id)

def _get_motor_collection(
self, task: BaseCheckerTaskMessage
) -> AsyncIOMotorCollection:
def _get_motor_collection(self, task: BaseCheckerTaskMessage) -> AgnosticCollection:
return self._mongodb[f"team_{task.team_id}"]

def _get_motor_database(self, task: BaseCheckerTaskMessage) -> AsyncIOMotorDatabase:
def _get_motor_database(self, task: BaseCheckerTaskMessage) -> AgnosticDatabase:
return self._mongodb

def _get_flag_searcher(self, task: ExploitCheckerTaskMessage) -> FlagSearcher:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
"License :: OSI Approved :: MIT License",
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
zip_safe=False, # This might be needed for requirements.txt
python_requires=">=3.7",
python_requires=">=3.8",
)
13 changes: 5 additions & 8 deletions tests/test_chaindb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import pymongo
import pytest
from motor.motor_asyncio import (
AsyncIOMotorClient,
AsyncIOMotorCollection,
AsyncIOMotorDatabase,
)
from motor.core import AgnosticClient, AgnosticCollection, AgnosticDatabase
from motor.motor_asyncio import AsyncIOMotorClient

from enochecker3 import ChainDB


@pytest.fixture
async def collection():
mongo: AsyncIOMotorClient = AsyncIOMotorClient()
db: AsyncIOMotorDatabase = mongo["enochecker3_tests"]
chain_collection: AsyncIOMotorCollection = db["chain_db"]
mongo: AgnosticClient = AsyncIOMotorClient()
db: AgnosticDatabase = mongo["enochecker3_tests"]
chain_collection: AgnosticCollection = db["chain_db"]

await chain_collection.create_index(
[("task_chain_id", pymongo.ASCENDING), ("key", pymongo.ASCENDING)],
Expand Down

0 comments on commit 453917c

Please sign in to comment.