Skip to content

Commit

Permalink
Add missing type hints
Browse files Browse the repository at this point in the history
Summary: Title

Reviewed By: danielocfb

Differential Revision: D51326636

fbshipit-source-id: cf0bcd6415c497e84da47da715787ca1d7e2195a
  • Loading branch information
yurinnick authored and facebook-github-bot committed Nov 29, 2023
1 parent 7d263cd commit 719a444
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 59 deletions.
6 changes: 3 additions & 3 deletions tests/common/patchwork_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


class PatchworkMock(Patchwork):
def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Any, **kwargs: Any) -> None:
# Force patchwork to use loopback/port 0 for whatever network access
# we fail to mock
# kwargs["server"] = "https://127.0.0.1:0"
Expand Down Expand Up @@ -69,7 +69,7 @@ def init_pw_responses(m: aioresponses, data: Dict[str, Any]) -> None:
m.get(re.compile(r"^.*$"), status=200, body=b"[]")


def get_dict_key(d: Dict[Any, Any], idx: int = 0) -> Any:
def get_dict_key(d: Dict[str, Any], idx: int = 0) -> str:
"""
Given a dictionary, get a list of keys and return the one a `idx`.
"""
Expand All @@ -80,7 +80,7 @@ def get_dict_key(d: Dict[Any, Any], idx: int = 0) -> Any:
FOO_SERIES_LAST = 10

