-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pythonic resources] Improve nested resource evaluation logic (#14807)
## Summary This PR attempts to address a couple shortcomings with the Pythonic resource system which are exposed in #14751. First, the logic to determine whether a field on a resource represents a nested resource or a config field is not a good heuristic currently. Right now, the check only assesses whether the value is a resource type - but this fails in the case that a user wants to provide a raw value (ie an instance of `IOManager`). This PR updates this check to inspect the annotation on the field, and treat it as a resource only if the annotation indicates as much. Second, the initialization logic for nested resources (`coerce_to_resource`) doesn't properly handle raw values such as a plain `IOManager`. This PR updates the logic in `coerce_to_resource` to call out to `wrap_resources_for_execution`, which can successfully deal with these cases. ## Example The following resource previously would not accept a raw `IOManager` as input, only a resource which produces one: ```python class MyResourceNeedsIOManager(ConfigurableIOManager): base_io_manager: ResourceDependency[IOManager] @io_manager def my_io_manager(context) -> IOManager: pass # previously OK, since my_io_manager is a resource MyResourceNeedsIOManager(base_io_manager=my_io_manager) class MyIOManager(IOManager): pass # previously errors, since MyIOManager is not a resource MyResourceNeedsIOManager(base_io_manager=MyIOManager()) ``` The new code will coerce `MyIOManager` to a resource by wrapping it, since it is annotated as a `ResourceDependency`. This gives the user more flexibility when specifying resource-resource dependencies to input raw values or mocks. ## Test Plan Existing unit tests succeed. New unit tests of nesting behavior with raw values (such as an IOManager).
- Loading branch information
Showing
6 changed files
with
692 additions
and
539 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.