From c1c15bc28ef631b5093682a6624cb26e4c160cc5 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Tue, 17 Sep 2024 18:29:12 +1000 Subject: [PATCH 1/6] truncate set repr after 3 paths --- fileformats/generic/set.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fileformats/generic/set.py b/fileformats/generic/set.py index 7484086..c6c3194 100644 --- a/fileformats/generic/set.py +++ b/fileformats/generic/set.py @@ -13,6 +13,18 @@ class TypedSet(FileSet): content_types: ty.Tuple[ty.Type[FileSet], ...] = () + MAX_REPR_PATHS = 3 + + def __repr__(self) -> str: + paths_repr = ( + "'" + + "', '".join(str(p) for p in sorted(self.fspaths)[: self.MAX_REPR_PATHS]) + + "'" + ) + if len(self.fspaths) > self.MAX_REPR_PATHS: + paths_repr += ", ..." + return f"{self.type_name}({paths_repr})" + @cached_property def contents(self) -> ty.List[FileSet]: contnts = [] From 1b0ffcff93b14aa1ed68e6f13204ca10c5a7fedc Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 20 Sep 2024 12:20:44 +1000 Subject: [PATCH 2/6] added test for set repr and fixed up repr for mocked classified classes --- fileformats/core/fileset.py | 9 ++++++--- fileformats/core/tests/test_identification.py | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/fileformats/core/fileset.py b/fileformats/core/fileset.py index b18fe02..db8b78e 100644 --- a/fileformats/core/fileset.py +++ b/fileformats/core/fileset.py @@ -1737,13 +1737,16 @@ def __init__( @classproperty def type_name(cls) -> str: - class_name: str = cls.__name__ # type: ignore - assert class_name.endswith("Mock") - return class_name[: -len("Mock")] + return cls.mocked.type_name def __bytes_repr__(self, cache: ty.Dict[str, ty.Any]) -> ty.Iterable[bytes]: yield from (str(fspath).encode() for fspath in self.fspaths) + @classproperty + def mocked(cls) -> FileSet: + """The "true" class that the mocked class is based on""" + return next(c for c in cls.__mro__ if not issubclass(c, MockMixin)) # type: ignore[no-any-return, attr-defined] + @classproperty def namespace(cls) -> str: """The "namespace" the format belongs to under the "fileformats" umbrella diff --git a/fileformats/core/tests/test_identification.py b/fileformats/core/tests/test_identification.py index dda8467..0aad313 100644 --- a/fileformats/core/tests/test_identification.py +++ b/fileformats/core/tests/test_identification.py @@ -9,6 +9,7 @@ from_paths, FileSet, ) +from fileformats.generic import File, SetOf from fileformats.core.exceptions import FormatRecognitionError from fileformats.testing import Foo, Bar from fileformats.application import Json, Yaml, Zip @@ -64,6 +65,13 @@ def test_repr(): assert repr(frmt.mock("/a/path")).startswith(f"{frmt.__name__}(") +def test_set_repr_trunc(): + assert ( + repr(SetOf[File].mock("/a/path", "/b/path", "/c/path", "/d/path")) + == "SetOf[File]('/a/path', '/b/path', '/c/path', ...)" + ) + + def test_from_paths(tmp_path): filesets = [] filesets.append(Json.sample(tmp_path, seed=1)) From d817705e70094879330ff7cbb0b009db4ce3af21 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 20 Sep 2024 13:14:23 +1000 Subject: [PATCH 3/6] fixed up set_repr test on windows --- fileformats/core/tests/test_identification.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fileformats/core/tests/test_identification.py b/fileformats/core/tests/test_identification.py index 0aad313..cd21c47 100644 --- a/fileformats/core/tests/test_identification.py +++ b/fileformats/core/tests/test_identification.py @@ -1,6 +1,7 @@ from collections import Counter import itertools import typing as ty +from pathlib import Path import pytest from fileformats.core import ( find_matching, @@ -66,9 +67,12 @@ def test_repr(): def test_set_repr_trunc(): + a = Path("/a/path") + b = Path("/b/path") + c = Path("/c/path") + d = Path("/d/path") assert ( - repr(SetOf[File].mock("/a/path", "/b/path", "/c/path", "/d/path")) - == "SetOf[File]('/a/path', '/b/path', '/c/path', ...)" + repr(SetOf[File].mock(a, b, c, d)) == f"SetOf[File]('{a}', '{b}', '{c}', ...)" ) From 6a91d594023a1de8a1550df9c215d1795d5012ad Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 20 Sep 2024 13:19:00 +1000 Subject: [PATCH 4/6] disabled unneeded xfails --- .../tests/test_application_serialization.py | 20 +++++++------- .../image/tests/test_image_converters.py | 26 +++++++++---------- fileformats/generic/tests/test_file.py | 4 +-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/extras/fileformats/extras/application/tests/test_application_serialization.py b/extras/fileformats/extras/application/tests/test_application_serialization.py index 1a2cda4..1ac09a0 100644 --- a/extras/fileformats/extras/application/tests/test_application_serialization.py +++ b/extras/fileformats/extras/application/tests/test_application_serialization.py @@ -1,5 +1,5 @@ -import sys -import pytest +# import sys +# import pytest from fileformats.application import Json, Yaml @@ -28,10 +28,10 @@ """ -@pytest.mark.xfail( - sys.version_info.minor <= 9, - reason="upstream Pydra issue with type-checking 'type' objects", -) +# @pytest.mark.xfail( +# sys.version_info.minor <= 9, +# reason="upstream Pydra issue with type-checking 'type' objects", +# ) def test_json_to_yaml(work_dir): in_file = work_dir / "test.json" with open(in_file, "w") as f: @@ -41,10 +41,10 @@ def test_json_to_yaml(work_dir): assert yml.contents == SAMPLE_YAML -@pytest.mark.xfail( - sys.version_info.minor <= 9, - reason="upstream Pydra issue with type-checking 'type' objects", -) +# @pytest.mark.xfail( +# sys.version_info.minor <= 9, +# reason="upstream Pydra issue with type-checking 'type' objects", +# ) def test_yaml_to_json(work_dir): in_file = work_dir / "test.yaml" with open(in_file, "w") as f: diff --git a/extras/fileformats/extras/image/tests/test_image_converters.py b/extras/fileformats/extras/image/tests/test_image_converters.py index c6be18e..2e85e34 100644 --- a/extras/fileformats/extras/image/tests/test_image_converters.py +++ b/extras/fileformats/extras/image/tests/test_image_converters.py @@ -1,4 +1,4 @@ -import sys +# import sys import pytest from imageio.core.fetching import get_remote_file from fileformats.image import Bitmap, Gif, Jpeg, Png, Tiff @@ -15,25 +15,25 @@ def png() -> Png: return Png(get_remote_file("images/chelsea.png")) -@pytest.mark.xfail( - sys.version_info.minor <= 9, - reason="upstream Pydra issue with type-checking 'type' objects", -) +# @pytest.mark.xfail( +# sys.version_info.minor <= 9, +# reason="upstream Pydra issue with type-checking 'type' objects", +# ) def test_jpg_to_gif(jpg): Gif.convert(jpg) -@pytest.mark.xfail( - sys.version_info.minor <= 9, - reason="upstream Pydra issue with type-checking 'type' objects", -) +# @pytest.mark.xfail( +# sys.version_info.minor <= 9, +# reason="upstream Pydra issue with type-checking 'type' objects", +# ) def test_png_to_tiff(png): Tiff.convert(png) -@pytest.mark.xfail( - sys.version_info.minor <= 9, - reason="upstream Pydra issue with type-checking 'type' objects", -) +# @pytest.mark.xfail( +# sys.version_info.minor <= 9, +# reason="upstream Pydra issue with type-checking 'type' objects", +# ) def test_png_to_bitmap(png): Bitmap.convert(png) diff --git a/fileformats/generic/tests/test_file.py b/fileformats/generic/tests/test_file.py index da9ed8e..73a182a 100644 --- a/fileformats/generic/tests/test_file.py +++ b/fileformats/generic/tests/test_file.py @@ -1,6 +1,6 @@ import pytest from fileformats.generic import FsObject, File -from fileformats.testing import Magic, Foo, MyFormatX +from fileformats.testing import Magic, MagicVersion, Foo, MyFormatX def test_sample_fsobject(): @@ -27,4 +27,4 @@ def test_sample_magic(): reason="generate_sample_data for WithMagicVersion file types is not implemented yet" ) def test_sample_magic_version(): - assert isinstance(Magic.sample(), Magic) + assert isinstance(MagicVersion.sample(), MagicVersion) From 404705aceef1408a9868b5ae552db2207375e1e9 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 20 Sep 2024 13:26:53 +1000 Subject: [PATCH 5/6] disabled warn unused ignores --- .github/workflows/ci-cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 7088088..b59ea63 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -47,7 +47,7 @@ jobs: - name: Install Extras Package run: python3 -m pip install -e ./extras[test] - name: MyPy - run: mypy --install-types --non-interactive . + run: mypy --install-types --non-interactive --no-warn-unused-ignores . - name: Pytest run: pytest -vvs --cov fileformats --cov-config .coveragerc --cov-report xml . - name: Upload coverage to Codecov From 9466babfd36c2397298e2ccd02aba03c51e2900b Mon Sep 17 00:00:00 2001 From: Tom Close Date: Fri, 20 Sep 2024 13:29:11 +1000 Subject: [PATCH 6/6] fixing up set_repr test on windows --- fileformats/core/tests/test_identification.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fileformats/core/tests/test_identification.py b/fileformats/core/tests/test_identification.py index cd21c47..56b4495 100644 --- a/fileformats/core/tests/test_identification.py +++ b/fileformats/core/tests/test_identification.py @@ -67,10 +67,10 @@ def test_repr(): def test_set_repr_trunc(): - a = Path("/a/path") - b = Path("/b/path") - c = Path("/c/path") - d = Path("/d/path") + a = Path("/a/path").absolute() + b = Path("/b/path").absolute() + c = Path("/c/path").absolute() + d = Path("/d/path").absolute() assert ( repr(SetOf[File].mock(a, b, c, d)) == f"SetOf[File]('{a}', '{b}', '{c}', ...)" )