Skip to content
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

Special handling of bytes_repr for mock filesets #743

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions pydra/utils/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from filelock import SoftFileLock
import attrs.exceptions
from fileformats.core import FileSet
from fileformats.core.fileset import FileSet, MockMixin
from . import user_cache_dir, add_exc_note

logger = logging.getLogger("pydra")
Expand Down Expand Up @@ -55,7 +55,7 @@
)

Hash = NewType("Hash", bytes)
CacheKey = NewType("CacheKey", ty.Tuple[ty.Hashable, ty.Hashable])
CacheKey = NewType("CacheKey", ty.Tuple[ty.Hashable, ...])


def location_converter(path: ty.Union[Path, str, None]) -> Path:
Expand Down Expand Up @@ -478,11 +478,29 @@ def bytes_repr_fileset(
fileset: FileSet, cache: Cache
) -> Iterator[ty.Union[CacheKey, bytes]]:
fspaths = sorted(fileset.fspaths)
# Yield the cache key for the fileset, which is a tuple of the file-system paths
# and their mtime. Is used to store persistent cache of the fileset hashes
# to avoid recomputation between calls
yield CacheKey(
tuple(repr(p) for p in fspaths) # type: ignore[arg-type]
+ tuple(p.lstat().st_mtime_ns for p in fspaths)
)
yield from fileset.__bytes_repr__(cache)
cls = type(fileset)
yield f"{cls.__module__}.{cls.__name__}:".encode()
for key, chunk_iter in fileset.byte_chunks():
yield (",'" + key + "'=").encode()
yield from chunk_iter


# Need to disable the mtime cache key for mocked filesets. Used in doctests
@register_serializer(MockMixin)
def bytes_repr_mock_fileset(
Comment on lines +495 to +497
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like putting test code in main modules. What about adding pydra.testing.fixtures and including:

@pytest.fixture(scope='session')
def register_mock_mixin_serializer():
    @register_serializer(MockMixin)
    ...

Then in conftest.py:

from pydra.testing.fixtures import register_mock_mixin_serializer

This would allow downstream tools that need this feature to also put this import in their conftest.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the sentiment in general, but in this case I reckon that might be creating a gotcha that would take a bit of tracking down how to solve for little practical benefit, i.e if someone creates a new task package and wants to write a doctest using NiftiGz.mock(), then they would need to know that they also have to add this fixture

mock_fileset: MockMixin, cache: Cache
) -> Iterator[ty.Union[CacheKey, bytes]]:
cls = type(mock_fileset)
yield f"{cls.__module__}.{cls.__name__}:".encode()
for key, _ in mock_fileset.byte_chunks():
yield (",'" + key + "'").encode()


@register_serializer(list)
Expand Down
Loading