Skip to content

Commit

Permalink
Updated annotations to use Python 3.10 style where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Mar 29, 2022
1 parent 376866f commit 9671a00
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
rev: v2.31.1
hooks:
- id: pyupgrade
args: [ "--py37-plus" ]
args: [ "--py37-plus", "--keep-runtime-typing" ]

- repo: https://github.com/psf/black
rev: 22.3.0
Expand Down
Empty file removed src/asphalt/__init__.pyi
Empty file.
2 changes: 2 additions & 0 deletions src/asphalt/core/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
from pathlib import Path
from typing import Any, Dict, Optional
Expand Down
10 changes: 6 additions & 4 deletions src/asphalt/core/component.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from __future__ import annotations

__all__ = ("Component", "ContainerComponent", "CLIApplicationComponent")

import asyncio
import sys
from abc import ABCMeta, abstractmethod
from collections import OrderedDict
from traceback import print_exception
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, Optional, Type, Union
from warnings import warn

from typeguard import check_argument_types

from asphalt.core.context import Context
from asphalt.core.utils import PluginContainer, merge_config, qualified_name

__all__ = ("Component", "ContainerComponent", "CLIApplicationComponent")


class Component(metaclass=ABCMeta):
"""This is the base class for all Asphalt components."""
Expand Down Expand Up @@ -60,7 +62,7 @@ def __init__(self, components: Dict[str, Optional[Dict[str, Any]]] = None) -> No
self.child_components: OrderedDict[str, Component] = OrderedDict()
self.component_configs = components or {}

def add_component(self, alias: str, type: Union[str, type] = None, **config):
def add_component(self, alias: str, type: Union[str, Type] = None, **config):
"""
Add a child component.
Expand Down
6 changes: 4 additions & 2 deletions src/asphalt/core/concurrent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

__all__ = ("executor",)

import inspect
from asyncio import get_running_loop
from concurrent.futures import Executor
Expand All @@ -8,8 +12,6 @@

from asphalt.core import Context

__all__ = ("executor",)

T_Retval = TypeVar("T_Retval")
WrappedCallable = Callable[..., Awaitable[T_Retval]]

Expand Down
32 changes: 17 additions & 15 deletions src/asphalt/core/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
from __future__ import annotations

__all__ = (
"ResourceEvent",
"ResourceConflict",
"ResourceNotFound",
"TeardownError",
"Context",
"executor",
"context_teardown",
)

import logging
import re
import warnings
Expand Down Expand Up @@ -33,16 +45,6 @@
from asphalt.core.event import Event, Signal, wait_event
from asphalt.core.utils import callable_name, qualified_name

__all__ = (
"ResourceEvent",
"ResourceConflict",
"ResourceNotFound",
"TeardownError",
"Context",
"executor",
"context_teardown",
)

logger = logging.getLogger(__name__)
factory_callback_type = Callable[["Context"], Any]
resource_name_re = re.compile(r"\w+")
Expand Down Expand Up @@ -77,7 +79,7 @@ def __init__(
self.context_attr = context_attr
self.is_factory = is_factory

def generate_value(self, ctx: "Context"):
def generate_value(self, ctx: Context):
assert self.is_factory, "generate_value() only works for resource factories"
value = self.value_or_factory(ctx)

Expand Down Expand Up @@ -122,7 +124,7 @@ class ResourceEvent(Event):

def __init__(
self,
source: "Context",
source: Context,
topic: str,
types: Tuple[type, ...],
name: str,
Expand Down Expand Up @@ -196,7 +198,7 @@ class Context:

resource_added = Signal(ResourceEvent)

def __init__(self, parent: "Context" = None) -> None:
def __init__(self, parent: Context = None) -> None:
assert check_argument_types()
self._parent = parent
self._loop = getattr(parent, "loop", None)
Expand All @@ -222,7 +224,7 @@ def __getattr__(self, name):
raise AttributeError(f"no such context variable: {name}")

@property
def context_chain(self) -> List["Context"]:
def context_chain(self) -> List[Context]:
"""Return a list of contexts starting from this one, its parent and so on."""
contexts = []
ctx: Optional[Context] = self
Expand All @@ -241,7 +243,7 @@ def loop(self) -> AbstractEventLoop:
return self._loop

@property
def parent(self) -> Optional["Context"]:
def parent(self) -> Optional[Context]:
"""Return the parent context, or ``None`` if there is no parent."""
return self._parent

Expand Down
10 changes: 6 additions & 4 deletions src/asphalt/core/event.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

__all__ = ("Event", "Signal", "wait_event", "stream_events")

import logging
import sys
import weakref
Expand Down Expand Up @@ -30,8 +34,6 @@
else:
from async_generator import aclosing

__all__ = ("Event", "Signal", "wait_event", "stream_events")

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -102,9 +104,9 @@ def __init__(
assert issubclass(
event_class, Event
), "event_class must be a subclass of Event"
self.bound_signals: MutableMapping[Any, "Signal"] = WeakKeyDictionary()
self.bound_signals: MutableMapping[Any, Signal] = WeakKeyDictionary()

def __get__(self, instance, owner) -> "Signal":
def __get__(self, instance, owner) -> Signal:
if instance is None:
return self

Expand Down
8 changes: 5 additions & 3 deletions src/asphalt/core/runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

__all__ = ("run_application",)

import asyncio
import signal
import sys
Expand All @@ -13,8 +17,6 @@
from asphalt.core.context import Context
from asphalt.core.utils import PluginContainer, qualified_name

__all__ = ("run_application",)

policies = PluginContainer("asphalt.core.event_loop_policies")


Expand All @@ -30,7 +32,7 @@ def run_application(
event_loop_policy: str = None,
max_threads: int = None,
logging: Union[Dict[str, Any], int, None] = INFO,
start_timeout: Union[int, float, None] = 10
start_timeout: Union[int, float, None] = 10,
):
"""
Configure logging and start the given root component in the default asyncio event loop.
Expand Down
22 changes: 12 additions & 10 deletions src/asphalt/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from __future__ import annotations

__all__ = (
"resolve_reference",
"qualified_name",
"callable_name",
"merge_config",
"PluginContainer",
)

import sys
from importlib import import_module
from inspect import isclass
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Callable, Dict, List, Optional, Type, Union

from typeguard import check_argument_types

Expand All @@ -10,14 +20,6 @@
else:
from importlib_metadata import EntryPoint, entry_points

__all__ = (
"resolve_reference",
"qualified_name",
"callable_name",
"merge_config",
"PluginContainer",
)


def resolve_reference(ref):
"""
Expand Down Expand Up @@ -157,7 +159,7 @@ def resolve(self, obj):

return value

def create_object(self, type: Union[type, str], **constructor_kwargs):
def create_object(self, type: Union[Type, str], **constructor_kwargs):
"""
Instantiate a plugin.
Expand Down

0 comments on commit 9671a00

Please sign in to comment.