Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add proper return types to resource sequence methods #368

Open
wants to merge 6 commits into
base: tdstein/import-typing-extensions
Choose a base branch
from

Conversation

tdstein
Copy link
Collaborator

@tdstein tdstein commented Dec 17, 2024

No description provided.

Copy link

github-actions bot commented Dec 17, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
1793 1711 95% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/posit/connect/client.py 99% 🟢
src/posit/connect/jobs.py 0% 🟢
src/posit/connect/resources.py 92% 🟢
TOTAL 64% 🟢

updated for commit: 35e0364 by action🐍

@tdstein tdstein force-pushed the tdstein/fix-protocol-return-types branch from 9a22be7 to 39e1eb0 Compare December 17, 2024 18:44
response = self._ctx.client.post(self._path, json=attributes)
result = response.json()
uid = result[self._uid]
path = posixpath.join(self._path, uid)
return _Resource(self._ctx, path, **result)
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to avoid this cast, but I haven't figured it out yet...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't either... I tried to do this locally before you opened the PR. 😜

@tdstein tdstein requested a review from schloerke December 17, 2024 19:00
Copy link
Collaborator

@schloerke schloerke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple typing style requests.

One question on the covariant type var.

self,
ctx: Context,
path: str,
factory: ResourceFactory[_T_co] = _Resource,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference is to put uncommon args that have defaults behind some sort of "name requirement" mechanism.

Suggested change
factory: ResourceFactory[_T_co] = _Resource,
*,
factory: ResourceFactory[_T_co] = _Resource,

self,
ctx: Context,
path: str,
factory: ResourceFactory[_T_co] = _Resource,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not adjust factory or uid, but their default values are shared objects between instances: https://www.pythonmorsels.com/mutable-default-arguments/

(No changes necessary... just a thought to file away for a different debug day)

@@ -89,38 +93,50 @@ def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride
super().update(**result)


T = TypeVar("T", bound=Resource)
_T = TypeVar("_T", bound=Resource)
_T_co = TypeVar("_T_co", bound=Resource, covariant=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A perfect use case!

Linking for my own reminder: https://stackoverflow.com/a/61737894/591574

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is it necessary since we're cast()ing?

I think we might be able to get away with a invariant type.

Suggested change
_T_co = TypeVar("_T_co", bound=Resource, covariant=True)
_R = TypeVar("_R", bound=Resource)

(R for factory R eturn value?)

Comment on lines +206 to +207
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
resource: _T_co = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)

Comment on lines +153 to +154
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From other repos, I've used an explicit type expectation before casting the value.

        resource: _T_co = self._factory(self._ctx, path, **result)
        resource = cast(_T, resource)

While it feels redundant, it gives me confidence if we ever change ._factory() and its return type. This way, we know we are transferring from a _T_co to _T (and not an Any to a _T).

Comment on lines +174 to +175
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
resource: _T_co = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)

Comment on lines +164 to +165
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resource = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)
resource: _T_co = self._factory(self._ctx, path, **result)
resource = cast(_T, resource)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants