Skip to content

Commit

Permalink
qa: Better typing annotation in context and commands.pr_add
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaty committed Apr 13, 2022
1 parent d193375 commit ef3d713
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ skip = [
"docs/conf.py",
]

[tool.mypy]
show_error_codes = true


[tool.pylint.MASTER]
load-plugins = [
"pylint_strict_informational",
Expand Down
1 change: 1 addition & 0 deletions src/cogite/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .base import BaseClient
from .github import GitHubApiClient
from .github import GitHubOAuthDeviceFlowTokenGetter
19 changes: 10 additions & 9 deletions src/cogite/commands/pr_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing

from cogite import completion
from cogite import context
from cogite import errors
from cogite import git
from cogite import interaction
Expand All @@ -12,23 +13,23 @@
from . import helpers


def add_draft_pull_request(context, **kwargs):
return add_pull_request(context, draft=True, **kwargs)
def add_draft_pull_request(ctx: context.Context, **kwargs):
return add_pull_request(ctx, draft=True, **kwargs)


def add_pull_request(context, *, base_branch, draft=False):
client = context.client
configuration = context.configuration
def add_pull_request(ctx: context.Context, *, base_branch: str, draft: bool=False):
client = ctx.client
configuration = ctx.configuration
base_branch = base_branch or configuration.master_branch

helpers.assert_current_branch_is_feature_branch(context.branch, configuration.master_branch)
helpers.assert_current_branch_is_feature_branch(ctx.branch, configuration.master_branch)

_git_push_to_origin(context.branch)
_git_push_to_origin(ctx.branch)

commits_text = os.linesep.join(
itertools.chain.from_iterable(
(commit, '', '')
for commit in git.get_commits_logs(base_branch, context.branch)
for commit in git.get_commits_logs(base_branch, ctx.branch)
)
)

Expand Down Expand Up @@ -64,7 +65,7 @@ def add_pull_request(context, *, base_branch, draft=False):
) as sp:
try:
pr = client.create_pull_request(
head=context.branch,
head=ctx.branch,
base=base_branch,
title=title.strip(),
body=body.strip(),
Expand Down
6 changes: 3 additions & 3 deletions src/cogite/completion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import List
from typing import Iterable

import prompt_toolkit
import prompt_toolkit.completion
Expand All @@ -13,7 +13,7 @@


class UserCompleter(prompt_toolkit.completion.Completer):
def __init__(self, users: List[models.User]):
def __init__(self, users: Iterable[models.User]):
self.users = users

def get_completions(self, document, complete_event):
Expand All @@ -28,7 +28,7 @@ def get_completions(self, document, complete_event):
)


def prompt_for_users(users: List[models.User]):
def prompt_for_users(users: Iterable[models.User]):
while True:
completer = UserCompleter(users)
response = prompt_toolkit.prompt(
Expand Down
12 changes: 10 additions & 2 deletions src/cogite/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re
import urllib.parse

from cogite import backends
from cogite import config
from cogite import errors
from cogite import git

Expand All @@ -18,8 +20,8 @@ class Context:
branch: str

# Injected from `cli._main()`
client = None
configuration = None
client: backends.BaseClient
configuration: config.Configuration

def as_dict(self):
return dict(self.__dict__) # copy, except for `client` and `configuration`
Expand Down Expand Up @@ -54,4 +56,10 @@ def get_context() -> Context:
owner=owner,
repository=repository,
branch=git.get_current_branch(),
# `client` and `configuration` will be overriden with proper
# values in `cli._main()`. We provide dummy values to avoid
# having to set `Context.client` and `Context.configuration`
# as Optional, because they are not.
client="dummy", # type: ignore[arg-type]
configuration="dummy", # type: ignore[arg-type]
)
2 changes: 2 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def _make_client():
owner="dummy_owner",
repository="dummy_repository",
branch="dummy_branch",
client="dummy",
configuration="dummy",
)
return github.GitHubApiClient(configuration, ctx)

Expand Down

0 comments on commit ef3d713

Please sign in to comment.