-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add autocomplete support for builds(<target>, populate_full_signature=True)
#224
Conversation
builds(<target>, populate_full_signature=True)
builds(<target>, populate_full_signature=True)
builds(<target>, populate_full_signature=True)
builds(<target>, populate_full_signature=True)
Is this only for Python 3.10+? |
No, this works for Python 3.6+. We use the backport of |
Note: I updated the original post substantially to reflect the fact that |
One unfortunate thing is that PyCharm can't keep up with almost any of these annotations.. It's type-checker is not good enough :/ |
@jgbos this is a pretty big PR, but I tried to keep the commits pretty modular and tidy. When you have the chance to review this, I recommend doing so commit-by-commit, rather than scrolling through all of the diffs by themselves. |
This PR:
builds(<target>, populate_full_signature=True)
.make_custom_builds_fn
to reflect relevant updated default values on the resultingbuilds
function. Specifically, this holds forzen_partial=True
andpopulate_full_signature=True
, respectively.ZenWrappers
a "public" type viahydra_zen.typing
Adding autocomplete for
builds(<target>, populate_full_signature=True)
Consider the following builds pattern:
Presently, IDEs / type-checkers cannot infer the signature to
Conf
, even though the user can infer that the full signature off
is reflected inConf
. This PR fixes that!Before:
After:
This means that instances of
Conf
are now amenable to static type-checking:Lastly, type-checkers are still able to "see" the correct type returned via
instantiate
:Adding overloads to
make_custom_builds_fn
Previously, we did not provide type-checkers with "dynamic" information about the builds-function produced by
make_custom_builds_fn
. Now, type-checkers can "see" some of the new defaults in thebuilds
function created bymake_custom_builds_fn
.Before:
After
This is especially nice, because now the aforementioned improvements to static inference on
builds(<target>, populate_full_signature=True)
can manifest viamake_custom_builds_fn
(otherwise, users would always have to writebuilds(<target>, populate_full_signature=True)
to get the nice static inference):This was accomplished by adding special overloads for
builds
with updated default values -- specifically forpopulate_full_signature=True
andzen_partial=True
, respectively -- which are stored inhydra_zen.typing._builds_overloads
. We then use some fanciness to conditionally dispatch the output ofmake_custom_builds_fn
using these specialized overloads.This adds some unfortunate boilerplate and redundancies, but I think it is ultimately well worth it. Otherwise the very common use case of
make_custom_builds_fn(populate_full_signature=True)
would not be as powerful as manually writingbuilds(<t>, populate_full_signature=True)
; furthermore, it would be highly non-trivial for end users to write their own aliases forbuilds(<t>, populate_full_signature=True)
such that they would have the desired type-inference behavior.Some Implementation Details
This signature reflection occurs only when:
populate_full_signature=True
is specifiedzen_partial=False
*args
/**kwargs**
are provided by the user to `buildsbuilds_bases
The reason for this is that when any of these conditions do not hold, then the user does not need to specify all of the parameters in the target's signature. E.g. consider:
because
x=1
is specified, the true signature ofConf
is(x: int = 1. y: str = "hello")
. Unfortunately, there is not way (that I know of) to tell the type-checkers about this modification to the signature. Thus if we enabled signature-reflection in this case, then you would get the false-negative:I do not want users to ever encounter false-positives due to our type annotations. Thus we disable signature reflection here:
It is also noteworthy that functions with
overloads
are not amenable to beingParamSpec
'd: