Skip to content

Commit

Permalink
Merge pull request #87 from ArcanaFramework/pep604-union-types
Browse files Browse the repository at this point in the history
added tests to check pep604 union types in typed collections
  • Loading branch information
tclose authored Sep 23, 2024
2 parents 85fc309 + a3b428a commit 8010f0f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions fileformats/generic/tests/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import typing as ty
import pytest
from fileformats.generic import Directory, DirectoryOf, SetOf
Expand Down Expand Up @@ -70,3 +71,36 @@ def test_set_optional_contents():
sample_set = SetOf[ty.Optional[MyFormatGz]](my_format)
assert sample_set.contents == [my_format]
assert list(sample_set.required_paths()) == [my_format.fspath]


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_directory_optional_contents_pep604(tmp_path):
my_format = MyFormatGz.sample(dest_dir=tmp_path)
sample_dir = DirectoryOf[MyFormatGz](tmp_path)
EncodedText.sample(dest_dir=tmp_path)
assert sample_dir.contents == [my_format]

optional_dir = DirectoryOf[MyFormatGz, YourFormat | None](sample_dir)
assert optional_dir.contents == [my_format]

your_format = YourFormat.sample(dest_dir=tmp_path)
optional_dir = DirectoryOf[MyFormatGz, YourFormat | None](sample_dir)
assert optional_dir.contents == [my_format, your_format]


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_set_optional_contents_pep604():
my_format = MyFormatGz.sample()
your_format = YourFormat.sample()

sample_set = SetOf[MyFormatGz, YourFormat | None](my_format)
assert sample_set.contents == [my_format]
assert list(sample_set.required_paths()) == [my_format.fspath]

sample_set = SetOf[MyFormatGz, YourFormat | None](my_format, your_format)
assert sample_set.contents == [my_format, your_format]
assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath}

sample_set = SetOf[MyFormatGz | None](my_format)
assert sample_set.contents == [my_format]
assert list(sample_set.required_paths()) == [my_format.fspath]

0 comments on commit 8010f0f

Please sign in to comment.