Skip to content

Commit 3abc44d

Browse files
authored
Merge pull request #83 from ArcanaFramework/set-repr
Truncates TypedSet and SetOf repr to only show 3 paths followed by ellipsis
2 parents c040b82 + 9466bab commit 3abc44d

File tree

7 files changed

+56
-29
lines changed

7 files changed

+56
-29
lines changed

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- name: Install Extras Package
4848
run: python3 -m pip install -e ./extras[test]
4949
- name: MyPy
50-
run: mypy --install-types --non-interactive .
50+
run: mypy --install-types --non-interactive --no-warn-unused-ignores .
5151
- name: Pytest
5252
run: pytest -vvs --cov fileformats --cov-config .coveragerc --cov-report xml .
5353
- name: Upload coverage to Codecov

extras/fileformats/extras/application/tests/test_application_serialization.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import sys
2-
import pytest
1+
# import sys
2+
# import pytest
33
from fileformats.application import Json, Yaml
44

55

@@ -28,10 +28,10 @@
2828
"""
2929

3030

31-
@pytest.mark.xfail(
32-
sys.version_info.minor <= 9,
33-
reason="upstream Pydra issue with type-checking 'type' objects",
34-
)
31+
# @pytest.mark.xfail(
32+
# sys.version_info.minor <= 9,
33+
# reason="upstream Pydra issue with type-checking 'type' objects",
34+
# )
3535
def test_json_to_yaml(work_dir):
3636
in_file = work_dir / "test.json"
3737
with open(in_file, "w") as f:
@@ -41,10 +41,10 @@ def test_json_to_yaml(work_dir):
4141
assert yml.contents == SAMPLE_YAML
4242

4343

44-
@pytest.mark.xfail(
45-
sys.version_info.minor <= 9,
46-
reason="upstream Pydra issue with type-checking 'type' objects",
47-
)
44+
# @pytest.mark.xfail(
45+
# sys.version_info.minor <= 9,
46+
# reason="upstream Pydra issue with type-checking 'type' objects",
47+
# )
4848
def test_yaml_to_json(work_dir):
4949
in_file = work_dir / "test.yaml"
5050
with open(in_file, "w") as f:
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sys
1+
# import sys
22
import pytest
33
from imageio.core.fetching import get_remote_file
44
from fileformats.image import Bitmap, Gif, Jpeg, Png, Tiff
@@ -15,25 +15,25 @@ def png() -> Png:
1515
return Png(get_remote_file("images/chelsea.png"))
1616

1717

18-
@pytest.mark.xfail(
19-
sys.version_info.minor <= 9,
20-
reason="upstream Pydra issue with type-checking 'type' objects",
21-
)
18+
# @pytest.mark.xfail(
19+
# sys.version_info.minor <= 9,
20+
# reason="upstream Pydra issue with type-checking 'type' objects",
21+
# )
2222
def test_jpg_to_gif(jpg):
2323
Gif.convert(jpg)
2424

2525

26-
@pytest.mark.xfail(
27-
sys.version_info.minor <= 9,
28-
reason="upstream Pydra issue with type-checking 'type' objects",
29-
)
26+
# @pytest.mark.xfail(
27+
# sys.version_info.minor <= 9,
28+
# reason="upstream Pydra issue with type-checking 'type' objects",
29+
# )
3030
def test_png_to_tiff(png):
3131
Tiff.convert(png)
3232

3333

34-
@pytest.mark.xfail(
35-
sys.version_info.minor <= 9,
36-
reason="upstream Pydra issue with type-checking 'type' objects",
37-
)
34+
# @pytest.mark.xfail(
35+
# sys.version_info.minor <= 9,
36+
# reason="upstream Pydra issue with type-checking 'type' objects",
37+
# )
3838
def test_png_to_bitmap(png):
3939
Bitmap.convert(png)

fileformats/core/fileset.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,13 +1737,16 @@ def __init__(
17371737

17381738
@classproperty
17391739
def type_name(cls) -> str:
1740-
class_name: str = cls.__name__ # type: ignore
1741-
assert class_name.endswith("Mock")
1742-
return class_name[: -len("Mock")]
1740+
return cls.mocked.type_name
17431741

17441742
def __bytes_repr__(self, cache: ty.Dict[str, ty.Any]) -> ty.Iterable[bytes]:
17451743
yield from (str(fspath).encode() for fspath in self.fspaths)
17461744

1745+
@classproperty
1746+
def mocked(cls) -> FileSet:
1747+
"""The "true" class that the mocked class is based on"""
1748+
return next(c for c in cls.__mro__ if not issubclass(c, MockMixin)) # type: ignore[no-any-return, attr-defined]
1749+
17471750
@classproperty
17481751
def namespace(cls) -> str:
17491752
"""The "namespace" the format belongs to under the "fileformats" umbrella

fileformats/core/tests/test_identification.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import Counter
22
import itertools
33
import typing as ty
4+
from pathlib import Path
45
import pytest
56
from fileformats.core import (
67
find_matching,
@@ -9,6 +10,7 @@
910
from_paths,
1011
FileSet,
1112
)
13+
from fileformats.generic import File, SetOf
1214
from fileformats.core.exceptions import FormatRecognitionError
1315
from fileformats.testing import Foo, Bar
1416
from fileformats.application import Json, Yaml, Zip
@@ -64,6 +66,16 @@ def test_repr():
6466
assert repr(frmt.mock("/a/path")).startswith(f"{frmt.__name__}(")
6567

6668

69+
def test_set_repr_trunc():
70+
a = Path("/a/path").absolute()
71+
b = Path("/b/path").absolute()
72+
c = Path("/c/path").absolute()
73+
d = Path("/d/path").absolute()
74+
assert (
75+
repr(SetOf[File].mock(a, b, c, d)) == f"SetOf[File]('{a}', '{b}', '{c}', ...)"
76+
)
77+
78+
6779
def test_from_paths(tmp_path):
6880
filesets = []
6981
filesets.append(Json.sample(tmp_path, seed=1))

fileformats/generic/set.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ class TypedSet(FileSet):
1313

1414
content_types: ty.Tuple[ty.Type[FileSet], ...] = ()
1515

16+
MAX_REPR_PATHS = 3
17+
18+
def __repr__(self) -> str:
19+
paths_repr = (
20+
"'"
21+
+ "', '".join(str(p) for p in sorted(self.fspaths)[: self.MAX_REPR_PATHS])
22+
+ "'"
23+
)
24+
if len(self.fspaths) > self.MAX_REPR_PATHS:
25+
paths_repr += ", ..."
26+
return f"{self.type_name}({paths_repr})"
27+
1628
@cached_property
1729
def contents(self) -> ty.List[FileSet]:
1830
contnts = []

fileformats/generic/tests/test_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
from fileformats.generic import FsObject, File
3-
from fileformats.testing import Magic, Foo, MyFormatX
3+
from fileformats.testing import Magic, MagicVersion, Foo, MyFormatX
44

55

66
def test_sample_fsobject():
@@ -27,4 +27,4 @@ def test_sample_magic():
2727
reason="generate_sample_data for WithMagicVersion file types is not implemented yet"
2828
)
2929
def test_sample_magic_version():
30-
assert isinstance(Magic.sample(), Magic)
30+
assert isinstance(MagicVersion.sample(), MagicVersion)

0 commit comments

Comments
 (0)