Skip to content

Commit

Permalink
Merge pull request #87 from simonsobs/dev
Browse files Browse the repository at this point in the history
Add type hints to all defs and enable `disallow_untyped_defs`
  • Loading branch information
TaiSakuma authored Sep 10, 2024
2 parents 6c817d3 + 79cc7b6 commit f46e70e
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 41 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ profile = "black"

[tool.mypy]
plugins = "strawberry.ext.mypy_plugin"
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = ["dynaconf.*", "async_asgi_testclient.*", "apluggy.*", "deepdiff.*"]
Expand Down
6 changes: 3 additions & 3 deletions src/nextline_schedule/auto/auto_mode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataclasses
from collections.abc import AsyncIterator, Awaitable, Callable
from typing import Literal
from typing import Any, Literal

from nextline import Nextline
from nextline.utils import pubsub
Expand Down Expand Up @@ -60,8 +60,8 @@ async def __aenter__(self) -> 'AutoMode':
await self._machine.__aenter__()
return self

async def __aexit__(self, exc_type, exc_value, traceback) -> None:
await self._machine.__aexit__(exc_type, exc_value, traceback)
async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
await self._machine.__aexit__(*args, **kwargs)
await self._pubsub_mode.aclose()

async def on_state_changed(self, state: str) -> None:
Expand Down
3 changes: 1 addition & 2 deletions src/nextline_schedule/auto/state_machine/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,5 @@ async def __aenter__(self) -> 'AutoModeStateMachine':
await self.start()
return self

async def __aexit__(self, exc_type, exc_value, traceback) -> None:
del exc_type, exc_value, traceback
async def __aexit__(self, *_: Any, **__: Any) -> None:
await self.close()
12 changes: 6 additions & 6 deletions src/nextline_schedule/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Mapping, MutableMapping
from collections.abc import AsyncIterator, Mapping, MutableMapping
from logging import getLogger
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -42,7 +42,7 @@ def dynaconf_validators(self) -> Optional[tuple[Validator, ...]]:
return VALIDATORS

@spec.hookimpl
def configure(self, settings: Dynaconf):
def configure(self, settings: Dynaconf) -> None:
logger = getLogger(__name__)
logger.info(f'{__package__} version: {__version__}')
api_rul = settings.schedule.api
Expand All @@ -56,19 +56,19 @@ def configure(self, settings: Dynaconf):
# self._scheduler = self._dummy

@spec.hookimpl
def schema(self):
def schema(self) -> tuple[type, type, type]:
return (Query, Mutation, Subscription)

@spec.hookimpl
@asynccontextmanager
async def lifespan(self, context: Mapping):
async def lifespan(self, context: Mapping) -> AsyncIterator[None]:
nextline = context['nextline']
self._queue = Queue()
self._auto_mode = AutoMode(
nextline=nextline, scheduler=self._scheduler, queue=self._queue
)
async with self._queue, self._auto_mode as y:
yield y
async with self._queue, self._auto_mode:
yield

@spec.hookimpl
def update_strawberry_context(self, context: MutableMapping) -> None:
Expand Down
6 changes: 3 additions & 3 deletions src/nextline_schedule/queue/queue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import AsyncIterator, Iterable
from logging import getLogger
from typing import Optional
from typing import Any, Optional

from nextline.utils import pubsub

Expand All @@ -26,7 +26,7 @@ async def __call__(self) -> str:
return item.script

@property
def items(self):
def items(self) -> list[QueueItem]:
return self._pubsub.latest()

def subscribe(self) -> AsyncIterator[list[QueueItem]]:
Expand Down Expand Up @@ -84,5 +84,5 @@ async def __aenter__(self) -> 'Queue':
await self._pubsub.publish(list(self._queue.items))
return self

async def __aexit__(self, *_, **__) -> None:
async def __aexit__(self, *_: Any, **__: Any) -> None:
await self.aclose()
4 changes: 3 additions & 1 deletion src/nextline_schedule/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


class Scheduler:
def __init__(self, api_url: str, length_minutes: int, policy: str, timeout=5.0):
def __init__(
self, api_url: str, length_minutes: int, policy: str, timeout: float = 5.0
):
self._api_url = api_url
self._length_minutes = length_minutes
self._policy = policy
Expand Down
2 changes: 1 addition & 1 deletion tests/auto/test_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ async def test_one() -> None:
assert expected == await states


async def subscribe_state(auto_mode: AutoMode):
async def subscribe_state(auto_mode: AutoMode) -> list[str]:
return [state async for state in auto_mode.subscribe_state()]
18 changes: 10 additions & 8 deletions tests/auto/test_auto_on_raised.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio
import time
from collections.abc import Callable
from typing import NoReturn

from nextline import Nextline

from nextline_schedule.auto import AutoMode, AutoModeStateMachine
from nextline_schedule.auto import AutoMode

