Skip to content

Commit

Permalink
chore: typehint context object (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanni-guidini authored Sep 10, 2024
1 parent ae3fc7e commit 6c2f585
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 37 deletions.
3 changes: 2 additions & 1 deletion codecov_cli/commands/base_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.encoder import slug_without_subgroups_is_invalid
from codecov_cli.services.commit.base_picking import base_picking_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -45,7 +46,7 @@
)
@click.pass_context
def pr_base_picking(
ctx,
ctx: CommandContext,
base_sha: str,
pr: typing.Optional[int],
slug: typing.Optional[str],
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.commit import create_commit_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -35,7 +36,7 @@
@global_options
@click.pass_context
def create_commit(
ctx,
ctx: CommandContext,
commit_sha: str,
parent_sha: typing.Optional[str],
pull_request_number: typing.Optional[int],
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/create_report_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from codecov_cli.helpers.options import global_options
from codecov_cli.services.report import create_report_results_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand All @@ -15,7 +16,7 @@
@global_options
@click.pass_context
def create_report_results(
ctx,
ctx: CommandContext,
commit_sha: str,
code: str,
slug: str,
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/empty_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.empty_upload import empty_upload_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand All @@ -16,7 +17,7 @@
@global_options
@click.pass_context
def empty_upload(
ctx,
ctx: CommandContext,
commit_sha: str,
force: bool,
slug: typing.Optional[str],
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/get_report_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.report import send_reports_result_get_request
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand All @@ -18,7 +19,7 @@
@global_options
@click.pass_context
def get_report_results(
ctx,
ctx: CommandContext,
commit_sha: str,
code: str,
slug: str,
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/labelanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
LabelAnalysisRequestResult,
LabelAnalysisRunnerInterface,
)
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -77,7 +78,7 @@
)
@click.pass_context
def label_analysis(
ctx: click.Context,
ctx: CommandContext,
token: str,
head_commit_sha: str,
base_commit_sha: str,
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.options import global_options
from codecov_cli.services.report import create_report_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand All @@ -25,7 +26,7 @@
@global_options
@click.pass_context
def create_report(
ctx,
ctx: CommandContext,
commit_sha: str,
code: str,
slug: str,
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/send_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.upload_completion import upload_completion_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand All @@ -15,7 +16,7 @@
@global_options
@click.pass_context
def send_notifications(
ctx,
ctx: CommandContext,
commit_sha: str,
slug: typing.Optional[str],
token: typing.Optional[str],
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/staticanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.validators import validate_commit_sha
from codecov_cli.services.staticanalysis import run_analysis_entrypoint
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -48,7 +49,7 @@
)
@click.pass_context
def static_analysis(
ctx,
ctx: CommandContext,
foldertosearch,
numberprocesses,
pattern,
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.options import global_options
from codecov_cli.services.upload import do_upload_logic
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -190,7 +191,7 @@ def global_upload_options(func):
@global_options
@click.pass_context
def do_upload(
ctx: click.Context,
ctx: CommandContext,
commit_sha: str,
report_code: str,
branch: typing.Optional[str],
Expand Down
3 changes: 2 additions & 1 deletion codecov_cli/commands/upload_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from codecov_cli.commands.report import create_report
from codecov_cli.commands.upload import do_upload, global_upload_options
from codecov_cli.helpers.options import global_options
from codecov_cli.types import CommandContext

logger = logging.getLogger("codecovcli")

Expand All @@ -22,7 +23,7 @@
)
@click.pass_context
def upload_process(
ctx,
ctx: CommandContext,
commit_sha: str,
report_code: str,
build_code: typing.Optional[str],
Expand Down
4 changes: 3 additions & 1 deletion codecov_cli/helpers/ci_adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging
from typing import Optional

from codecov_cli.helpers.ci_adapters.appveyor_ci import AppveyorCIAdapter
from codecov_cli.helpers.ci_adapters.azure_pipelines import AzurePipelinesCIAdapter
from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
from codecov_cli.helpers.ci_adapters.bitbucket_ci import BitbucketAdapter
from codecov_cli.helpers.ci_adapters.bitrise_ci import BitriseCIAdapter
from codecov_cli.helpers.ci_adapters.buildkite import BuildkiteAdapter
Expand All @@ -22,7 +24,7 @@
logger = logging.getLogger("codecovcli")


def get_ci_adapter(provider_name: str = None):
def get_ci_adapter(provider_name: str = None) -> Optional[CIAdapterBase]:
if provider_name:
for provider in get_ci_providers_list():
if provider.get_service_name().lower() == provider_name.lower():
Expand Down
4 changes: 2 additions & 2 deletions codecov_cli/helpers/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import pathlib
import typing
import typing as t

import yaml

Expand Down Expand Up @@ -45,7 +45,7 @@ def _find_codecov_yamls():
return yamls


def load_cli_config(codecov_yml_path: typing.Optional[pathlib.Path]):
def load_cli_config(codecov_yml_path: t.Optional[pathlib.Path]) -> t.Optional[dict]:
if not codecov_yml_path:
yamls = _find_codecov_yamls()
codecov_yml_path = yamls[0] if yamls else None
Expand Down
2 changes: 1 addition & 1 deletion codecov_cli/helpers/folder_searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def search_files(
filename_exclude_regex: typing.Optional[typing.Pattern] = None,
multipart_include_regex: typing.Optional[typing.Pattern] = None,
multipart_exclude_regex: typing.Optional[typing.Pattern] = None,
search_for_directories: bool = False
search_for_directories: bool = False,
) -> typing.Generator[pathlib.Path, None, None]:
""" "
Searches for files or directories in a given folder
Expand Down
18 changes: 6 additions & 12 deletions codecov_cli/helpers/versioning_systems.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import subprocess
import typing
import typing as t
from pathlib import Path
from shutil import which

Expand All @@ -14,21 +14,17 @@ class VersioningSystemInterface(object):
def __repr__(self) -> str:
return str(type(self))

def get_fallback_value(
self, fallback_field: FallbackFieldEnum
) -> typing.Optional[str]:
def get_fallback_value(self, fallback_field: FallbackFieldEnum) -> t.Optional[str]:
pass

def get_network_root(self) -> typing.Optional[Path]:
def get_network_root(self) -> t.Optional[Path]:
pass

def list_relevant_files(
self, directory: typing.Optional[Path] = None
) -> typing.List[str]:
def list_relevant_files(self, directory: t.Optional[Path] = None) -> t.List[str]:
pass


def get_versioning_system() -> VersioningSystemInterface:
def get_versioning_system() -> t.Optional[VersioningSystemInterface]:
for klass in [GitVersioningSystem, NoVersioningSystem]:
if klass.is_available():
logger.debug(f"versioning system found: {klass}")
Expand Down Expand Up @@ -123,9 +119,7 @@ def get_network_root(self):
return Path(p.stdout.decode().rstrip())
return None

def list_relevant_files(
self, root_folder: typing.Optional[Path] = None
) -> typing.List[str]:
def list_relevant_files(self, root_folder: t.Optional[Path] = None) -> t.List[str]:
dir_to_use = root_folder or self.get_network_root()
if dir_to_use is None:
raise ValueError("Can't determine root folder")
Expand Down
36 changes: 26 additions & 10 deletions codecov_cli/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import pathlib
import typing
import typing as t
from dataclasses import dataclass

import click

from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
from codecov_cli.helpers.versioning_systems import VersioningSystemInterface


class ContextObject(t.TypedDict):
ci_adapter: t.Optional[CIAdapterBase]
versioning_system: t.Optional[VersioningSystemInterface]
codecov_yaml: t.Optional[dict]
enterprise_url: t.Optional[str]


class CommandContext(click.Context):
obj: ContextObject


class UploadCollectionResultFile(object):
def __init__(self, path: pathlib.Path):
Expand Down Expand Up @@ -31,17 +47,17 @@ def __hash__(self) -> int:
class UploadCollectionResultFileFixer(object):
__slots__ = ["path", "fixed_lines_without_reason", "fixed_lines_with_reason", "eof"]
path: pathlib.Path
fixed_lines_without_reason: typing.Set[int]
fixed_lines_with_reason: typing.Optional[typing.Set[typing.Tuple[int, str]]]
eof: typing.Optional[int]
fixed_lines_without_reason: t.Set[int]
fixed_lines_with_reason: t.Optional[t.Set[t.Tuple[int, str]]]
eof: t.Optional[int]


@dataclass
class UploadCollectionResult(object):
__slots__ = ["network", "files", "file_fixes"]
network: typing.List[str]
files: typing.List[UploadCollectionResultFile]
file_fixes: typing.List[UploadCollectionResultFileFixer]
network: t.List[str]
files: t.List[UploadCollectionResultFile]
file_fixes: t.List[UploadCollectionResultFileFixer]


class PreparationPluginInterface(object):
Expand All @@ -59,14 +75,14 @@ class RequestResultWarning(object):
class RequestError(object):
__slots__ = ("code", "params", "description")
code: str
params: typing.Dict
params: t.Dict
description: str


@dataclass
class RequestResult(object):
__slots__ = ("error", "warnings", "status_code", "text")
error: typing.Optional[RequestError]
warnings: typing.List[RequestResultWarning]
error: t.Optional[RequestError]
warnings: t.List[RequestResultWarning]
status_code: int
text: str

0 comments on commit 6c2f585

Please sign in to comment.