diff --git a/dlt/feature/__init__.py b/dlt/feature/__init__.py new file mode 100644 index 0000000000..c36f28de0f --- /dev/null +++ b/dlt/feature/__init__.py @@ -0,0 +1,10 @@ +try: + from dlt_example_plugin.feature import SupportsExtendedFeature as SupportsFeature + from dlt_example_plugin.feature import ExtendedFeature as Feature +except ImportError: + from dlt.feature.reference import SupportsSimpleFeature as SupportsFeature + from dlt.feature.impl import SimpleFeature as Feature + + +def get_feature() -> SupportsFeature: + return Feature() diff --git a/dlt/feature/impl.py b/dlt/feature/impl.py new file mode 100644 index 0000000000..51b8ad74e1 --- /dev/null +++ b/dlt/feature/impl.py @@ -0,0 +1,6 @@ +from dlt.feature.reference import SupportsSimpleFeature + + +class SimpleFeature(SupportsSimpleFeature): + def calculate(self, a: int, b: int) -> int: + return a + b diff --git a/dlt/feature/reference.py b/dlt/feature/reference.py new file mode 100644 index 0000000000..281e480dce --- /dev/null +++ b/dlt/feature/reference.py @@ -0,0 +1,5 @@ +from typing import Protocol + + +class SupportsSimpleFeature(Protocol): + def calculate(self, a: int, b: int) -> int: ... diff --git a/tests/plugins/dlt_example_plugin/dlt_example_plugin/feature/__init__.py b/tests/plugins/dlt_example_plugin/dlt_example_plugin/feature/__init__.py new file mode 100644 index 0000000000..c45516e8e2 --- /dev/null +++ b/tests/plugins/dlt_example_plugin/dlt_example_plugin/feature/__init__.py @@ -0,0 +1,21 @@ +from typing import TYPE_CHECKING + +from dlt.feature.impl import SimpleFeature +from dlt.feature.reference import SupportsSimpleFeature + + +# extend interface +class SupportsExtendedFeature(SupportsSimpleFeature): + # add new method + def more_calculation(self, a: int, b: int) -> int: ... + + +# extend implementation +class ExtendedFeature(SupportsExtendedFeature, SimpleFeature): + # override a method + def calculate(self, a: int, b: int) -> int: + return a * b + + # add a new method + def more_calculation(self, a: int, b: int) -> int: + return a**b diff --git a/tests/plugins/test_plugin_discovery.py b/tests/plugins/test_plugin_discovery.py index 6962e89bf7..a47bc8db82 100644 --- a/tests/plugins/test_plugin_discovery.py +++ b/tests/plugins/test_plugin_discovery.py @@ -45,7 +45,10 @@ def plugin_install(): sys.path.remove(temp_dir) shutil.rmtree(temp_dir) importlib.reload(importlib.metadata) - del container[plugins.PluginContext] + if plugins.PluginContext in container: + del container[plugins.PluginContext] + + def test_example_plugin() -> None: @@ -83,3 +86,11 @@ def test_cli_hook(script_runner: ScriptRunner) -> None: assert result.returncode == -55 assert "Plugin overwrote init command" in result.stdout assert "INIT_DOCS_URL" in result.stdout + + +def test_extended_feature(): + from dlt.feature import get_feature + + feature = get_feature() + assert feature.calculate(3, 3) == 9 + assert feature.more_calculation(3, 3) == 27 diff --git a/tests/plugins/test_simple_feature.py b/tests/plugins/test_simple_feature.py new file mode 100644 index 0000000000..2fb4041d1c --- /dev/null +++ b/tests/plugins/test_simple_feature.py @@ -0,0 +1,5 @@ +def test_simple_feature(): + from dlt.feature import get_feature + + feature = get_feature() + assert feature.calculate(3, 3) == 6