Skip to content

Commit

Permalink
Merge pull request #83 from ArcanaFramework/set-repr
Browse files Browse the repository at this point in the history
Truncates TypedSet and SetOf repr to only show 3 paths followed by ellipsis
  • Loading branch information
tclose authored Sep 20, 2024
2 parents c040b82 + 9466bab commit 3abc44d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import pytest
# import sys
# import pytest
from fileformats.application import Json, Yaml


Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
26 changes: 13 additions & 13 deletions extras/fileformats/extras/image/tests/test_image_converters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
9 changes: 6 additions & 3 deletions fileformats/core/fileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions fileformats/core/tests/test_identification.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,6 +10,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
Expand Down Expand Up @@ -64,6 +66,16 @@ def test_repr():
assert repr(frmt.mock("/a/path")).startswith(f"{frmt.__name__}(")


def test_set_repr_trunc():
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}', ...)"
)


def test_from_paths(tmp_path):
filesets = []
filesets.append(Json.sample(tmp_path, seed=1))
Expand Down
12 changes: 12 additions & 0 deletions fileformats/generic/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions fileformats/generic/tests/test_file.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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)

0 comments on commit 3abc44d

Please sign in to comment.