Skip to content

Commit

Permalink
Fix import tests
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
  • Loading branch information
astrojuanlu committed Jul 10, 2023
1 parent bbe5415 commit 6295f27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gitpod/workspace-full
FROM gitpod/workspace-full:2023-05-08-21-16-55

# Some datasets work on 3.8 only
RUN pyenv install 3.8.15\
Expand Down
8 changes: 4 additions & 4 deletions kedro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

__version__ = "0.18.11"

print("IMPORT KEDRO!")

class KedroPythonVersionWarning(UserWarning):
"""Custom class for warnings about incompatibilities with Python versions."""
Expand All @@ -20,9 +19,10 @@ class KedroPythonVersionWarning(UserWarning):
warnings.simplefilter("error", KedroPythonVersionWarning)

if sys.version_info >= (3, 11):
warnings.warn(KedroPythonVersionWarning(
warnings.warn(
"""Kedro is not yet fully compatible with this Python version.
To proceed at your own risk and ignore this warning,
run Kedro with `python -W "default:Kedro is not yet fully compatible" -m kedro ...`
or set the PYTHONWARNINGS environment variable accordingly.""")
)
or set the PYTHONWARNINGS environment variable accordingly.""",
KedroPythonVersionWarning,
)
47 changes: 20 additions & 27 deletions tests/test_import.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import importlib
import warnings
import pytest

def test_import_kedro_with_no_official_support_raise_error(mocker, monkeypatch):
"""Test importing kedro should fails with python>=3.11 should fails"""
import kedro
with mocker.patch("kedro.sys"):
kedro.__loader__.exec_module(kedro)
import kedro

@pytest.mark.filterwarnings("default:Kedro")
def test_import_kedro_success(mocker):
"""Test importing kedro should fails with python>=3.11 should fails"""
import kedro
with mocker.patch("kedro.sys"):
kedro.__loader__.exec_module(kedro)

def test_import_kedro_success_with_env_variable(mocker, monkeypatch):
"""Test importing kedro should fails with python>=3.11 should fails"""
import kedro
monkeypatch.setenv("PYTHONWARNINGS", "default:Kedro")
with mocker.patch("kedro.sys"):
def test_import_kedro_with_no_official_support_raise_error(mocker):
"""Test importing kedro with python>=3.11 should fail"""
mocker.patch("kedro.sys.version_info", (3, 11))

# We use the parent class to avoid issues with `exec_module`
with pytest.raises(UserWarning) as excinfo:
kedro.__loader__.exec_module(kedro)

def test_import_kedro_success_with_env_variable(mocker, monkeypatch):
"""Test importing kedro should fails with python>=3.11 should fails"""
import kedro
with mocker.patch("kedro.sys"):
assert "Kedro is not yet fully compatible" in str(excinfo.value)


def test_import_kedro_with_no_official_support_emits_warning(mocker):
"""Test importing kedro python>=3.11 and controlled warnings should work"""
mocker.patch("kedro.sys.version_info", (3, 11))
mocker.patch("kedro.sys.warnoptions", ["default:Kedro is not yet fully compatible"])

# We use the parent class to avoid issues with `exec_module`
with pytest.warns(UserWarning) as record:
kedro.__loader__.exec_module(kedro)

@pytest.mark.filterwarnings("default:Kedro")
def test_import_kedro_slient_warning(mocker, monkeypatch):
"""Test importing kedro when warning is silent"""
import kedro

assert len(record) == 1
assert "Kedro is not yet fully compatible" in record[0].message.args[0]

0 comments on commit 6295f27

Please sign in to comment.