Skip to content

Commit 6a5d6bb

Browse files
committed
implemented convertible_from method in FileSet
1 parent ef35591 commit 6a5d6bb

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed
Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tempfile
1+
import typing as ty
22
import attrs
33
from pathlib import Path
44
import pytest
@@ -10,59 +10,43 @@
1010
from conftest import write_test_file
1111

1212

13-
@pytest.fixture
14-
def FooBarConverter():
15-
work_dir = Path(tempfile.mkdtemp())
13+
@converter
14+
@python.define(outputs={"out_file": Bar}) # type: ignore[misc]
15+
def FooBarConverter(in_file: Foo):
16+
return Bar(write_test_file(Path.cwd() / "bar.bar", in_file.raw_contents))
1617

17-
@converter
18-
@python.define(outputs={"out_file": Bar}) # type: ignore[misc]
19-
def FooBarConverter_(in_file: Foo):
20-
return Bar(write_test_file(work_dir / "bar.bar", in_file.raw_contents))
2118

22-
return FooBarConverter_
19+
@converter(out_file="out")
20+
@python.define(outputs={"out": Bar}) # type: ignore[misc]
21+
def BazBarConverter(in_file: Baz):
22+
assert in_file
23+
return Bar(write_test_file(Path.cwd() / "bar.bar", in_file.raw_contents))
2324

2425

25-
@pytest.fixture
26-
def BazBarConverter():
27-
work_dir = Path(tempfile.mkdtemp())
26+
@converter(source_format=Foo, target_format=Qux)
27+
@shell.define
28+
class FooQuxConverter(shell.Task["FooQuxConverter.Outputs"]):
2829

29-
@converter(out_file="out")
30-
@python.define(outputs={"out": Bar}) # type: ignore[misc]
31-
def BazBarConverter_(in_file: Baz):
32-
assert in_file
33-
return Bar(write_test_file(work_dir / "bar.bar", in_file.raw_contents))
30+
in_file: File = shell.arg(help="the input file", argstr="")
31+
executable = "cp"
3432

35-
return BazBarConverter_
33+
class Outputs(shell.Outputs):
34+
out_file: File = shell.outarg(
35+
help="output file",
36+
argstr="",
37+
position=-1,
38+
path_template="out.qux",
39+
)
3640

3741

38-
@pytest.fixture
39-
def FooQuxConverter():
40-
@converter(source_format=Foo, target_format=Qux)
41-
@shell.define
42-
class FooQuxConverter_(shell.Task["FooQuxConverter_.Outputs"]):
43-
44-
in_file: File = shell.arg(help="the input file", argstr="")
45-
executable = "cp"
46-
47-
class Outputs(shell.Outputs):
48-
out_file: File = shell.outarg(
49-
help="output file",
50-
argstr="",
51-
position=-1,
52-
path_template="out.qux",
53-
)
54-
55-
return FooQuxConverter_
56-
57-
58-
def test_get_converter_functask(FooBarConverter, work_dir):
42+
def test_get_converter_functask(work_dir):
5943

6044
fspath = work_dir / "test.foo"
6145
write_test_file(fspath)
6246
assert attrs.asdict(Bar.get_converter(Foo).task) == attrs.asdict(FooBarConverter())
6347

6448

65-
def test_get_converter_shellcmd(FooQuxConverter, work_dir):
49+
def test_get_converter_shellcmd(work_dir):
6650

6751
fspath = work_dir / "test.foo"
6852
write_test_file(fspath)
@@ -77,7 +61,7 @@ def test_get_converter_fail(work_dir):
7761
Baz.get_converter(Foo)
7862

7963

80-
def test_convert_functask(FooBarConverter, work_dir):
64+
def test_convert_functask(work_dir):
8165

8266
fspath = work_dir / "test.foo"
8367
write_test_file(fspath)
@@ -87,7 +71,7 @@ def test_convert_functask(FooBarConverter, work_dir):
8771
assert bar.raw_contents == foo.raw_contents
8872

8973

90-
def test_convert_shellcmd(FooQuxConverter, work_dir):
74+
def test_convert_shellcmd(work_dir):
9175

9276
fspath = work_dir / "test.foo"
9377
write_test_file(fspath)
@@ -97,11 +81,19 @@ def test_convert_shellcmd(FooQuxConverter, work_dir):
9781
assert qux.raw_contents == foo.raw_contents
9882

9983

100-
def test_convert_mapped_conversion(BazBarConverter, work_dir):
84+
def test_convert_mapped_conversion(work_dir):
10185

10286
fspath = work_dir / "test.baz"
10387
write_test_file(fspath)
10488
baz = Baz(fspath)
10589
bar = Bar.convert(baz)
10690
assert type(bar) is Bar
10791
assert bar.raw_contents == baz.raw_contents
92+
93+
94+
def test_convertible_from():
95+
96+
assert Bar.convertible_from() == ty.Union[Bar, Foo, Baz]
97+
assert Qux.convertible_from() == ty.Union[Qux, Foo]
98+
assert Foo.convertible_from() == Foo
99+
assert Baz.convertible_from() == Baz

fileformats/core/fileset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,16 @@ def get_converters_dict(
624624
converters_dict = klass.converters = {}
625625
return converters_dict
626626

627+
@classmethod
628+
def convertible_from(cls) -> ty.Type[DataType]:
629+
"""Union of types that can be converted to this type, including the current type.
630+
If there are no other types that can be converted to this type, return the current type
631+
"""
632+
datatypes = (cls,) + tuple(cls.get_converters_dict().keys())
633+
if len(datatypes) == 1:
634+
return cls
635+
return ty.Union.__getitem__(datatypes) # type: ignore[return-value]
636+
627637
@classmethod
628638
def get_converter_defs(cls, source_format: type) -> ty.List["Converter"]:
629639
"""Search the registered converters to find any matches and return list of

0 commit comments

Comments
 (0)