Skip to content

Commit 1907f7a

Browse files
authored
Merge pull request #1250 from dimaqq/upstream-type-fixes
#1250 - update type hints in juju/application.py, pyright is now happy - correct type hints for facade.Type - work around pytest-dev/pytest-asyncio#1039
2 parents a58645e + e7bcdc9 commit 1907f7a

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

juju/application.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
3+
from __future__ import annotations
34

45
import asyncio
56
import hashlib
67
import json
78
import logging
89
from pathlib import Path
9-
from typing import Dict, List, Optional, Union
1010

1111
from typing_extensions import deprecated
1212

@@ -61,7 +61,7 @@ def min_units(self) -> int:
6161
return self.safe_data["min-units"]
6262

6363
@property
64-
def constraints(self) -> Dict[str, Union[str, int, bool]]:
64+
def constraints(self) -> dict[str, str | int | bool]:
6565
return self.safe_data["constraints"]
6666

6767
@property
@@ -112,7 +112,7 @@ def subordinate_units(self):
112112
return [u for u in self.units if u.is_subordinate]
113113

114114
@property
115-
def relations(self) -> List[Relation]:
115+
def relations(self) -> list[Relation]:
116116
return [rel for rel in self.model.relations if rel.matches(self.name)]
117117

118118
def related_applications(self, endpoint_name=None):
@@ -579,7 +579,7 @@ def attach_resource(self, resource_name, file_name, file_obj):
579579
data = file_obj.read()
580580

581581
headers["Content-Type"] = "application/octet-stream"
582-
headers["Content-Length"] = len(data)
582+
headers["Content-Length"] = str(len(data))
583583
data_bytes = data if isinstance(data, bytes) else bytes(data, "utf-8")
584584
headers["Content-Sha384"] = hashlib.sha384(data_bytes).hexdigest()
585585

@@ -589,7 +589,7 @@ def attach_resource(self, resource_name, file_name, file_obj):
589589

590590
headers["Content-Disposition"] = f'form-data; filename="{file_name}"'
591591
headers["Accept-Encoding"] = "gzip"
592-
headers["Bakery-Protocol-Version"] = 3
592+
headers["Bakery-Protocol-Version"] = "3"
593593
headers["Connection"] = "close"
594594

595595
conn.request("PUT", url, data, headers)
@@ -638,14 +638,15 @@ async def run(self, command, timeout=None):
638638
)
639639

640640
@property
641-
def charm_name(self):
641+
def charm_name(self) -> str:
642642
"""Get the charm name of this application
643643
644644
:return str: The name of the charm
645645
"""
646-
return URL.parse(self.charm_url).name
646+
return URL.parse(self.safe_data["charm-url"]).name
647647

648648
@property
649+
@deprecated("Application.charm_url is deprecated and will be removed in v4")
649650
def charm_url(self):
650651
"""Get the charm url for this application
651652
@@ -733,14 +734,14 @@ async def set_constraints(self, constraints):
733734

734735
async def refresh(
735736
self,
736-
channel: Optional[str] = None,
737+
channel: str | None = None,
737738
force: bool = False,
738739
force_series: bool = False,
739740
force_units: bool = False,
740-
path: Optional[Union[Path, str]] = None,
741-
resources: Optional[Dict[str, str]] = None,
742-
revision: Optional[int] = None,
743-
switch: Optional[str] = None,
741+
path: Path | str | None = None,
742+
resources: dict[str, str] | None = None,
743+
revision: int | None = None,
744+
switch: str | None = None,
744745
):
745746
"""Refresh the charm for this application.
746747
@@ -841,15 +842,17 @@ async def refresh(
841842
# need to process the given resources, as they can be
842843
# paths or revisions
843844
_arg_res_filenames = {}
844-
_arg_res_revisions = {}
845+
_arg_res_revisions: dict[str, str] = {}
845846
for res, filename_or_rev in arg_resources.items():
846847
if isinstance(filename_or_rev, int):
847848
_arg_res_revisions[res] = filename_or_rev
848849
else:
849850
_arg_res_filenames[res] = filename_or_rev
850851

851852
# Get the existing resources from the ResourcesFacade
852-
request_data = [client.Entity(self.tag)]
853+
request_data: list[client.Entity | client.CharmResource] = [
854+
client.Entity(self.tag)
855+
]
853856
resources_facade = client.ResourcesFacade.from_connection(self.connection)
854857
response = await resources_facade.ListResources(entities=request_data)
855858
existing_resources = {
@@ -930,8 +933,8 @@ async def local_refresh(
930933
force: bool,
931934
force_series: bool,
932935
force_units: bool,
933-
path: Union[Path, str],
934-
resources: Optional[Dict[str, str]],
936+
path: Path | str,
937+
resources: dict[str, str] | None,
935938
):
936939
"""Refresh the charm for this application with a local charm.
937940
@@ -1012,8 +1015,8 @@ async def get_metrics(self):
10121015

10131016
def _refresh_origin(
10141017
current_origin: client.CharmOrigin,
1015-
channel: Optional[str] = None,
1016-
revision: Optional[int] = None,
1018+
channel: str | None = None,
1019+
revision: int | None = None,
10171020
) -> client.CharmOrigin:
10181021
chan = None if channel is None else Channel.parse(channel).normalize()
10191022

juju/client/facade.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import packaging.version
2020
import typing_inspect
21-
from typing_extensions import TypeAlias
21+
from typing_extensions import Self, TypeAlias
2222

2323
from . import codegen
2424

@@ -661,6 +661,9 @@ def default(self, obj: _RichJson) -> _Json:
661661

662662

663663
class Type:
664+
_toSchema: dict[str, str]
665+
_toPy: dict[str, str]
666+
664667
def connect(self, connection):
665668
self.connection = connection
666669

@@ -678,7 +681,7 @@ async def rpc(self, msg: dict[str, _RichJson]) -> _Json:
678681
return result
679682

680683
@classmethod
681-
def from_json(cls, data: Type | str | dict[str, Any] | list[Any]) -> Type | None:
684+
def from_json(cls, data: Type | str | dict[str, Any] | list[Any]) -> Self | None:
682685
def _parse_nested_list_entry(expr, result_dict):
683686
if isinstance(expr, str):
684687
if ">" in expr or ">=" in expr:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies = [
4040
dev = [
4141
"typing-inspect",
4242
"pytest",
43-
"pytest-asyncio",
43+
"pytest-asyncio <= 0.25.0", # https://github.com/pytest-dev/pytest-asyncio/issues/1039
4444
"Twine",
4545
"freezegun",
4646
]

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"dev": [
3939
"typing-inspect",
4040
"pytest",
41-
"pytest-asyncio",
41+
"pytest-asyncio <= 0.25.0", # https://github.com/pytest-dev/pytest-asyncio/issues/1039
4242
"Twine",
4343
"freezegun",
4444
]

0 commit comments

Comments
 (0)