Skip to content

Commit

Permalink
ci(linters): address more pyright warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Matsuoka <[email protected]>
  • Loading branch information
cmatsuoka committed Sep 10, 2023
1 parent 1a507e1 commit c0574b6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
4 changes: 2 additions & 2 deletions craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class _Dispatcher(craft_cli.Dispatcher):
@property
def parsed_args(self) -> argparse.Namespace:
"""The map of parsed command-line arguments."""
return self._parsed_command_args
return self._parsed_command_args or argparse.Namespace()


@final
Expand Down Expand Up @@ -129,7 +129,7 @@ def cache_dir(self) -> str:
# xdg types: https://github.com/python/typeshed/pull/10163
return save_cache_path(self.app.name) # type: ignore[no-any-return]

def _configure_services(self, build_for: str) -> None:
def _configure_services(self, build_for: str | None) -> None:
"""Configure additional keyword arguments for any service classes.
Any child classes that override this must either call this directly or must
Expand Down
3 changes: 1 addition & 2 deletions craft_application/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
This defines the structure of the input file (e.g. snapcraft.yaml)
"""
import abc
import dataclasses
from typing import Any, Dict, List, Optional, Union

Expand Down Expand Up @@ -80,6 +79,6 @@ def effective_base(self) -> Any: # noqa: ANN401 app specific classes can improv
return self.base
raise RuntimeError("Could not determine effective base")

@abc.abstractmethod
def get_build_plan(self) -> List[BuildInfo]:
"""Obtain the list of architectures and bases from the project file."""
raise NotImplementedError
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from collections.abc import Iterator


class MyProject(craft_application.models.Project):
class MyProject(models.Project):
def get_build_plan(self) -> list[models.BuildInfo]:
arch = craft_application.util.get_host_architecture()
arch = util.get_host_architecture()
return [models.BuildInfo(arch, arch, bases.BaseName("ubuntu", "22.04"))]


Expand All @@ -50,7 +50,7 @@ def app_metadata() -> craft_application.AppMetadata:


@pytest.fixture()
def fake_project() -> MyProject:
def fake_project() -> models.Project:
return MyProject(
name="full-project", # pyright: ignore[reportGeneralTypeIssues]
title="A fully-defined project", # pyright: ignore[reportGeneralTypeIssues]
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/services/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pytest
from craft_application.models import BuildInfo
from craft_application.util import get_host_architecture
from craft_providers import bases


@pytest.mark.parametrize(
Expand Down Expand Up @@ -57,7 +58,7 @@ def test_provider_lifecycle(
provider_service.get_provider(name)

arch = get_host_architecture()
build_info = BuildInfo(arch, arch, craft_providers.bases.BaseName(*base_name))
build_info = BuildInfo(arch, arch, bases.BaseName(*base_name))
instance = provider_service.instance(build_info, work_dir=snap_safe_tmp_path)
executor = None
try:
Expand Down
28 changes: 11 additions & 17 deletions tests/unit/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@
"""Tests for BaseProject"""
import pathlib
from textwrap import dedent
from typing import List, Optional
from typing import Optional

import pytest
from craft_application.errors import CraftValidationError
from craft_application.models import BuildInfo, Project


class MyProject(Project):
def get_build_plan(self) -> List[BuildInfo]:
return []

from craft_application.models import Project

PROJECTS_DIR = pathlib.Path(__file__).parent / "project_models"
PARTS_DICT = {"my-part": {"plugin": "nil"}}
# pyright doesn't like these types and doesn't have a pydantic plugin like mypy.
# Because of this, we need to silence several errors in these constants.
BASIC_PROJECT = MyProject(
BASIC_PROJECT = Project(
name="project-name", # pyright: ignore[reportGeneralTypeIssues]
version="1.0", # pyright: ignore[reportGeneralTypeIssues]
parts=PARTS_DICT,
Expand All @@ -42,7 +36,7 @@ def get_build_plan(self) -> List[BuildInfo]:
"version": "1.0",
"parts": PARTS_DICT,
}
FULL_PROJECT = MyProject(
FULL_PROJECT = Project(
name="full-project", # pyright: ignore[reportGeneralTypeIssues]
title="A fully-defined project", # pyright: ignore[reportGeneralTypeIssues]
base="core24",
Expand Down Expand Up @@ -88,23 +82,23 @@ def test_marshal(project, project_dict):
[(BASIC_PROJECT, BASIC_PROJECT_DICT), (FULL_PROJECT, FULL_PROJECT_DICT)],
)
def test_unmarshal_success(project, project_dict):
assert MyProject.unmarshal(project_dict) == project
assert Project.unmarshal(project_dict) == project


@pytest.mark.parametrize("data", [None, [], (), 0, ""])
def test_unmarshal_error(data):
with pytest.raises(TypeError):
MyProject.unmarshal(data)
Project.unmarshal(data)


@pytest.mark.parametrize("project", [BASIC_PROJECT, FULL_PROJECT])
def test_marshal_then_unmarshal(project):
assert MyProject.unmarshal(project.marshal()) == project
assert Project.unmarshal(project.marshal()) == project


@pytest.mark.parametrize("project_dict", [BASIC_PROJECT_DICT, FULL_PROJECT_DICT])
def test_unmarshal_then_marshal(project_dict):
assert MyProject.unmarshal(project_dict).marshal() == project_dict
assert Project.unmarshal(project_dict).marshal() == project_dict


@pytest.mark.parametrize(
Expand All @@ -116,7 +110,7 @@ def test_unmarshal_then_marshal(project_dict):
)
def test_from_yaml_file_success(project_file, expected):
with project_file.open():
actual = MyProject.from_yaml_file(project_file)
actual = Project.from_yaml_file(project_file)

assert expected == actual

Expand All @@ -130,7 +124,7 @@ def test_from_yaml_file_success(project_file, expected):
)
def test_from_yaml_file_failure(project_file, error_class):
with pytest.raises(error_class):
MyProject.from_yaml_file(project_file)
Project.from_yaml_file(project_file)


@pytest.mark.parametrize(
Expand All @@ -153,7 +147,7 @@ def test_effective_base_is_base(project):
assert project.effective_base == project.base


class FakeBuildBaseProject(MyProject):
class FakeBuildBaseProject(Project):
build_base: Optional[str]


Expand Down
15 changes: 9 additions & 6 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import pytest_check
from craft_application import application, commands, services
from craft_application.models import BuildInfo
from craft_application.util import (
get_host_architecture, # pyright: ignore[reportGeneralTypeIssues]
)
from craft_providers import bases

EMPTY_COMMAND_GROUP = craft_cli.CommandGroup("FakeCommands", [])
Expand All @@ -48,7 +51,7 @@ def app(app_metadata, fake_services):

@pytest.fixture()
def mock_dispatcher(monkeypatch):
dispatcher = mock.Mock(spec_set=craft_application.application._Dispatcher)
dispatcher = mock.Mock(spec_set=application._Dispatcher)
monkeypatch.setattr(
"craft_application.application._Dispatcher", mock.Mock(return_value=dispatcher)
)
Expand Down Expand Up @@ -101,7 +104,7 @@ def test_run_managed_success(app, fake_project, emitter):
app.services.provider = mock_provider
app.project = fake_project

arch = craft_application.util.get_host_architecture()
arch = get_host_architecture()
app.run_managed(arch)

emitter.assert_debug(f"Running testcraft in {arch} instance...")
Expand All @@ -115,7 +118,7 @@ def test_run_managed_failure(app, fake_project):
app.project = fake_project

with pytest.raises(craft_providers.ProviderError) as exc_info:
app.run_managed(craft_application.util.get_host_architecture())
app.run_managed(get_host_architecture())

assert exc_info.value.brief == "Failed to execute testcraft in instance."

Expand All @@ -125,7 +128,7 @@ def test_run_managed_multiple(app, fake_project, emitter, monkeypatch):
app.services.provider = mock_provider
app.project = fake_project

arch = craft_application.util.get_host_architecture()
arch = get_host_architecture()
monkeypatch.setattr(
app.project.__class__,
"get_build_plan",
Expand All @@ -145,7 +148,7 @@ def test_run_managed_specified(app, fake_project, emitter, monkeypatch):
app.services.provider = mock_provider
app.project = fake_project

arch = craft_application.util.get_host_architecture()
arch = get_host_architecture()
monkeypatch.setattr(
app.project.__class__,
"get_build_plan",
Expand Down Expand Up @@ -242,7 +245,7 @@ def test_run_success_managed(monkeypatch, app, fake_project):
def test_run_success_managed_with_arch(monkeypatch, app, fake_project):
app.project = fake_project
app.run_managed = mock.Mock()
arch = craft_application.util.get_host_architecture()
arch = get_host_architecture()
monkeypatch.setattr(sys, "argv", ["testcraft", "pull", f"--build-for={arch}"])

pytest_check.equal(app.run(), 0)
Expand Down

0 comments on commit c0574b6

Please sign in to comment.