DEFAULT_FREEZE_DATE = "2010-07-23T00:00:00"
DEFAULT_TEST_RESPONSES = {
DEFAULT_TEST_RESPONSES: Dict[str, Any] = {
"https://127.0.0.1:0/api/1.1/series/?q=foo": [
# Does not match the subject name
{
Expand Down
53 changes: 27 additions & 26 deletions tests/test_branch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import unittest
from dataclasses import dataclass, field
from datetime import datetime
from typing import List
from typing import Any, Dict, List
from unittest.mock import MagicMock, patch

from aioresponses import aioresponses
Expand All @@ -21,7 +21,6 @@
create_color_labels,
has_same_base_different_remote,
HEAD_BASE_SEPARATOR,
PullRequest,
temporary_patch_file,
UPSTREAM_REMOTE_NAME,
)
Expand All @@ -46,7 +45,7 @@
TEST_CI_BRANCH = "test_ci_branch"
TEST_BASE_DIRECTORY = "/repos"
TEST_BRANCH = "test-branch"
TEST_CONFIG = {
TEST_CONFIG: Dict[str, Any] = {
"version": 2,
"project": "test",
"pw_url": "pw",
Expand All @@ -69,7 +68,7 @@
"RFC": "f2e318",
"new": "c2e0c6",
}
SERIES_DATA = {
SERIES_DATA: Dict[str, Any] = {
"id": 0,
"name": "foo",
"date": "2010-07-20T01:00:00",
Expand All @@ -84,7 +83,7 @@


class BranchWorkerMock(BranchWorker):
def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Any, **kwargs: Any) -> None:
presets = {
"patchwork": MagicMock(),
"labels_cfg": TEST_LABELS_CFG,
Expand Down Expand Up @@ -327,24 +326,24 @@ def test_relevant_pr(self) -> None:
self._bw.user_or_org = user_login

def make_munch(
head_user=user_login,
base_user=user_login,
base_ref=TEST_REPO_PR_BASE_BRANCH,
state="open",
):
head_user: str = user_login,
base_user: str = user_login,
base_ref: str = TEST_REPO_PR_BASE_BRANCH,
state: str = "open",
) -> Munch:
"""Helper to make a Munch that can be consumed as a PR (e.g accessing nested attributes)"""
return munchify(
{
"head": {"user": {"login": head_user}},
"base": {"user": {"login": base_user}, "ref": base_ref},
"state": state,
}
},
)

@dataclass
class TestCase:
name: str
pr: PullRequest
pr: Munch
relevant: bool

test_cases = [
Expand Down Expand Up @@ -382,6 +381,8 @@ class TestCase:

for case in test_cases:
with self.subTest(msg=case.name):
# pyre-fixme: Incompatible parameter type [6]: In call `BranchWorker._is_relevant_pr`,
# for 1st positional argument, expected `PullRequest` but got `Munch`.
self.assertEqual(self._bw._is_relevant_pr(case.pr), case.relevant)

def test_fetch_repo_path_doesnt_exist_full_sync(self) -> None:
Expand Down Expand Up @@ -478,7 +479,7 @@ class TestCase:
for case in test_cases:
with self.subTest(msg=case.name):
self._bw.branches = case.branches
self._bw.all_prs = case.all_prs
self._bw.all_prs = {p: {} for p in case.all_prs}
with patch.object(self._bw, "filter_closed_pr") as fcp, patch.object(
self._bw, "delete_branch"
) as db, freeze_time(not_expired_time):
Expand All @@ -502,11 +503,11 @@ def test_filter_closed_pr(self) -> None:
base_datetime = datetime.fromtimestamp(base_time)

def make_munch(
head_ref="test",
state="closed",
updated_at=base_datetime,
title="title",
):
head_ref: str = "test",
state: str = "closed",
updated_at: datetime = base_datetime,
title: str = "title",
) -> Munch:
"""Helper to make a Munch that can be consumed as a PR (e.g accessing nested attributes)"""
return munchify(
{
Expand Down Expand Up @@ -586,7 +587,7 @@ def test_delete_branches(self) -> None:
ggr.return_value.delete.assert_called_once()

@aioresponses()
async def test_guess_pr_return_from_active_pr_cache(self, m) -> None:
async def test_guess_pr_return_from_active_pr_cache(self, m: aioresponses) -> None:
# Whatever is in our self.prs's cache dictionary will be returned.
series = Series(self._pw, SERIES_DATA)
sentinel = random.random()
Expand All @@ -611,7 +612,7 @@ async def test_guess_pr_return_from_secondary_cache_with_specified_branch(

@aioresponses()
async def test_guess_pr_return_from_secondary_cache_without_specified_branch(
self, m
self, m: aioresponses
) -> None:
init_pw_responses(m, DEFAULT_TEST_RESPONSES)
# After self.prs, we will look into self.all_prs
Expand All @@ -628,7 +629,7 @@ async def test_guess_pr_return_from_secondary_cache_without_specified_branch(

@aioresponses()
async def test_guess_pr_not_in_cache_no_specified_branch_no_remote_branch(
self, m
self, m: aioresponses
) -> None:
"""
Handling of series which is not in our PR cache (self.prs, self.all_prs empty)
Expand All @@ -652,7 +653,7 @@ async def test_guess_pr_not_in_cache_no_specified_branch_no_remote_branch(

@aioresponses()
async def test_guess_pr_not_in_cache_no_specified_branch_has_remote_branch_v1(
self, m
self, m: aioresponses
) -> None:
"""
Handling of series which is not in our PR cache (self.prs, self.all_prs empty)
Expand All @@ -668,7 +669,7 @@ async def test_guess_pr_not_in_cache_no_specified_branch_has_remote_branch_v1(

series = Series(self._pw, {**SERIES_DATA, "version": 1})
mybranch = await self._bw.subject_to_branch(Subject(series.subject, self._pw))
self._bw.branches = "aaa"
self._bw.branches = ["aaa"]
pr = await self._bw._guess_pr(series, mybranch)

# branch is an active remote branch, our series version is v1. We look up closed
Expand All @@ -678,7 +679,7 @@ async def test_guess_pr_not_in_cache_no_specified_branch_has_remote_branch_v1(

@aioresponses()
async def test_guess_pr_not_in_cache_no_specified_branch_has_remote_branch_v2_first_series(
self, m
self, m: aioresponses
) -> None:
"""
Handling of series which is not in our PR cache (self.prs, self.all_prs empty)
Expand All @@ -705,7 +706,7 @@ async def test_guess_pr_not_in_cache_no_specified_branch_has_remote_branch_v2_fi

@aioresponses()
async def test_guess_pr_not_in_cache_no_specified_branch_is_remote_branch_v2_multiple_series_noclosed_pr(
self, m
self, m: aioresponses
) -> None:
"""
Handling of series which is not in our PR cache (self.prs, self.all_prs empty)
Expand Down Expand Up @@ -736,7 +737,7 @@ async def test_guess_pr_not_in_cache_no_specified_branch_is_remote_branch_v2_mul

@aioresponses()
async def test_guess_pr_not_in_cache_no_specified_branch_is_remote_branch_v2_multiple_series_with_closed_pr(
self, m
self, m: aioresponses
) -> None:
"""
Handling of series which is not in our PR cache (self.prs, self.all_prs empty)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import os
import unittest
from typing import Dict
from typing import Dict, Union
from unittest.mock import mock_open, patch

from kernel_patches_daemon.config import (
Expand All @@ -19,7 +19,7 @@
)


def read_fixture(filepath: str) -> Dict:
def read_fixture(filepath: str) -> Dict[str, Union[str, int, bool]]:
with open(os.path.join(os.path.dirname(__file__), filepath)) as f:
return json.load(f)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_github_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import datetime
import unittest
from dataclasses import dataclass
from typing import Optional
from typing import Any, List, Optional
from unittest.mock import MagicMock, patch

import freezegun
Expand All @@ -30,7 +30,7 @@
# unfortunately, this has the side effect of not freezing the time for the github
# package... Here we are popping anything that could get in the way from the dfault list
# https://github.com/spulec/freezegun/issues/484
default_ignore_list = [
default_ignore_list: List[str] = [
x for x in freezegun.config.DEFAULT_IGNORE_LIST if not "github".startswith(x)
]
# pyre-fixme[16]: Module freezegun has no attribute configure
Expand All @@ -43,7 +43,7 @@
TEST_REPO_URL = f"https://user:pass@127.0.0.1:0/{TEST_ORG}/{TEST_REPO}"
TEST_APP_ID = 1
TEST_INSTALLATION_ID = 2
TEST_PRIV_KEY = (
TEST_PRIV_KEY: str = (
rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
Expand All @@ -58,7 +58,7 @@


class GithubConnectorMock(GithubConnector):
def __init__(self, *args, **kwargs) -> None:
def __init__(self, *args: Any, **kwargs: Any) -> None:
presets = {
"repo_url": TEST_REPO_URL,
}
Expand All @@ -71,7 +71,7 @@ def get_default_gc_oauth_client() -> GithubConnectorMock:
return GithubConnectorMock(github_oauth_token="random_gh_oauth_token")


def get_default_gc_app_auth_client(**kwargs) -> GithubConnectorMock:
def get_default_gc_app_auth_client(**kwargs: Any) -> GithubConnectorMock:
presets = {
"app_auth": AppInstallationAuth(
AppAuth(app_id=TEST_APP_ID, private_key=TEST_PRIV_KEY),
Expand Down
11 changes: 7 additions & 4 deletions tests/test_github_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import copy
import unittest
from dataclasses import dataclass
from typing import Any, Dict, Optional
from unittest.mock import MagicMock, patch

from kernel_patches_daemon.config import KPDConfig
from kernel_patches_daemon.github_sync import GithubSync

TEST_BRANCH = "test-branch"
TEST_CONFIG = {
TEST_CONFIG: Dict[str, Any] = {
"version": 3,
"patchwork": {
"project": "test",
Expand All @@ -36,7 +37,9 @@


class GithubSyncMock(GithubSync):
def __init__(self, kpd_config=None, *args, **kwargs) -> None:
def __init__(
self, kpd_config: Optional[KPDConfig] = None, *args: Any, **kwargs: Any
) -> None:
if kpd_config is None:
kpd_config = KPDConfig.from_json(TEST_CONFIG)

Expand All @@ -61,12 +64,12 @@ def setUp(self) -> None:

self._gh = GithubSyncMock()

def test_init_with_base_directory(self):
def test_init_with_base_directory(self) -> None:
@dataclass
class TestCase:
name: str
prefix: str
base_dir: str = None
base_dir: Optional[str] = None

test_cases = [
TestCase(
Expand Down
Loading

0 comments on commit 719a444

Please sign in to comment.