Skip to content

Commit

Permalink
lint: fix mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik committed Sep 12, 2024
1 parent 1d41dc5 commit 27ab0b5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 76 deletions.
23 changes: 16 additions & 7 deletions faststream/_internal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,23 @@ def __init__(
self.logger = logger
self.context = context

self._on_startup_calling: List[AsyncFunc] = [apply_types(to_async(x)) for x in on_startup]
self._after_startup_calling: List[AsyncFunc] = [apply_types(to_async(x)) for x in after_startup]
self._on_shutdown_calling: List[AsyncFunc] = [apply_types(to_async(x)) for x in on_shutdown]
self._after_shutdown_calling: List[AsyncFunc] = [apply_types(to_async(x)) for x in after_shutdown]
self._on_startup_calling: List[AsyncFunc] = [
apply_types(to_async(x)) for x in on_startup
]
self._after_startup_calling: List[AsyncFunc] = [
apply_types(to_async(x)) for x in after_startup
]
self._on_shutdown_calling: List[AsyncFunc] = [
apply_types(to_async(x)) for x in on_shutdown
]
self._after_shutdown_calling: List[AsyncFunc] = [
apply_types(to_async(x)) for x in after_shutdown
]

if lifespan is not None:
self.lifespan_context = apply_types(func=lifespan, wrap_model=drop_response_type)
self.lifespan_context = apply_types(
func=lifespan, wrap_model=drop_response_type
)
else:
self.lifespan_context = fake_context

Expand All @@ -103,8 +113,7 @@ async def run(
log_level: int,
run_extra_options: Optional[Dict[str, "SettingField"]] = None,
sleep_time: float = 0.1,
) -> None:
...
) -> None: ...

def set_broker(self, broker: "BrokerUsecase[Any, Any]") -> None:
"""Set already existed App object broker.
Expand Down
1 change: 1 addition & 0 deletions faststream/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def as_asgi(
) -> AsgiFastStream:
return AsgiFastStream.from_app(self, asgi_routes, asyncapi_path)


try:
from contextlib import asynccontextmanager

Expand Down
23 changes: 16 additions & 7 deletions faststream/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Sequence,
Tuple,
Union,
cast,
)

import anyio
Expand Down Expand Up @@ -94,10 +95,10 @@ def __init__(

@classmethod
def from_app(
cls,
app: Application,
asgi_routes: Sequence[Tuple[str, "ASGIApp"]],
asyncapi_path: Optional[str] = None
cls,
app: Application,
asgi_routes: Sequence[Tuple[str, "ASGIApp"]],
asyncapi_path: Optional[str] = None,
) -> "AsgiFastStream":
asgi_app = cls(
app.broker,
Expand Down Expand Up @@ -143,12 +144,20 @@ async def run(
self,
log_level: int,
run_extra_options: Optional[Dict[str, "SettingField"]] = None,
sleep_time: float = 0.1
sleep_time: float = 0.1,
) -> None:
import uvicorn
port = run_extra_options.pop("port", 1337)

run_extra_options = run_extra_options or {}
port = cast(str, run_extra_options.pop("port", "1337"))
host = run_extra_options.pop("host", "0.0.0.0")
config = uvicorn.Config(self, host=host, port=int(port), log_level=log_level, **run_extra_options)
config = uvicorn.Config(
self,
host=host,
port=int(port),
log_level=log_level,
**run_extra_options,
)
server = uvicorn.Server(config)
await server.serve()

Expand Down
16 changes: 8 additions & 8 deletions faststream/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ def run(

args = (app, extra, is_factory, casted_log_level)

if not isinstance(app_obj, FastStream):
return app_obj.run() # TODO: explicit uvicorn run call

if reload and workers > 1:
raise SetupError("You can't use reload option with multiprocessing")

Expand All @@ -146,11 +143,14 @@ def run(
elif workers > 1:
from faststream.cli.supervisors.multiprocess import Multiprocess

return Multiprocess(
target=_run,
args=(*args, logging.DEBUG),
workers=workers,
).run()
if isinstance(app_obj, FastStream):
return Multiprocess(
target=_run,
args=(*args, logging.DEBUG),
workers=workers,
).run()
else:
return _run(*args)

else:
return _run(*args)
Expand Down
104 changes: 52 additions & 52 deletions faststream/cli/utils/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,58 @@
from faststream._internal.application import Application


def try_import_app(module: Path, app: str) -> "Application":
def import_from_string(
import_str: str,
*,
is_factory: bool = False,
) -> Tuple[Path, "Application"]:
"""Import FastStream application from module specified by a string."""
if not isinstance(import_str, str):
raise typer.BadParameter("Given value is not of type string")

module_str, _, attrs_str = import_str.partition(":")
if not module_str or not attrs_str:
raise typer.BadParameter(
f'Import string "{import_str}" must be in format "<module>:<attribute>"'
)

try:
module = importlib.import_module( # nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
module_str
)

except ModuleNotFoundError:
module_path, app_name = _get_app_path(import_str)
instance = _try_import_app(module_path, app_name)

else:
attr = module
try:
for attr_str in attrs_str.split("."):
attr = getattr(attr, attr_str)
instance = attr # type: ignore[assignment]

except AttributeError as e:
typer.echo(e, err=True)
raise typer.BadParameter(
f'Attribute "{attrs_str}" not found in module "{module_str}".'
) from e

if module.__file__:
module_path = Path(module.__file__).resolve().parent
else:
module_path = Path.cwd()

if is_factory:
instance = instance() # type: ignore[operator]

return module_path, instance


def _try_import_app(module: Path, app: str) -> "Application":
"""Tries to import a FastStream app from a module."""
try:
app_object = import_object(module, app)
app_object = _import_object(module, app)

except FileNotFoundError as e:
typer.echo(e, err=True)
Expand All @@ -26,7 +74,7 @@ def try_import_app(module: Path, app: str) -> "Application":
return app_object # type: ignore


def import_object(module: Path, app: str) -> object:
def _import_object(module: Path, app: str) -> object:
"""Import an object from a module."""
spec = spec_from_file_location(
"mode",
Expand All @@ -53,7 +101,7 @@ def import_object(module: Path, app: str) -> object:
return obj


def get_app_path(app: str) -> Tuple[Path, str]:
def _get_app_path(app: str) -> Tuple[Path, str]:
"""Get the application path."""
if ":" not in app:
raise SetupError(f"`{app}` is not a FastStream")
Expand All @@ -65,51 +113,3 @@ def get_app_path(app: str) -> Tuple[Path, str]:
mod_path = mod_path / i

return mod_path, app_name


def import_from_string(
import_str: str,
*,
is_factory: bool = False,
) -> Tuple[Path, "Application"]:
"""Import FastStream application from module specified by a string."""
if not isinstance(import_str, str):
raise typer.BadParameter("Given value is not of type string")

module_str, _, attrs_str = import_str.partition(":")
if not module_str or not attrs_str:
raise typer.BadParameter(
f'Import string "{import_str}" must be in format "<module>:<attribute>"'
)

try:
module = importlib.import_module( # nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
module_str
)

except ModuleNotFoundError:
module_path, app_name = get_app_path(import_str)
instance = try_import_app(module_path, app_name)

else:
attr = module
try:
for attr_str in attrs_str.split("."):
attr = getattr(attr, attr_str)
instance = attr # type: ignore[assignment]

except AttributeError as e:
typer.echo(e, err=True)
raise typer.BadParameter(
f'Attribute "{attrs_str}" not found in module "{module_str}".'
) from e

if module.__file__:
module_path = Path(module.__file__).resolve().parent
else:
module_path = Path.cwd()

if is_factory:
instance = instance()

return module_path, instance
4 changes: 2 additions & 2 deletions faststream/cli/utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING, DefaultDict, Optional, Union

if TYPE_CHECKING:
from faststream.app import FastStream
from faststream._internal.application import Application
from faststream.types import LoggerProto


Expand Down Expand Up @@ -64,7 +64,7 @@ def get_log_level(level: Union[LogLevels, str, int]) -> int:
return LOG_LEVELS[level.lower()]


def set_log_level(level: int, app: "FastStream") -> None:
def set_log_level(level: int, app: "Application") -> None:
"""Sets the log level for an application."""
if app.logger and getattr(app.logger, "setLevel", None):
app.logger.setLevel(level) # type: ignore[attr-defined]
Expand Down

0 comments on commit 27ab0b5

Please sign in to comment.