Skip to content

Commit

Permalink
Merge pull request #86 from ArcanaFramework/relaxed-typed-set
Browse files Browse the repository at this point in the history
relaxes typed set to allow unused fspaths
  • Loading branch information
tclose authored Sep 23, 2024
2 parents 542c75c + db674c5 commit 85fc309
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
15 changes: 3 additions & 12 deletions fileformats/generic/set.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import typing as ty
import itertools
from pathlib import Path
from fileformats.core import FileSet, validated_property
from fileformats.core import FileSet
from fileformats.core.mixin import WithClassifiers
from fileformats.core.collection import TypedCollection
from fileformats.core.exceptions import FormatMismatchError


class TypedSet(TypedCollection):
Expand All @@ -27,16 +26,8 @@ def __repr__(self) -> str:
paths_repr += ", ..."
return f"{self.type_name}({paths_repr})"

@validated_property
def _all_paths_used(self) -> None:
all_contents_paths = set(itertools.chain(*(c.fspaths for c in self.contents)))
missing = self.fspaths - all_contents_paths
if missing:
contents_str = "\n".join(repr(c) for c in self.contents)
raise FormatMismatchError(
f"Paths {[str(p) for p in missing]} are not used by any of the "
f"contents of {self.type_name}:\n{contents_str}"
)
def required_paths(self) -> ty.FrozenSet[Path]:
return frozenset(itertools.chain(*(c.required_paths() for c in self.contents)))


class SetOf(WithClassifiers, TypedSet): # type: ignore[misc]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ def test_directory_optional_contents(tmp_path):

optional_dir = DirectoryOf[MyFormatGz, ty.Optional[YourFormat]](sample_dir)
assert optional_dir.contents == [my_format]

your_format = YourFormat.sample(dest_dir=tmp_path)
optional_dir = DirectoryOf[MyFormatGz, ty.Optional[YourFormat]](sample_dir)
assert optional_dir.contents == [my_format, your_format]

required_dir = DirectoryOf[MyFormatGz, YourFormat](sample_dir)
assert required_dir.contents == [my_format, your_format]

Expand All @@ -47,15 +49,24 @@ def test_set_optional_contents():

sample_set = SetOf[MyFormatGz, YourFormat](my_format, your_format)
assert sample_set.contents == [my_format, your_format]
with pytest.raises(
FormatMismatchError, match="are not used by any of the contents of "
):
SetOf[MyFormatGz](my_format, your_format)
assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath}

sample_set = SetOf[MyFormatGz](my_format, your_format)
assert list(sample_set.required_paths()) == [my_format.fspath]

with pytest.raises(
FormatMismatchError, match="Did not find the required content types"
):
SetOf[MyFormatGz, YourFormat](my_format)

sample_set = SetOf[MyFormatGz, ty.Optional[YourFormat]](my_format)
assert sample_set.contents == [my_format]
assert list(sample_set.required_paths()) == [my_format.fspath]

sample_set = SetOf[MyFormatGz, ty.Optional[YourFormat]](my_format, your_format)
assert sample_set.contents == [my_format, your_format]
assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath}

sample_set = SetOf[ty.Optional[MyFormatGz]](my_format)
assert sample_set.contents == [my_format]
assert list(sample_set.required_paths()) == [my_format.fspath]

0 comments on commit 85fc309

Please sign in to comment.