Skip to content

Commit 15f854a

Browse files
committed
Fix class duck typing using inspiration from #364
1 parent 666a186 commit 15f854a

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

src/posit/connect/content.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import posixpath
66
import time
77
from abc import abstractmethod
8-
from collections.abc import Mapping
9-
from posixpath import dirname
108

119
from typing_extensions import (
1210
TYPE_CHECKING,
1311
Any,
12+
Hashable,
1413
List,
1514
Literal,
1615
NotRequired,
@@ -29,14 +28,7 @@
2928
from .errors import ClientError
3029
from .oauth.associations import ContentItemAssociations
3130
from .permissions import Permissions
32-
from .resources import (
33-
Active,
34-
Resource,
35-
Resources,
36-
_Resource,
37-
_ResourceSequence,
38-
_ResourceUpdatePatchMixin,
39-
)
31+
from .resources import Active, Resource, Resources, _Resource, _ResourceSequence
4032
from .tags import ContentItemTags
4133
from .vanities import VanityMixin
4234
from .variants import Variants
@@ -48,16 +40,25 @@
4840
from .tasks import Task
4941

5042

43+
# TODO-barret: Replace with Resource class from https://github.com/posit-dev/posit-sdk-py/pull/364/files#diff-94b7dc3c7d7d7c7b1a5f25e06c37df5fc53e1921cb10d41d4f04b18a715fae55R72
44+
class ResourceP(Protocol):
45+
def __getitem__(self, key: Hashable, /) -> Any: ...
46+
47+
5148
def _assert_guid(guid: str):
5249
assert isinstance(guid, str), "Expected 'guid' to be a string"
5350
assert len(guid) > 0, "Expected 'guid' to be non-empty"
5451

5552

5653
# ContentItem Repository uses a PATCH method, not a PUT for updating.
57-
class _ContentItemRepositoryResource(_ResourceUpdatePatchMixin, _Resource): ...
54+
class _ContentItemRepository(_Resource):
55+
def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride]
56+
response = self._ctx.client.patch(self._path, json=attributes)
57+
result = response.json()
58+
super().update(**result)
5859

5960

60-
class ContentItemRepository(Mapping[str, Any]):
61+
class ContentItemRepository(ResourceP):
6162
"""
6263
Content items GitHub repository information.
6364
@@ -216,7 +217,7 @@ def oauth(self) -> ContentItemOAuth:
216217
@property
217218
def repository(self) -> ContentItemRepository | None:
218219
try:
219-
return _Resource(
220+
return _ContentItemRepository(
220221
self._ctx,
221222
f"v1/content/{self['guid']}/repository",
222223
)
@@ -256,10 +257,10 @@ def create_repository(self, /, **attributes) -> ContentItemRepository:
256257
ContentItemRepository
257258
"""
258259
path = f"v1/content/{self['guid']}/repository"
259-
response = self._ctx.session.put(path, json=attributes)
260+
response = self._ctx.client.session.put(path, json=attributes)
260261
result = response.json()
261262

262-
return _ContentItemRepositoryResource(
263+
return _ContentItemRepository(
263264
self._ctx,
264265
path,
265266
**result,

src/posit/connect/resources.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ def __init__(self, ctx: Context, path: str, /, **attributes):
6363
self._path = path
6464

6565

66-
class _ResourceP(Protocol):
67-
_ctx: Context
68-
_path: str
69-
70-
def destroy(self) -> None: ...
71-
72-
def update(self, **attributes): ...
73-
74-
7566
class _Resource(dict):
7667
def __init__(self, ctx: Context, path: str, /, **attributes):
7768
self._ctx = ctx
@@ -87,20 +78,6 @@ def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride
8778
super().update(**result)
8879

8980

90-
class _ResourceUpdatePatchMixin:
91-
def update(self: _ResourceP, **attributes): # type: ignore[reportIncompatibleMethodOverride]
92-
response = self._ctx.client.patch(self._path, json=attributes)
93-
result = response.json()
94-
super().update(**result)
95-
96-
97-
class _ResourcePatch(_Resource):
98-
def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride]
99-
response = self._ctx.client.patch(self._path, json=attributes)
100-
result = response.json()
101-
super().update(**result)
102-
103-
10481
class _ResourceSequence(Sequence):
10582
def __init__(self, ctx: Context, path: str, *, uid: str = "guid"):
10683
self._ctx = ctx

0 commit comments

Comments
 (0)