Skip to content

Commit

Permalink
style(type): fix pyright AttributeAccessIssues
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Dec 18, 2024
1 parent 47e6dc2 commit 8362b5e
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
5 changes: 3 additions & 2 deletions charmcraft/application/commands/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

from craft_cli import emit

from charmcraft import extensions, utils
from charmcraft import utils
from charmcraft.application.commands import base
from charmcraft.extensions import registry


class ListExtensionsCommand(base.CharmcraftCommand):
Expand All @@ -40,7 +41,7 @@ class ListExtensionsCommand(base.CharmcraftCommand):

def run(self, parsed_args: argparse.Namespace):
"""Print the list of available extensions and their bases."""
extension_data = extensions.registry.get_extensions()
extension_data = registry.get_extensions()

if not parsed_args.format:
extension_data = [
Expand Down
4 changes: 2 additions & 2 deletions charmcraft/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Revision:
created_at: datetime.datetime
status: str
errors: list[Error]
bases: list[Base]
bases: list[Base | None]


@dataclasses.dataclass(frozen=True)
Expand All @@ -152,7 +152,7 @@ class Release:
channel: str
expires_at: datetime.datetime
resources: list[Resource]
base: Base
base: Base | None


@dataclasses.dataclass(frozen=True)
Expand Down
4 changes: 2 additions & 2 deletions charmcraft/store/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _build_errors(item):

def _build_revision(item: dict[str, Any]) -> Revision:
"""Build a Revision from a response item."""
bases = [Base(**base) for base in item["bases"] if base is not None]
bases = [(None if base is None else Base(**base)) for base in item["bases"]]
return Revision(
revision=item["revision"],
version=item["version"],
Expand Down Expand Up @@ -401,7 +401,7 @@ def list_releases(
# `datetime.datetime.fromisoformat` is available only since Py3.7
expires_at = parser.parse(expires_at)
resources = [_build_resource(r) for r in item["resources"]]
base = Base(**item["base"])
base = None if item["base"] is None else Base(**item["base"])
channel_map.append(
Release(
revision=item["revision"],
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ include = [
"tests/integration",
]
analyzeUnannotatedFunctions = false
reportAttributeAccessIssue = "warning"
reportIncompatibleVariableOverride = "warning"
reportOptionalMemberAccess = "warning"

Expand Down
8 changes: 4 additions & 4 deletions tests/integration/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import charmcraft
from charmcraft import errors
from charmcraft.application import commands
from charmcraft.application.commands import init
from charmcraft.utils import S_IXALL

with contextlib.suppress(ImportError):
Expand Down Expand Up @@ -97,7 +97,7 @@

@pytest.fixture
def init_command():
return commands.InitCommand(
return init.InitCommand(
{"app": charmcraft.application.APP_METADATA, "services": None}
)

Expand All @@ -107,7 +107,7 @@ def create_namespace(
name="my-charm",
author="J Doe",
force=False,
profile=commands.init.DEFAULT_PROFILE,
profile=init.DEFAULT_PROFILE,
project_dir: pathlib.Path | None = None,
):
"""Helper to create a valid namespace."""
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_executable_set(new_path, init_command):
bool(os.getenv("RUNNING_TOX")) and sys.version_info < (3, 11),
reason="does not work inside tox in Python3.10 and below",
)
@pytest.mark.parametrize("profile", list(commands.init.PROFILES))
@pytest.mark.parametrize("profile", list(init.PROFILES))
def test_tox_success(new_path, init_command, profile):
# fix the PYTHONPATH and PATH so the tests in the initted environment use our own
# virtualenv libs and bins (if any), as they need them, but we're not creating a
Expand Down
22 changes: 10 additions & 12 deletions tests/unit/commands/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import argparse
import pathlib
from unittest import mock

import craft_cli
import pytest
Expand Down Expand Up @@ -133,13 +134,12 @@ def test_pack_update_charm_libs_empty(
simple_charm,
emitter: RecordingEmitter,
service_factory: services.ServiceFactory,
mock_store_anonymous_client: mock.Mock,
):
simple_charm.charm_libs = [models.CharmLib(lib="my_charm.my_lib", version="0.1")]
store_lib = Library("lib_id", "my_lib", "my_charm", 0, 1, "Lib contents", "hash")
service_factory.store.anonymous_client.fetch_libraries_metadata.return_value = [
store_lib
]
service_factory.store.anonymous_client.get_library.return_value = store_lib
mock_store_anonymous_client.fetch_libraries_metadata.return_value = [store_lib]
mock_store_anonymous_client.get_library.return_value = store_lib

pack._update_charm_libs()

Expand All @@ -156,16 +156,15 @@ def test_pack_update_charm_libs_no_update(
simple_charm,
emitter: RecordingEmitter,
service_factory: services.ServiceFactory,
mock_store_anonymous_client: mock.Mock,
):
simple_charm.charm_libs = [models.CharmLib(lib="my_charm.my_lib", version="0.1")]
store_lib = Library("lib_id", "my_lib", "my_charm", 0, 1, "Lib contents", "hash")
path = fake_project_dir / utils.get_lib_path("my_charm", "my_lib", 0)
path.parent.mkdir(parents=True)
path.write_text("LIBID='id'\nLIBAPI=0\nLIBPATCH=1")
service_factory.store.anonymous_client.fetch_libraries_metadata.return_value = [
store_lib
]
service_factory.store.anonymous_client.get_library.return_value = store_lib
mock_store_anonymous_client.fetch_libraries_metadata.return_value = [store_lib]
mock_store_anonymous_client.get_library.return_value = store_lib

pack._update_charm_libs()

Expand All @@ -181,16 +180,15 @@ def test_pack_update_charm_libs_needs_update(
simple_charm,
emitter: RecordingEmitter,
service_factory: services.ServiceFactory,
mock_store_anonymous_client: mock.Mock,
):
simple_charm.charm_libs = [models.CharmLib(lib="my_charm.my_lib", version="0.2")]
store_lib = Library("lib_id", "my_lib", "my_charm", 0, 2, "Lib contents", "hash")
path = fake_project_dir / utils.get_lib_path("my_charm", "my_lib", 0)
path.parent.mkdir(parents=True)
path.write_text("LIBID='id'\nLIBAPI=0\nLIBPATCH=1")
service_factory.store.anonymous_client.fetch_libraries_metadata.return_value = [
store_lib
]
service_factory.store.anonymous_client.get_library.return_value = store_lib
mock_store_anonymous_client.fetch_libraries_metadata.return_value = [store_lib]
mock_store_anonymous_client.get_library.return_value = store_lib

pack._update_charm_libs()

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/commands/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def test_register_bundle_warning(monkeypatch: pytest.MonkeyPatch, emitter):

emitter.assert_progress(
"\u001b[31mWARNING:\u001b[0m New bundle registration will stop working on 2024-11-01. For "
f"more information, see: {commands.store.BUNDLE_REGISTRATION_REMOVAL_URL}",
f"more information, see: {store_commands.BUNDLE_REGISTRATION_REMOVAL_URL}",
permanent=True,
)
mock_store.assert_called()
Expand All @@ -358,6 +358,6 @@ def test_register_bundle_error(monkeypatch: pytest.MonkeyPatch, emitter):

emitter.assert_message(
"\u001b[31mERROR:\u001b[0m New bundle registration is discontinued as of 2024-11-01. For "
f"more information, see: {commands.store.BUNDLE_REGISTRATION_REMOVAL_URL}",
f"more information, see: {store_commands.BUNDLE_REGISTRATION_REMOVAL_URL}",
)
mock_store.assert_not_called()
3 changes: 2 additions & 1 deletion tests/unit/test_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#
# For further info, check https://github.com/canonical/charmcraft

import pydantic
import pytest
from pyfakefs.fake_filesystem import FakeFilesystem

Expand Down Expand Up @@ -84,7 +85,7 @@ def test_partconfig_strict_dependencies_failure(

part_config.update(MINIMAL_STRICT_CHARM)

with pytest.raises(Exception) as exc_info:
with pytest.raises(pydantic.ValidationError) as exc_info:
parts.process_part_config(part_config)

assert message in {e["msg"] for e in exc_info.value.errors()}

0 comments on commit 8362b5e

Please sign in to comment.