Skip to content

Commit

Permalink
More type annotation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed May 21, 2022
1 parent 41b6236 commit 2e8006e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion examples/tutorial1/tests/test_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def test_client_and_server(
event_loop: AbstractEventLoop, capsys: CaptureFixture
event_loop: AbstractEventLoop, capsys: CaptureFixture[str]
) -> None:
async def run() -> None:
async with Context() as ctx:
Expand Down
9 changes: 5 additions & 4 deletions src/asphalt/core/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
from typing import Any, Dict, Optional

import click
from ruamel.yaml import YAML
from ruamel.yaml import YAML, ScalarNode
from ruamel.yaml.loader import Loader

from asphalt.core.runner import policies, run_application
from asphalt.core.utils import merge_config, qualified_name


def env_constructor(loader, node):
def env_constructor(loader: Loader, node: ScalarNode) -> str | None:
value = loader.construct_scalar(node)
return os.getenv(value)


def text_file_constructor(loader, node):
def text_file_constructor(loader: Loader, node: ScalarNode) -> str:
value = loader.construct_scalar(node)
return Path(value).read_text()


def binary_file_constructor(loader, node):
def binary_file_constructor(loader: Loader, node: ScalarNode) -> bytes:
value = loader.construct_scalar(node)
return Path(value).read_bytes()

Expand Down
3 changes: 2 additions & 1 deletion src/asphalt/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
import sys
from abc import ABCMeta, abstractmethod
from asyncio import Future
from collections import OrderedDict
from traceback import print_exception
from typing import Any, Dict, Optional, Type, Union
Expand Down Expand Up @@ -133,7 +134,7 @@ class CLIApplicationComponent(ContainerComponent):
"""

async def start(self, ctx: Context) -> None:
def run_complete(f):
def run_complete(f: Future[int | None]) -> None:
# If run() raised an exception, print it with a traceback and exit with code 1
exc = f.exception()
if exc is not None:
Expand Down
8 changes: 4 additions & 4 deletions src/asphalt/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def add_teardown_callback(
self._check_closed()
self._teardown_callbacks.append((callback, pass_exception))

async def close(self, exception: BaseException = None) -> None:
async def close(self, exception: BaseException | None = None) -> None:
"""
Close this context and call any necessary resource teardown callbacks.
Expand Down Expand Up @@ -442,7 +442,7 @@ def add_resource(
self,
value,
name: str = "default",
context_attr: str = None,
context_attr: str | None = None,
types: Union[type, Sequence[type]] = (),
) -> None:
"""
Expand Down Expand Up @@ -517,7 +517,7 @@ def add_resource_factory(
factory_callback: factory_callback_type,
types: Union[type, Sequence[Type], None] = None,
name: str = "default",
context_attr: str = None,
context_attr: str | None = None,
) -> None:
"""
Add a resource factory to this context.
Expand Down Expand Up @@ -900,7 +900,7 @@ async def start(self, ctx: Context):

@wraps(func)
async def wrapper(*args, **kwargs) -> None:
async def teardown_callback(exception: Optional[Exception]):
async def teardown_callback(exception: Optional[Exception]) -> None:
try:
await generator.asend(exception)
except StopAsyncIteration:
Expand Down
4 changes: 2 additions & 2 deletions src/asphalt/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ def wait_event(
return wait_event([self], filter)

def stream_events(
self, filter: Callable[[Event], bool] = None, *, max_queue_size: int = 0
self, filter: Callable[[T_Event], bool] = None, *, max_queue_size: int = 0
) -> AsyncIterator[T_Event]:
"""Shortcut for calling :func:`stream_events` with this signal in the first argument."""
return stream_events([self], filter, max_queue_size=max_queue_size)


def stream_events(
signals: Sequence[Signal],
signals: Sequence[Signal[T_Event]],
filter: Callable[[T_Event], bool] = None,
*,
max_queue_size: int = 0,
Expand Down
8 changes: 2 additions & 6 deletions src/asphalt/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from logging.config import dictConfig
from typing import Any, Dict, Optional, Union, cast

from typeguard import check_argument_types

from asphalt.core.component import Component, component_types
from asphalt.core.context import Context, _current_context
from asphalt.core.utils import PluginContainer, qualified_name
Expand All @@ -29,8 +27,8 @@ def sigterm_handler(logger: Logger, event_loop: AbstractEventLoop) -> None:
def run_application(
component: Union[Component, Dict[str, Any]],
*,
event_loop_policy: str = None,
max_threads: int = None,
event_loop_policy: str | None = None,
max_threads: int | None = None,
logging: Union[Dict[str, Any], int, None] = INFO,
start_timeout: Union[int, float, None] = 10,
) -> None:
Expand Down Expand Up @@ -68,8 +66,6 @@ def run_application(
up before giving up (``None`` = wait forever)
"""
assert check_argument_types()

# Configure the logging system
if isinstance(logging, dict):
dictConfig(logging)
Expand Down
2 changes: 1 addition & 1 deletion src/asphalt/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from importlib_metadata import EntryPoint, entry_points


def resolve_reference(ref):
def resolve_reference(ref: Any) -> Any:
"""
Return the object pointed to by ``ref``.
Expand Down

0 comments on commit 2e8006e

Please sign in to comment.