Skip to content

Commit db674c5

Browse files
committed
relaxes typed set to allow unused fspaths (will be dropped on copy if trimmed is provided)
1 parent 542c75c commit db674c5

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

fileformats/generic/set.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import typing as ty
22
import itertools
33
from pathlib import Path
4-
from fileformats.core import FileSet, validated_property
4+
from fileformats.core import FileSet
55
from fileformats.core.mixin import WithClassifiers
66
from fileformats.core.collection import TypedCollection
7-
from fileformats.core.exceptions import FormatMismatchError
87

98

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

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

4132

4233
class SetOf(WithClassifiers, TypedSet): # type: ignore[misc]

fileformats/generic/tests/test_directory.py renamed to fileformats/generic/tests/test_collection.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ def test_directory_optional_contents(tmp_path):
3434

3535
optional_dir = DirectoryOf[MyFormatGz, ty.Optional[YourFormat]](sample_dir)
3636
assert optional_dir.contents == [my_format]
37+
3738
your_format = YourFormat.sample(dest_dir=tmp_path)
3839
optional_dir = DirectoryOf[MyFormatGz, ty.Optional[YourFormat]](sample_dir)
3940
assert optional_dir.contents == [my_format, your_format]
41+
4042
required_dir = DirectoryOf[MyFormatGz, YourFormat](sample_dir)
4143
assert required_dir.contents == [my_format, your_format]
4244

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

4850
sample_set = SetOf[MyFormatGz, YourFormat](my_format, your_format)
4951
assert sample_set.contents == [my_format, your_format]
50-
with pytest.raises(
51-
FormatMismatchError, match="are not used by any of the contents of "
52-
):
53-
SetOf[MyFormatGz](my_format, your_format)
52+
assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath}
53+
54+
sample_set = SetOf[MyFormatGz](my_format, your_format)
55+
assert list(sample_set.required_paths()) == [my_format.fspath]
56+
5457
with pytest.raises(
5558
FormatMismatchError, match="Did not find the required content types"
5659
):
5760
SetOf[MyFormatGz, YourFormat](my_format)
61+
5862
sample_set = SetOf[MyFormatGz, ty.Optional[YourFormat]](my_format)
5963
assert sample_set.contents == [my_format]
64+
assert list(sample_set.required_paths()) == [my_format.fspath]
65+
6066
sample_set = SetOf[MyFormatGz, ty.Optional[YourFormat]](my_format, your_format)
6167
assert sample_set.contents == [my_format, your_format]
68+
assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath}
69+
70+
sample_set = SetOf[ty.Optional[MyFormatGz]](my_format)
71+
assert sample_set.contents == [my_format]
72+
assert list(sample_set.required_paths()) == [my_format.fspath]

0 commit comments

Comments
 (0)