Skip to content

Commit

Permalink
chore: Enable type checking in meltano.core.schedule_service (melta…
Browse files Browse the repository at this point in the history
…no#6831)

Co-authored-by: Will Da Silva <will@willdasilva.xyz>
  • Loading branch information
edgarrmondragon and WillDaSilva committed Oct 4, 2022
1 parent fbfccfa commit b4e8bdd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def mypy(session: Session) -> None:
session.install(
"mypy",
"sqlalchemy2-stubs",
"types-croniter",
"types-requests",
)
session.run("mypy", *args)
16 changes: 14 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pyupgrade = "^2.29.1"
requests-mock = "^1.6.0"
setproctitle = "^1.3" # Used by pytest-xdist to aid with handling resource intensive processes.
tox = "^3.24.4"
types-croniter = "^1.3.2"
types-requests = "^2.28.9"
wemake-python-styleguide = "^0.16.1"

Expand Down Expand Up @@ -144,7 +145,9 @@ show_missing = true
skip_covered = true
precision = 2
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
"\\.\\.\\.",
]

[tool.commitizen]
Expand Down Expand Up @@ -204,7 +207,6 @@ module = [
"meltano.core.project_plugins_service",
"meltano.core.project_settings_service",
"meltano.core.schedule",
"meltano.core.schedule_service",
"meltano.core.select_service",
"meltano.core.setting",
"meltano.core.setting_definition",
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Schedule(NameEq, Canonical): # noqa: WPS230

def __init__(
self,
name: str | None = None,
name: str,
extractor: str | None = None,
loader: str | None = None,
transform: str | None = None,
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/schedule_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def default_start_date(self, session, extractor: str) -> datetime:
extractor_plugin = self.plugins_service.find_plugin(
extractor, plugin_type=PluginType.EXTRACTORS
)
start_date = None
start_date: str | datetime | date | None = None
try:
settings_service = PluginSettingsService(
self.project, extractor_plugin, plugins_service=self.plugins_service
Expand Down
47 changes: 43 additions & 4 deletions src/meltano/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import math
import os
import re
import sys
import traceback
from collections import OrderedDict
from copy import deepcopy
from datetime import date, datetime, time
from pathlib import Path
from typing import Any, Callable, Iterable, TypeVar
from typing import Any, Callable, Iterable, TypeVar, overload

import flatten_dict
from requests.auth import HTTPBasicAuth
Expand All @@ -35,6 +36,11 @@
except AttributeError:
asyncio_all_tasks = asyncio.Task.all_tasks

if sys.version_info >= (3, 8):
from typing import Protocol
else:
from typing_extensions import Protocol


class NotFound(Exception):
"""Occurs when an element is not found."""
Expand Down Expand Up @@ -290,7 +296,22 @@ def truthy(val: str) -> bool:
return str(val).lower() in TRUTHY


def coerce_datetime(d: date | datetime) -> datetime | None:
@overload
def coerce_datetime(d: None) -> None:
... # noqa: WPS428


@overload
def coerce_datetime(d: datetime) -> datetime:
... # noqa: WPS428


@overload
def coerce_datetime(d: date) -> datetime:
... # noqa: WPS428


def coerce_datetime(d):
"""Add a `time` component to `d` if it is missing.
Args:
Expand All @@ -308,7 +329,17 @@ def coerce_datetime(d: date | datetime) -> datetime | None:
return datetime.combine(d, time())


def iso8601_datetime(d: str) -> datetime | None:
@overload
def iso8601_datetime(d: None) -> None:
... # noqa: WPS428


@overload
def iso8601_datetime(d: str) -> datetime:
... # noqa: WPS428


def iso8601_datetime(d):
if d is None:
return None

Expand All @@ -328,7 +359,15 @@ def iso8601_datetime(d: str) -> datetime | None:
raise ValueError(f"{d} is not a valid UTC date.")


def find_named(xs: Iterable[dict], name: str, obj_type: type = None) -> dict:
class _GetItemProtocol(Protocol):
def __getitem__(self, key: str) -> str:
... # noqa: WPS428


_G = TypeVar("_G", bound=_GetItemProtocol)


def find_named(xs: Iterable[_G], name: str, obj_type: type = None) -> _G:
"""Find an object by its 'name' key.
Args:
Expand Down

0 comments on commit b4e8bdd

Please sign in to comment.