-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[typing] prefect.client #16265
[typing] prefect.client #16265
Conversation
622ebb8
to
e5e608c
Compare
CodSpeed Performance ReportMerging #16265 will not alter performanceComparing Summary
|
e5e608c
to
b602080
Compare
b602080
to
aa13337
Compare
fb5e9fa
to
4050ee8
Compare
4050ee8
to
735ecb1
Compare
Code now passes pyright checking in strict mode.
735ecb1
to
67d1b4f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mjpieters!
def _current_async_client( | ||
client: Union["PrefectClient", "SyncPrefectClient"], | ||
) -> TypeIs["PrefectClient"]: | ||
from prefect._internal.concurrency.event_loop import get_running_loop | ||
|
||
# Only a PrefectClient will have a _loop attribute that is the current loop | ||
return getattr(client, "_loop", None) == get_running_loop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commenting for visibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have used the wrong special form here. This type narrowing function returns a TypeIs
construct but now that I reread this it should have been a TypeGuard
instead.
That's because this function returns False in two different cases:
- the client is a
SynPrefectClient
instance. - the client is a
PrefectClient
instance, attached to a different loop.
The TypeIs
construct acts like an isinstance()
check, where the type checker can infer that if False is returned then the client must be a SyncPrefectClient
as that's the only type left out of the input Union. But that's not correct for case 2.
A TypeGuard
function explicitly doesn't make promises about types when False is returned and so fits this function much better.
It is a technically moot point in how this function is used in this module but an important distinction if you were to use this as an example 🤦.
TLDR:
- use
TypeIs[R]
when True means this is of typeR
and False means this is not of typeR
- use
TypeGuard[R]
when True means this meets conditions that tell me this is of typeR
but False means uh, I am not sure what this is and I don't care 🤷.
thank you @mjpieters ! |
Code now passes pyright checking in strict mode.