Skip to content

Commit 646d90d

Browse files
committed
chore: improve codebase with better code pratices
1 parent 0924020 commit 646d90d

7 files changed

Lines changed: 31 additions & 26 deletions

File tree

packages/polywrap-client/tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def test_invoke(
5757
manifest=simple_wrap_manifest
5858
)
5959
uri_wrapper = UriWrapper(uri=Uri("ens/wrapper.eth"), wrapper=wrapper)
60-
resolver = StaticResolver._from([uri_wrapper])
60+
resolver = StaticResolver.from_list([uri_wrapper]).unwrap()
6161

6262
config = PolywrapClientConfig(resolver=resolver)
6363
client = PolywrapClient(config=config)

packages/polywrap-uri-resolvers/polywrap_uri_resolvers/abc/resolver_with_history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from polywrap_result import Result
55

66

7-
class ResolverWithHistory(IUriResolver, ABC):
7+
class IResolverWithHistory(IUriResolver, ABC):
88
async def try_resolve_uri(self, uri: Uri, client: Client, resolution_context: IUriResolutionContext):
99
result = await self._try_resolve_uri(uri, client, resolution_context)
1010
step = IUriResolutionStep(

packages/polywrap-uri-resolvers/polywrap_uri_resolvers/package_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from .abc import ResolverWithHistory
1+
from .abc import IResolverWithHistory
22
from polywrap_core import Uri, UriPackageOrWrapper, IWrapPackage, UriResolutionResult
33
from polywrap_result import Result
44

55

6-
class PackageResolver(ResolverWithHistory):
6+
class PackageResolver(IResolverWithHistory):
77
uri: Uri
88
wrap_package: IWrapPackage
99

packages/polywrap-uri-resolvers/polywrap_uri_resolvers/static_resolver.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, cast
22
from polywrap_core import IUriResolutionStep, Wrapper, IWrapPackage, UriResolutionResult, IUriResolver, UriPackageOrWrapper, Uri, Client, IUriResolutionContext, UriPackage, UriWrapper
3-
from polywrap_result import Result
3+
from polywrap_result import Result, Err, Ok
44

55
from .helpers import UriResolverLike
66

@@ -12,37 +12,42 @@ def __init__(self, uri_map: dict[str, UriPackageOrWrapper]):
1212
self.uri_map = uri_map
1313

1414
@staticmethod
15-
def _from(static_resolver_likes: List[UriResolverLike]) -> "StaticResolver":
15+
def from_list(static_resolver_likes: List[UriResolverLike]) -> Result["StaticResolver"]:
1616
uri_map: dict[str, UriPackageOrWrapper] = dict()
1717
for static_resolver_like in static_resolver_likes:
1818
if type(static_resolver_like) == list:
19-
resolver = StaticResolver._from(cast(List[UriResolverLike], static_resolver_like))
20-
for uri, package_or_wrapper in resolver.uri_map.items():
19+
resolver = StaticResolver.from_list(cast(List[UriResolverLike], static_resolver_like))
20+
for uri, package_or_wrapper in resolver.unwrap().uri_map.items():
2121
uri_map[uri] = package_or_wrapper
2222

23-
elif hasattr(static_resolver_like, "uri") and hasattr(static_resolver_like, "package"):
23+
elif isinstance(static_resolver_like, UriPackage):
2424
uri_package = UriPackage(uri=static_resolver_like.uri, package=static_resolver_like.package) # type: ignore
2525
uri_map[uri_package.uri.uri] = uri_package
26-
elif hasattr(static_resolver_like, "uri") and hasattr(static_resolver_like, "wrapper"):
26+
elif isinstance(static_resolver_like, UriWrapper):
2727
uri_wrapper = UriWrapper(uri=static_resolver_like.uri, wrapper=static_resolver_like.wrapper) # type: ignore
2828
uri_map[uri_wrapper.uri.uri] = uri_wrapper
29+
elif isinstance(static_resolver_like, Uri):
30+
uri_map[static_resolver_like.uri] = static_resolver_like
2931
else:
30-
raise Exception("Unknown static-resolver-like type provided.")
32+
return Err(Exception("Unknown static-resolver-like type provided."))
3133

32-
return StaticResolver(uri_map)
34+
return Ok(StaticResolver(uri_map))
3335

3436
async def try_resolve_uri(self, uri: Uri, client: Client, resolution_context: IUriResolutionContext) -> Result["UriPackageOrWrapper"]:
35-
package_or_wrapper = self.uri_map.get(uri.uri)
37+
uri_package_or_wrapper = self.uri_map.get(uri.uri)
3638

3739
result: Result[UriPackageOrWrapper] = UriResolutionResult.ok(uri)
3840
description: str = "StaticResolver - Miss"
3941

40-
if package_or_wrapper:
41-
if hasattr(package_or_wrapper, "package"):
42-
result = UriResolutionResult.ok(uri, cast(IWrapPackage, package_or_wrapper.package))
42+
if uri_package_or_wrapper:
43+
if isinstance(uri_package_or_wrapper, UriPackage):
44+
result = UriResolutionResult.ok(uri, uri_package_or_wrapper.package)
4345
description = f"Static - Package ({uri.uri})"
44-
elif hasattr(package_or_wrapper, "wrapper"):
45-
result = UriResolutionResult.ok(uri, None, cast(Wrapper, package_or_wrapper.wrapper))
46+
elif isinstance(uri_package_or_wrapper, UriWrapper):
47+
result = UriResolutionResult.ok(uri, None, uri_package_or_wrapper.wrapper)
48+
description = f"Static - Wrapper ({uri.uri})"
49+
elif isinstance(uri_package_or_wrapper, Uri):
50+
result = UriResolutionResult.ok(uri)
4651
description = f"Static - Wrapper ({uri.uri})"
4752

4853
step = IUriResolutionStep(source_uri=uri, result=result, description=description)

packages/polywrap-uri-resolvers/polywrap_uri_resolvers/uri_resolver_wrapper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from typing import Optional, Union, cast
22
from polywrap_core import Uri, Client, IUriResolutionContext, UriPackageOrWrapper
3-
from polywrap_uri_resolvers import ResolverWithHistory
3+
from polywrap_uri_resolvers import IResolverWithHistory
44
from polywrap_result import Result, Ok, Err
55

6-
class UriResolverWrapper(ResolverWithHistory):
6+
class UriResolverWrapper(IResolverWithHistory):
77
implementation_uri: Uri
88

99
def __init__(self, uri: Uri) -> None:
1010
self.implementation_uri = uri
1111

1212
def get_step_description(self) -> str:
13-
return super().get_step_description()
13+
return ""
1414

1515
async def _try_resolve_uri(
1616
self,
@@ -19,7 +19,7 @@ async def _try_resolve_uri(
1919
resolution_context: IUriResolutionContext
2020
) -> Result[UriPackageOrWrapper]:
2121
result = await try_resolve_uri_with_implementation(uri, self.implementation_uri, client, resolution_context)
22-
22+
2323
if result.is_err():
2424
return cast(Err, result)
2525

packages/polywrap-uri-resolvers/polywrap_uri_resolvers/wrapper_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from .abc import ResolverWithHistory
1+
from .abc import IResolverWithHistory
22
from polywrap_core import Uri, UriPackageOrWrapper, Wrapper, UriResolutionResult
33
from polywrap_result import Result
44

55

6-
class WrapperResolver(ResolverWithHistory):
6+
class WrapperResolver(IResolverWithHistory):
77
uri: Uri
88
wrapper: Wrapper
99

packages/polywrap-uri-resolvers/tests/test_static_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ async def test_static_resolver(
4848
uri_wrapper = UriWrapper(uri=Uri("ens/wrapper.eth"), wrapper=wrapper)
4949
uri_package = UriPackage(uri=Uri("ens/package.eth"), package=package)
5050

51-
resolver = StaticResolver._from([ uri_package, uri_wrapper, [
51+
resolver = StaticResolver.from_list([ uri_package, uri_wrapper, [
5252
UriPackage(uri=Uri("ens/nested-package.eth"), package=package)
53-
]])
53+
]]).unwrap()
5454

5555
resolution_context = UriResolutionContext()
5656
result = await resolver.try_resolve_uri(Uri("ens/package.eth"), PolywrapClient(), resolution_context)

0 commit comments

Comments
 (0)