Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing improvements #670

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Example:
```bash
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ pip install -r requirements_dev.txt
```


Expand Down
4 changes: 3 additions & 1 deletion flux_local/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ async def _run_piped_with_sem(cmds: Sequence[Task]) -> str:
try:
out = await asyncio.wait_for(cmd.run(stdin), _TIMEOUT)
except asyncio.exceptions.TimeoutError as err:
raise cmd.exc(f"Command '{cmd}' timed out") from err
if isinstance(cmd, Command):
raise cmd.exc(f"Command '{cmd}' timed out") from err
raise err
stdin = out
return out.decode("utf-8") if out else ""

Expand Down
14 changes: 11 additions & 3 deletions flux_local/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ async def build_kustomization(
]


def _ready_kustomizations(kustomizations: list[Kustomization], visited: set[str]) -> tuple[Kustomization, Kustomization]:
def _ready_kustomizations(
kustomizations: list[Kustomization], visited: set[str]
) -> tuple[list[Kustomization], list[Kustomization]]:
"""Split the kustomizations into those that are ready vs pending."""
ready = []
pending = []
Expand Down Expand Up @@ -690,7 +692,11 @@ async def update_kustomization(cluster: Cluster) -> None:
build_tasks = []
(ready, pending) = _ready_kustomizations(queue, visited)
for kustomization in ready:
_LOGGER.debug("Processing kustomization '%s': %s", kustomization.name, kustomization.path)
_LOGGER.debug(
"Processing kustomization '%s': %s",
kustomization.name,
kustomization.path,
)

if kustomization.postbuild_substitute_from:
values.expand_postbuild_substitute_reference(
Expand Down Expand Up @@ -772,7 +778,9 @@ async def update_kustomization(cluster: Cluster) -> None:


@contextlib.contextmanager
def create_worktree(repo: git.repo.Repo, existing_branch: str | None = None) -> Generator[Path, None, None]:
def create_worktree(
repo: git.repo.Repo, existing_branch: str | None = None
) -> Generator[Path, None, None]:
"""Create a ContextManager for a new git worktree in the current repo.

This is used to get a fork of the current repo without any local changes
Expand Down
5 changes: 2 additions & 3 deletions flux_local/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
SECRET_KIND,
REPO_TYPE_OCI,
HELM_REPOSITORY,
GIT_REPOSITORY
GIT_REPOSITORY,
)
from .exceptions import HelmException

Expand All @@ -68,7 +68,7 @@
def _chart_name(release: HelmRelease, repo: HelmRepository | None) -> str:
"""Return the helm chart name used for the helm template command."""
if release.chart.repo_kind == HELM_REPOSITORY:
if repo.repo_type == REPO_TYPE_OCI:
if repo and repo.repo_type == REPO_TYPE_OCI:
return f"{repo.url}/{release.chart.name}"
return release.chart.chart_name
elif release.chart.repo_kind == GIT_REPOSITORY:
Expand All @@ -79,7 +79,6 @@ def _chart_name(release: HelmRelease, repo: HelmRepository | None) -> str:
)



class RepositoryConfig:
"""Generates a helm repository configuration from flux HelmRepository objects."""

Expand Down
6 changes: 4 additions & 2 deletions flux_local/kustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from aiofiles.ospath import isdir
import asyncio
from contextlib import asynccontextmanager
from collections.abc import AsyncIterator
import logging
from pathlib import Path
import tempfile
Expand Down Expand Up @@ -70,6 +71,7 @@
# Used to limit access to specific resources
_LOCK_MAP: dict[str, asyncio.Lock] = {}


class Kustomize:
"""Library for issuing a kustomize command."""

Expand Down Expand Up @@ -194,9 +196,9 @@ async def run(self, stdin: bytes | None = None) -> bytes:


@asynccontextmanager
async def _resource_lock(key: str):
async def _resource_lock(key: str) -> AsyncIterator[None]:
"""Run while holding a lock for the specified resource.

This is not threadsafe and expected to be run in the asyncio loop.
"""
if not (lock := _LOCK_MAP.get(key)):
Expand Down
2 changes: 1 addition & 1 deletion flux_local/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def parse_yaml(cls, content: str) -> "BaseManifest":

def yaml(self, exclude: dict[str, Any] | None = None) -> str:
"""Return a YAML string representation of compact_dict."""
return yaml_encode(self, self.__class__)
return yaml_encode(self, self.__class__) # type: ignore[return-value]

class Config(BaseConfig):
omit_none = True
Expand Down
8 changes: 0 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ warn_unused_configs = true
warn_unused_ignores = true
disable_error_code = [
"import-untyped",
# TODO: Fix these warnings
"assignment",
"attr-defined",
"return-value",
"no-untyped-def",
"union-attr",
"assignment",
"arg-type",
]
extra_checks = false
check_untyped_defs = true
Expand Down
10 changes: 6 additions & 4 deletions script/run-mypy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

set -o errexit

# Change directory to the project root directory.
cd "$(dirname "$0")"
# other common virtualenvs
my_path=$(git rev-parse --show-toplevel)

pip3 install -r requirements_dev.txt --no-input --quiet
if [ -f "${my_path}/venv/bin/activate" ]; then
. "${my_path}/venv/bin/activate"
fi

mypy .
mypy ${my_path}
3 changes: 2 additions & 1 deletion tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

from flux_local.command import Command, CommandException, run, run_piped
from flux_local.command import Command, run, run_piped
from flux_local.exceptions import CommandException


async def test_command() -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def test_kustomization_visitor(snapshot: SnapshotAssertion) -> None:
query.path.path = TESTDATA

stream = io.StringIO()
visits: list[tuple[str, str, str, str]] = []
visits: list[tuple[str, str, str]] = []

async def write(x: Path, y: Any, cmd: Kustomize | None) -> None:
visits.append((str(x), y.namespace, y.name))
Expand All @@ -112,7 +112,7 @@ async def test_helm_repo_visitor(snapshot: SnapshotAssertion) -> None:
query = ResourceSelector()
query.path.path = TESTDATA

visits: list[tuple[str, str, str, str]] = []
visits: list[tuple[str, str, str]] = []

async def append(x: Path, y: Any, z: Any) -> None:
visits.append((str(x), y.namespace, y.name))
Expand All @@ -132,7 +132,7 @@ async def test_helm_release_visitor(snapshot: SnapshotAssertion) -> None:
query = ResourceSelector()
query.path.path = TESTDATA

visits: list[tuple[str, str, str, str]] = []
visits: list[tuple[str, str, str]] = []

async def append(x: Path, y: Any, z: Any) -> None:
visits.append((str(x), y.namespace, y.name))
Expand Down
Loading