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: Inject Client #12258

Merged
merged 7 commits into from
Mar 13, 2024
Merged

Refactor: Inject Client #12258

merged 7 commits into from
Mar 13, 2024

Conversation

aaazzam
Copy link
Contributor

@aaazzam aaazzam commented Mar 12, 2024

Prefect's inject_client decorator wraps an async function with an Optional[PrefectClient] kwarg, injecting an inferred or new client if none is provided.

This PR refactors the get_or_infer_client logic to a separate function. This will makes type-hinting much easier. Getting static type checkers to acknowledge that @inject_client is providing a default to an optional kwarg is hard.

Consider the _get_variable_by_name utility, which fails basic type hinting:

Screenshot 2024-03-12 at 2 15 40 PM

With get_or_infer_client factored out, we recover better type hinting:

Screenshot 2024-03-12 at 2 17 37 PM

Checklist

  • This pull request includes tests or only affects documentation.
  • This pull request includes a label categorizing the change e.g. maintenance, fix, feature, enhancement, docs.

For new functions or classes in the Python SDK:

  • This pull request includes helpful docstrings.

@aaazzam aaazzam requested a review from a team as a code owner March 12, 2024 18:19
Copy link

netlify bot commented Mar 12, 2024

Deploy Preview for prefect-docs-preview ready!

Name Link
🔨 Latest commit 69d6e88
🔍 Latest deploy log https://app.netlify.com/sites/prefect-docs-preview/deploys/65f0d013ce4bd000080841bb
😎 Deploy Preview https://deploy-preview-12258--prefect-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@aaazzam
Copy link
Contributor Author

aaazzam commented Mar 12, 2024

woof my fears are realized: the in-function imports were for avoiding circular imports

@aaazzam
Copy link
Contributor Author

aaazzam commented Mar 12, 2024

I've changed the devex to be get_or_create and a la Django it returns a tuple of the client and whether a new context was created

Copy link
Collaborator

@chrisguidry chrisguidry left a comment

Choose a reason for hiding this comment

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

🔥 Thank you!

@aaazzam aaazzam merged commit ae45e37 into main Mar 13, 2024
45 checks passed
@aaazzam aaazzam deleted the inject-client-refactor branch March 13, 2024 15:27
bunchesofdonald pushed a commit that referenced this pull request Mar 14, 2024
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.

None yet

2 participants