From d1cdb8131be2f7934257d264f305f72b4ea2e187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Wed, 26 Jul 2023 13:55:01 -0600 Subject: [PATCH 1/2] refactor: Use `importlib.resources` instead of `__file__` to retrieve sample Singer output files --- singer_sdk/helpers/_compat.py | 7 ++++++- singer_sdk/testing/target_test_streams/__init__.py | 1 + singer_sdk/testing/templates.py | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 singer_sdk/testing/target_test_streams/__init__.py diff --git a/singer_sdk/helpers/_compat.py b/singer_sdk/helpers/_compat.py index 87033ea4c..20b7a399a 100644 --- a/singer_sdk/helpers/_compat.py +++ b/singer_sdk/helpers/_compat.py @@ -11,4 +11,9 @@ from importlib import metadata from typing import final # noqa: ICN003 -__all__ = ["metadata", "final"] +if sys.version_info < (3, 9): + import importlib_resources as resources +else: + from importlib import resources + +__all__ = ["metadata", "final", "resources"] diff --git a/singer_sdk/testing/target_test_streams/__init__.py b/singer_sdk/testing/target_test_streams/__init__.py new file mode 100644 index 000000000..14d313288 --- /dev/null +++ b/singer_sdk/testing/target_test_streams/__init__.py @@ -0,0 +1 @@ +"""Singer output samples, used for testing target behavior.""" diff --git a/singer_sdk/testing/templates.py b/singer_sdk/testing/templates.py index b43d37830..5e75051d6 100644 --- a/singer_sdk/testing/templates.py +++ b/singer_sdk/testing/templates.py @@ -7,6 +7,9 @@ import warnings from pathlib import Path +from singer_sdk.helpers._compat import resources +from singer_sdk.testing import target_test_streams + if t.TYPE_CHECKING: from singer_sdk.streams import Stream @@ -334,5 +337,4 @@ def singer_filepath(self) -> Path: Returns: The expected Path to this tests singer file. """ - current_dir = Path(__file__).resolve().parent - return current_dir / "target_test_streams" / f"{self.name}.singer" + return resources.files(target_test_streams) / f"{self.name}.singer" From 244932a148fb4b9bc8a874147f8f88792f3ff0d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Wed, 26 Jul 2023 13:59:17 -0600 Subject: [PATCH 2/2] Address mypy false positive --- singer_sdk/testing/templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/singer_sdk/testing/templates.py b/singer_sdk/testing/templates.py index 5e75051d6..bfb54c360 100644 --- a/singer_sdk/testing/templates.py +++ b/singer_sdk/testing/templates.py @@ -337,4 +337,4 @@ def singer_filepath(self) -> Path: Returns: The expected Path to this tests singer file. """ - return resources.files(target_test_streams) / f"{self.name}.singer" + return resources.files(target_test_streams).joinpath(f"{self.name}.singer") # type: ignore[no-any-return] # noqa: E501