Skip to content

Commit aac78a0

Browse files
authored
Merge pull request #120 from ArcanaFramework/subtypevar-convertible-from
handle subtypevars in convertible_from
2 parents 61b391b + a65ff28 commit aac78a0

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

extras/fileformats/extras/testing/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ def ConvertibleToConverter(in_file: AbstractFile) -> ConvertibleToFile:
1616

1717
@converter # pyright: ignore[reportArgumentType]
1818
@python.define(outputs=["out_file"]) # type: ignore[misc]
19-
def EncodedFromTextConverter(in_file: TextFile) -> EncodedText:
19+
def EncodedFromTextConverter(in_file: TextFile, shift: int = 1) -> EncodedText:
2020
contents = in_file.read_text()
2121
# Encode by shifting ASCII codes forward by 1
22-
encoded_contents = "".join(chr(ord(c) + 1) for c in contents)
22+
encoded_contents = "".join(chr(ord(c) + shift) for c in contents)
2323
out_file = EncodedText.sample()
2424
out_file.write_text(encoded_contents)
2525
return out_file
2626

2727

2828
@converter # pyright: ignore[reportArgumentType]
2929
@python.define(outputs=["out_file"]) # type: ignore[misc]
30-
def EncodedToTextConverter(in_file: EncodedText) -> TextFile:
30+
def EncodedToTextConverter(in_file: EncodedText, shift: int = 1) -> TextFile:
3131
contents = in_file.read_text()
3232
# Decode by shifting ASCII codes back by 1
33-
decoded_contents = "".join(chr(ord(c) - 1) for c in contents)
33+
decoded_contents = "".join(chr(ord(c) - shift) for c in contents)
3434
out_file = TextFile.sample()
3535
out_file.write_text(decoded_contents)
3636
return out_file

fileformats/core/fileset.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from typing_extensions import TypeAlias
2525

2626
from fileformats.core.typing import Self
27+
from fileformats.core.converter_helpers import SubtypeVar
2728
from fileformats.core.utils import _excluded_subpackages
2829

2930
from .classifier import Classifier
@@ -670,8 +671,6 @@ def convertible_from(
670671
671672
Parameters
672673
----------
673-
include_generic: bool
674-
If True, only consider parent classes in the same namespace for conversion.
675674
union_sort_key : callable[[DataType], Any], optional
676675
A function used to sort the union of types. Defaults to sorting by the type name.
677676
@@ -683,9 +682,13 @@ def convertible_from(
683682

684683
datatypes: ty.List[ty.Type[DataType]] = [cls]
685684
cls._import_extras_module()
686-
exclude_subpackages = copy(_excluded_subpackages)
687-
exclude_subpackages.discard(cls.namespace)
688-
for subcls in FileSet.subclasses(exclude=exclude_subpackages):
685+
if cls.namespace == "generic":
686+
subclasses = [cls]
687+
else:
688+
exclude_subpackages = copy(_excluded_subpackages)
689+
exclude_subpackages.discard(cls.namespace)
690+
subclasses = list(FileSet.subclasses(exclude=exclude_subpackages))
691+
for subcls in subclasses:
689692
if issubclass(subcls, cls):
690693
subcls._import_extras_module()
691694
datatypes.extend(subcls.get_converters_dict().keys())
@@ -698,6 +701,8 @@ def add_concrete(datatype: ty.Type[DataType]) -> None:
698701
if inspect.isabstract(datatype):
699702
for subclass in datatype.subclasses():
700703
add_concrete(subclass)
704+
elif issubclass(datatype, SubtypeVar):
705+
concrete_datatypes.add(datatype.bound)
701706
else:
702707
concrete_datatypes.add(datatype)
703708

0 commit comments

Comments
 (0)