11# Copyright 2023 Canonical Ltd.
22# Licensed under the Apache V2, see LICENCE file for details.
3+ from __future__ import annotations
34
45import asyncio
56import hashlib
67import json
78import logging
89from pathlib import Path
9- from typing import Dict , List , Optional , Union
1010
1111from 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
10131016def _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
0 commit comments