STATEMENT_QUEUE = '''
"""queue"""
Expand All @@ -20,17 +22,17 @@ class MockError(Exception):
pass


def f():
def f() -> None:
time.sleep(0.001)


def g():
def g() -> NoReturn:
time.sleep(0.001)
raise MockError()


async def test_on_raised_while_pulling():
async def pull():
async def test_on_raised_while_pulling() -> None:
async def pull() -> NoReturn:
raise MockError()

run_no = 1
Expand All @@ -50,8 +52,8 @@ async def pull():
assert expected == await states


async def test_on_raised_while_running():
async def pull():
async def test_on_raised_while_running() -> None:
async def pull() -> Callable[[], None]:
return g

run_no = 1
Expand All @@ -71,5 +73,5 @@ async def pull():
assert expected == await states


async def subscribe_state(auto_mode: AutoModeStateMachine):
async def subscribe_state(auto_mode: AutoMode) -> list[str]:
return [state async for state in auto_mode.subscribe_state()]
11 changes: 6 additions & 5 deletions tests/auto/test_auto_turn_off.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import asyncio
import time
from collections.abc import Callable

from nextline import Nextline

from nextline_schedule.auto import AutoMode, AutoModeStateMachine
from nextline_schedule.auto import AutoMode

STATEMENT_QUEUE = '''
"""queue"""
Expand All @@ -16,15 +17,15 @@ async def mock_queue() -> str:
return STATEMENT_QUEUE


def f():
def f() -> None:
time.sleep(0.001)


async def pull():
async def pull() -> Callable[[], None]:
return f


async def test_turn_off_while_waiting():
async def test_turn_off_while_waiting() -> None:
run_no = 1
nextline = Nextline(
statement=f,
Expand Down Expand Up @@ -54,5 +55,5 @@ async def test_turn_off_while_waiting():
assert expected == await states


async def subscribe_state(auto_mode: AutoModeStateMachine):
async def subscribe_state(auto_mode: AutoMode) -> list[str]:
return [state async for state in auto_mode.subscribe_state()]
4 changes: 2 additions & 2 deletions tests/queue/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@given(st.data())
async def test_queue(data: st.DataObject):
async def test_queue(data: st.DataObject) -> None:
queue = data.draw(st_queue())

async def subscribe() -> list[list[QueueItem]]:
Expand Down Expand Up @@ -45,7 +45,7 @@ async def subscribe() -> list[list[QueueItem]]:
await queue.push(arg)
case 'remove', _:
ids = [i.id for i in queue.items]
ids.append(st.integers(min_value=0))
ids.append(data.draw(st.integers(min_value=0)))
id = data.draw(st.sampled_from(ids))
success = await queue.remove(id)
if not success:
Expand Down
8 changes: 4 additions & 4 deletions tests/queue/test_queue_imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@given(queue=st_queue_imp())
def test_queue(queue: QueueImp):
def test_queue(queue: QueueImp) -> None:
# __len__
assert len(queue) == len(queue.items)

Expand All @@ -18,7 +18,7 @@ def test_queue(queue: QueueImp):


@given(queue=st_queue_imp(), push_arg=st_push_arg())
def test_push(queue: QueueImp, push_arg: PushArg):
def test_push(queue: QueueImp, push_arg: PushArg) -> None:
initial_len = len(queue)
item = queue.push(push_arg)
assert item.script == push_arg.script
Expand All @@ -27,7 +27,7 @@ def test_push(queue: QueueImp, push_arg: PushArg):


@given(queue=st_queue_imp())
def test_pop(queue: QueueImp):
def test_pop(queue: QueueImp) -> None:
initial_len = len(queue)
if initial_len == 0:
assert queue.pop() is None
Expand All @@ -38,7 +38,7 @@ def test_pop(queue: QueueImp):


@given(queue=st_queue_imp())
def test_remove(queue: QueueImp):
def test_remove(queue: QueueImp) -> None:
initial_len = len(queue)
if initial_len == 0:
assert not queue.remove(0)
Expand Down
2 changes: 1 addition & 1 deletion tests/schema/queries/test_queue_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@given(queue=st_queue())
async def test_schema(queue: Queue, schema: Schema):
async def test_schema(queue: Queue, schema: Schema) -> None:
async with queue:
context_schedule = {'queue': queue}
context = {'schedule': context_schedule}
Expand Down
10 changes: 6 additions & 4 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def client() -> AsyncIterator[TestClient]:
yield y


async def test_plugin(client: TestClient):
async def test_plugin(client: TestClient) -> None:
turned_on = asyncio.Event()
task = asyncio.create_task(subscribe_auto_mode_state(client, turned_on))

Expand Down Expand Up @@ -62,7 +62,9 @@ async def test_plugin(client: TestClient):
await task


async def subscribe_auto_mode_state(client: TestClient, turned_on: asyncio.Event):
async def subscribe_auto_mode_state(
client: TestClient, turned_on: asyncio.Event
) -> list[str]:
await turned_on.wait()
ret = []
async for data in gql_subscribe(client, SUBSCRIBE_AUTO_MODE_STATE):
Expand All @@ -74,7 +76,7 @@ async def subscribe_auto_mode_state(client: TestClient, turned_on: asyncio.Event


@pytest.fixture(autouse=True)
def configure_by_envvar(monkeypatch: pytest.MonkeyPatch):
def configure_by_envvar(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv('NEXTLINE_SCHEDULE__API', 'https://example.com')
monkeypatch.setenv('NEXTLINE_SCHEDULE__LENGTH_MINUTES', '60')
monkeypatch.setenv('NEXTLINE_SCHEDULE__POLICY', 'test')
Expand All @@ -87,5 +89,5 @@ def configure_by_envvar(monkeypatch: pytest.MonkeyPatch):


@pytest.fixture(autouse=True)
def mock_httpx(httpx_mock: HTTPXMock):
def mock_httpx(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(json={'commands': COMMANDS})
2 changes: 1 addition & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nextline_schedule


def test_version():
def test_version() -> None:
'''Confirm that the version string is attached to the module'''
nextline_schedule.__version__

0 comments on commit f46e70e

Please sign in to comment.