-
-
Notifications
You must be signed in to change notification settings - Fork 636
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
Allow HydratedSourcesRequest
to indicate which Sources types are expected
#9641
Changes from 2 commits
5340f74
ff97ac4
9fe244a
d76cbfc
59743cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -472,7 +472,11 @@ async def get_sources( | |
) -> SetupPySources: | ||
targets = request.targets | ||
stripped_srcs_list = await MultiGet( | ||
Get[SourceRootStrippedSources](StripSourcesFieldRequest(target.get(Sources))) | ||
Get[SourceRootStrippedSources]( | ||
StripSourcesFieldRequest( | ||
target.get(Sources), valid_sources_types=(PythonSources, ResourcesSources) | ||
) | ||
) | ||
for target in targets | ||
) | ||
|
||
|
@@ -518,7 +522,9 @@ async def get_ancestor_init_py( | |
""" | ||
source_roots = source_root_config.get_source_roots() | ||
sources = await Get[SourceFiles]( | ||
AllSourceFilesRequest(tgt[PythonSources] for tgt in targets if tgt.has_field(PythonSources)) | ||
AllSourceFilesRequest( | ||
(tgt.get(Sources) for tgt in targets), valid_sources_types=(PythonSources,) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change will be necessary once we have codegen. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we add codegen, with this PR: AllSourceFilesRequest(
(tgt.get(Sources) for tgt in targets),
valid_sources_types=(PythonSources,),
codegen_enabled=True
) If we go with the approach in #9634: AllSourceFilesRequest(
(
tgt[Sources]
for tgt in targets
if tgt.has_field(PythonSources) or tgt.has_field(CodegenSources)
),
codegen_language=PythonSources,
) |
||
) | ||
# Find the ancestors of all dirs containing .py files, including those dirs themselves. | ||
source_dir_ancestors: Set[Tuple[str, str]] = set() # Items are (src_root, path incl. src_root). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1343,15 +1343,39 @@ def filespec(self) -> Filespec: | |
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
@frozen_after_init | ||
@dataclass(unsafe_hash=True) | ||
class HydrateSourcesRequest: | ||
field: Sources | ||
valid_sources_types: Tuple[Type[Sources], ...] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming wise, the word "valid" would seem to imply that the request will fail if the sources can't be converted to one of these types. "desired" would be a bit verbose. Maybe just |
||
|
||
def __init__( | ||
self, field: Sources, *, valid_sources_types: Iterable[Type[Sources]] = (Sources,) | ||
) -> None: | ||
"""Convert raw sources globs into an instance of HydratedSources. | ||
|
||
If you only want to convert certain Sources fields, such as only PythonSources, set | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/convert/handle/ You could probably also the fact that you might get subclasses of your requested types, but that the output type will always be an exact match. |
||
`valid_sources_types`. Any invalid sources will return an empty `HydratedSources` instance, | ||
indicated by the attribute `output_type = None`. | ||
""" | ||
self.field = field | ||
self.valid_sources_types = tuple(valid_sources_types) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class HydratedSources: | ||
"""The result of hydrating a SourcesField. | ||
|
||
The `output_type` will indicate which of the `HydrateSourcesRequest.valid_sources_type` the | ||
result corresponds to, e.g. if the result comes from `FilesSources` vs. `PythonSources`. If this | ||
value is None, then the input `Sources` field was not one of the expected types. This property | ||
allows for switching on the result, e.g. handling hydrated files() sources differently than | ||
hydrated Python sources. | ||
""" | ||
|
||
snapshot: Snapshot | ||
filespec: Filespec | ||
output_type: Optional[Type[Sources]] | ||
|
||
def eager_fileset_with_spec(self, *, address: Address) -> EagerFilesetWithSpec: | ||
return EagerFilesetWithSpec(address.spec_path, self.filespec, self.snapshot) | ||
|
@@ -1362,10 +1386,21 @@ async def hydrate_sources( | |
request: HydrateSourcesRequest, glob_match_error_behavior: GlobMatchErrorBehavior | ||
) -> HydratedSources: | ||
sources_field = request.field | ||
globs = sources_field.sanitized_raw_value | ||
|
||
output_type = next( | ||
( | ||
valid_type | ||
for valid_type in request.valid_sources_types | ||
if isinstance(sources_field, valid_type) | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means that we will upcast subclasses like |
||
None, | ||
) | ||
if output_type is None: | ||
return HydratedSources(EMPTY_SNAPSHOT, sources_field.filespec, output_type=None) | ||
|
||
globs = sources_field.sanitized_raw_value | ||
if globs is None: | ||
return HydratedSources(EMPTY_SNAPSHOT, sources_field.filespec) | ||
return HydratedSources(EMPTY_SNAPSHOT, sources_field.filespec, output_type=output_type) | ||
|
||
conjunction = ( | ||
GlobExpansionConjunction.all_match | ||
|
@@ -1387,7 +1422,7 @@ async def hydrate_sources( | |
) | ||
) | ||
sources_field.validate_snapshot(snapshot) | ||
return HydratedSources(snapshot, sources_field.filespec) | ||
return HydratedSources(snapshot, sources_field.filespec, output_type=output_type) | ||
|
||
|
||
# TODO: figure out what support looks like for this with the Target API. The expected value is an | ||
|
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.
Once we add codegen, with this PR:
With the approach in #9634: