Skip to content

Commit

Permalink
Fix up types with new mypy strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer committed May 19, 2018
1 parent 5f5d1fe commit 49c12ef
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
23 changes: 19 additions & 4 deletions routemaster/context.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
"""Context definition for exit condition programs."""
import datetime
from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional, Sequence
from typing import (
TYPE_CHECKING,
Any,
Dict,
Callable,
Iterable,
Optional,
Sequence,
ContextManager,
)

from routemaster.utils import get_path

if TYPE_CHECKING:
from routemaster.feeds import Feed # noqa
import requests # noqa
from routemaster.feeds import Feed, FeedResponseLogger # noqa

FeedLoggingContext = Callable[
[str],
ContextManager[Callable[['requests.Response'], None]],
]


class Context(object):
Expand All @@ -20,7 +35,7 @@ def __init__(
feeds: Dict[str, 'Feed'],
accessed_variables: Iterable[str],
current_history_entry: Optional[Any],
feed_logging_context,
feed_logging_context: FeedLoggingContext,
) -> None:
"""Create an execution context."""
if now.tzinfo is None:
Expand Down Expand Up @@ -82,7 +97,7 @@ def _pre_warm_feeds(
self,
label: str,
accessed_variables: Iterable[str],
logging_context,
logging_context: FeedLoggingContext,
) -> None:
for accessed_variable in accessed_variables:
parts = accessed_variable.split('.')
Expand Down
4 changes: 3 additions & 1 deletion routemaster/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from routemaster.utils import get_path, template_url
from routemaster.config import StateMachine

FeedResponseLogger = Callable[[requests.Response], None]


def feeds_for_state_machine(state_machine: StateMachine) -> Dict[str, 'Feed']:
"""Get a mapping of feed prefixes to unfetched feeds."""
Expand Down Expand Up @@ -43,7 +45,7 @@ class Feed:
def prefetch(
self,
label: str,
log_response: Callable[[requests.Response], None] = lambda x: None,
log_response: FeedResponseLogger = lambda x: None,
) -> None:
"""Trigger the fetching of a feed's data."""
if self.data is not None:
Expand Down
2 changes: 1 addition & 1 deletion routemaster/logging/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class BaseLogger:
"""Base class for logging plugins."""

def __init__(self, config: Any, *args, **kwargs) -> None:
def __init__(self, config: Any, **kwargs: str) -> None:
self.config = config

def init_flask(self, flask_app):
Expand Down
8 changes: 6 additions & 2 deletions routemaster/logging/python_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import time
import logging
import contextlib
from typing import TYPE_CHECKING

from routemaster.logging.base import BaseLogger

if TYPE_CHECKING:
from routemaster.config import Config # noqa


class PythonLogger(BaseLogger):
"""Routemaster logging interface for Python's logging library."""

def __init__(self, *args, log_level: str) -> None:
super().__init__(*args)
def __init__(self, config: 'Config', log_level: str) -> None:
super().__init__(config)

logging.basicConfig(
format=(
Expand Down
9 changes: 6 additions & 3 deletions routemaster/logging/split_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import functools
import contextlib
from typing import List
from typing import TYPE_CHECKING, List

from routemaster.logging.base import BaseLogger

if TYPE_CHECKING:
from routemaster.config import Config # noqa


class SplitLogger(BaseLogger):
"""Proxies logging calls to all loggers in a list."""

def __init__(self, *args, loggers: List[BaseLogger]) -> None:
super().__init__(*args)
def __init__(self, config: 'Config', loggers: List[BaseLogger]) -> None:
super().__init__(config)

self.loggers = loggers

Expand Down
2 changes: 1 addition & 1 deletion routemaster/logging/tests/test_loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
]}),
(SplitLogger, {'loggers': [
PythonLogger(None, log_level='WARN'),
BaseLogger(None, {}),
BaseLogger(None),
]}),
]

Expand Down

0 comments on commit 49c12ef

Please sign in to comment.