-
Notifications
You must be signed in to change notification settings - Fork 16
Reimplement expand using SnakemakeFormatter instead of Snakemake's expand
#526
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -15,11 +15,11 @@ | |
| from pvandyken.deprecated import deprecated | ||
| from typing_extensions import Self, TypedDict | ||
|
|
||
| from snakebids.core.expanding import expand as _expand | ||
| from snakebids.core.filtering import filter_list | ||
| from snakebids.exceptions import DuplicateComponentError | ||
| from snakebids.io.console import get_console_size | ||
| from snakebids.io.printing import format_zip_lists, quote_wrap | ||
| from snakebids.snakemake_compat import expand as sn_expand | ||
| from snakebids.types import ZipList | ||
| from snakebids.utils.containers import ImmutableList, MultiSelectDict | ||
| from snakebids.utils.snakemake_templates import MissingEntityError, SnakemakeFormatter | ||
|
|
@@ -122,16 +122,11 @@ def expand( | |
| Keywords not found in the path will be ignored. Keywords take values or | ||
| lists of values to be expanded over the provided paths. | ||
| """ | ||
| return sn_expand( | ||
| return _expand( | ||
| list(itx.always_iterable(paths)), | ||
| allow_missing=allow_missing | ||
| if isinstance(allow_missing, bool) | ||
| else list(itx.always_iterable(allow_missing)), | ||
| **{self.entity: list(dict.fromkeys(self._data))}, | ||
| **{ | ||
| wildcard: list(itx.always_iterable(v)) | ||
| for wildcard, v in wildcards.items() | ||
| }, | ||
| zip_lists={self.entity: list(dict.fromkeys(self._data))}, | ||
| allow_missing=bool(allow_missing), | ||
| **wildcards, | ||
| ) | ||
|
|
||
| def filter( | ||
|
|
@@ -349,7 +344,7 @@ def expand( | |
| paths: Iterable[Path | str] | Path | str, | ||
| /, | ||
| allow_missing: bool | str | Iterable[str] = False, | ||
| **wildcards: str | Iterable[str], | ||
| **wildcards: Iterable[str | None] | str | None, | ||
| ) -> list[str]: | ||
| """Safely expand over given paths with component wildcards. | ||
|
||
|
|
||
|
|
@@ -376,39 +371,18 @@ def expand( | |
| Keywords not found in the path will be ignored. Keywords take values or | ||
| lists of values to be expanded over the provided paths. | ||
| """ | ||
|
|
||
| def sequencify(item: bool | str | Iterable[str]) -> bool | list[str]: | ||
| if isinstance(item, bool): | ||
| return item | ||
| return list(itx.always_iterable(item)) | ||
|
|
||
| allow_missing_seq = sequencify(allow_missing) | ||
| if self.zip_lists: | ||
| inner_expand = list( | ||
| # order preserving deduplication | ||
| dict.fromkeys( | ||
| sn_expand( | ||
| list(itx.always_iterable(paths)), | ||
| zip, | ||
| allow_missing=True if wildcards else allow_missing_seq, | ||
| **self.zip_lists, | ||
| ) | ||
| ) | ||
| ) | ||
| else: | ||
| inner_expand = list(itx.always_iterable(paths)) | ||
| if not wildcards: | ||
| return inner_expand | ||
|
|
||
| return sn_expand( | ||
| inner_expand, | ||
| allow_missing=allow_missing_seq, | ||
| # Turn all the wildcard items into lists because Snakemake doesn't handle | ||
| # iterables very well | ||
| **{ | ||
| wildcard: list(itx.always_iterable(v)) | ||
| for wildcard, v in wildcards.items() | ||
| }, | ||
| path_list = list(itx.always_iterable(paths)) | ||
|
|
||
| # When zip_lists is empty and no extra wildcards, return paths as-is | ||
| # (no formatting: avoids errors on arbitrary path text with {wildcards}) | ||
| if allow_missing and not self.zip_lists and not wildcards: | ||
| return path_list | ||
|
|
||
| return _expand( | ||
| path_list, | ||
| zip_lists=self.zip_lists, | ||
| allow_missing=bool(allow_missing), | ||
| **wildcards, | ||
| ) | ||
|
|
||
| def filter( | ||
|
|
@@ -615,7 +589,7 @@ def expand( | |
| paths: Iterable[Path | str] | Path | str | None = None, | ||
| /, | ||
| allow_missing: bool | str | Iterable[str] = False, | ||
| **wildcards: str | Iterable[str], | ||
| **wildcards: Iterable[str | None] | str | None, | ||
| ) -> list[str]: | ||
| """Safely expand over given paths with component wildcards. | ||
|
|
||
|
|
@@ -645,7 +619,7 @@ def expand( | |
| Keywords not found in the path will be ignored. Keywords take values or | ||
| lists of values to be expanded over the provided paths. | ||
| """ | ||
| paths = paths or self.path | ||
| paths = self.path if paths is None else paths | ||
| return super().expand(paths, allow_missing, **wildcards) | ||
|
|
||
| @property | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| import itertools as it | ||||||
| from collections.abc import Iterable | ||||||
| from typing import Any | ||||||
|
|
||||||
| import more_itertools as itx | ||||||
|
|
||||||
| from snakebids.types import ZipListLike | ||||||
| from snakebids.utils.snakemake_templates import MissingEntityError, SnakemakeFormatter | ||||||
|
|
||||||
| try: | ||||||
| from snakemake.io import AnnotatedString as _AnnotatedString # type: ignore | ||||||
|
|
||||||
| except ImportError: | ||||||
|
|
||||||
| def _get_flags(path: Any) -> dict[str, Any] | None: | ||||||
| """Return None when snakemake is not installed (no AnnotatedString support).""" | ||||||
| return None | ||||||
|
|
||||||
| def _make_annotated(s: str, flags: dict[str, Any]) -> str: | ||||||
| """Return the string unchanged when snakemake is not installed.""" | ||||||
| return s | ||||||
| else: | ||||||
|
|
||||||
| def _get_flags(path: Any) -> dict[str, Any] | None: | ||||||
| """Extract flags from an AnnotatedString path.""" | ||||||
| if isinstance(path, _AnnotatedString) and path.flags: # type: ignore | ||||||
|
||||||
| if isinstance(path, _AnnotatedString) and path.flags: # type: ignore | |
| if isinstance(path, _AnnotatedString): # type: ignore |
Copilot
AI
Feb 28, 2026
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.
expand() produces no results when zip_lists is empty because rows = list(zip(*zip_lists.values(), strict=True)) becomes [] and the subsequent it.product(rows, ...) loop never runs. This breaks expansion for components/templates with no zip-list wildcards (should still format once per template, and still allow expansion over extra wildcards only). Consider treating an empty zip_lists as a single empty row (e.g., rows = [()]) so the product loop executes and missing-wildcard behavior is handled by the formatter.
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.
This call path now supports normalizing
Nonewildcard values (via the new formatter-based expander), butBidsComponentRow.expand()'s type signature still restricts**wildcardstostr | Iterable[str]. Consider widening the public type annotation forBidsComponentRow.expand()to includeNone(scalar or within iterables) to match actual supported behavior andBidsComponent.expand().