From 3d9a893615721cc118379b36168d0752b5295ec1 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:31:50 -0500 Subject: [PATCH 01/10] refactor: replace deprecated pkg_resources API --- flow_control/flow.py | 5 ++--- flow_control/tests/test_flowcontrol.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/flow_control/flow.py b/flow_control/flow.py index 4162f46..4cb71b6 100644 --- a/flow_control/flow.py +++ b/flow_control/flow.py @@ -2,7 +2,7 @@ either display the unit's content or take an alternative action """ import logging -import pkg_resources +from importlib.resources import files as importlib_files import re from functools import reduce @@ -24,8 +24,7 @@ def load(path): """Handy helper for getting resources from our kit.""" - data = pkg_resources.resource_string(__name__, path) - return data.decode("utf8") + return importlib_files(__package__).joinpath(path).read_text(encoding="utf-8") def _actions_generator(block): # pylint: disable=unused-argument diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index 74cc773..b000b5d 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -89,9 +89,9 @@ def test_load(self): It should return the corresponding resource """ path_mock = MagicMock() - with patch('pkg_resources.resource_string') as my_patch: + with patch('importlib.resources.files') as my_patch: load(path_mock) - my_patch.assert_called_once_with('flow_control.flow', path_mock) + my_patch.assert_called_once_with('flow_control.flow').joinpath(path_mock).read_text(encoding="utf-8") @ddt.data( 'course-v1:Course+course+course', From 96fc1b3b084eabad940b007022ac1d76ca9c31f6 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:39:03 -0500 Subject: [PATCH 02/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index b000b5d..e4eda4f 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -89,9 +89,15 @@ def test_load(self): It should return the corresponding resource """ path_mock = MagicMock() - with patch('importlib.resources.files') as my_patch: - load(path_mock) - my_patch.assert_called_once_with('flow_control.flow').joinpath(path_mock).read_text(encoding="utf-8") + + with patch("importlib.resources.files") as mock_files: + mock_package = mock_files.return_value + mock_path = mock_package.joinpath.return_value + with patch.object(mock_path, "read_text") as mock_read_text: + load(path_mock) + mock_files.assert_called_once_with("flow_control.flow") + mock_package.joinpath.assert_called_once_with(path_mock) + mock_read_text.assert_called_once_with(encoding="utf-8") @ddt.data( 'course-v1:Course+course+course', From e2db6745c504899e5a1d7bb07920957474b04fe2 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:42:24 -0500 Subject: [PATCH 03/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index e4eda4f..15eb928 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -95,7 +95,7 @@ def test_load(self): mock_path = mock_package.joinpath.return_value with patch.object(mock_path, "read_text") as mock_read_text: load(path_mock) - mock_files.assert_called_once_with("flow_control.flow") + mock_files.assert_called_once_with("flow_control") mock_package.joinpath.assert_called_once_with(path_mock) mock_read_text.assert_called_once_with(encoding="utf-8") From 313b1b61fe3be30071120e5cd8014e92614b8e3b Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:44:20 -0500 Subject: [PATCH 04/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index 15eb928..e2a178e 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -95,7 +95,7 @@ def test_load(self): mock_path = mock_package.joinpath.return_value with patch.object(mock_path, "read_text") as mock_read_text: load(path_mock) - mock_files.assert_called_once_with("flow_control") + mock_files.assert_called_once_with(__package__) mock_package.joinpath.assert_called_once_with(path_mock) mock_read_text.assert_called_once_with(encoding="utf-8") From 9917a704e0a7c8de09c9a69fc6a8e078b2a5f5e2 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:49:21 -0500 Subject: [PATCH 05/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index e2a178e..d006acc 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -88,16 +88,20 @@ def test_load(self): """ It should return the corresponding resource """ - path_mock = MagicMock() - - with patch("importlib.resources.files") as mock_files: - mock_package = mock_files.return_value - mock_path = mock_package.joinpath.return_value - with patch.object(mock_path, "read_text") as mock_read_text: - load(path_mock) - mock_files.assert_called_once_with(__package__) - mock_package.joinpath.assert_called_once_with(path_mock) - mock_read_text.assert_called_once_with(encoding="utf-8") + path_mock = "test_resource.txt" + + with patch("importlib.resources.files") as files_mock, patch("importlib.resources.as_file") as as_file_mock: + file_path_mock = MagicMock() + files_mock.return_value.joinpath.return_value = file_path_mock + as_file_mock.return_value.__enter__.return_value = file_path_mock + file_path_mock.read_text.return_value = "mocked content" + + result = load(path_mock) + + files_mock.assert_called_once_with("flow_control.flow") + file_path_mock.read_text.assert_called_once_with(encoding="utf-8") + assert result == "mocked content" + @ddt.data( 'course-v1:Course+course+course', From 0f1b7327fe3d1ff64cf7ecaa557ea586ffddeb1d Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:51:27 -0500 Subject: [PATCH 06/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index d006acc..eee0a43 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -84,18 +84,18 @@ def test_operators_generator(self): ] self.assertEqual(operators, operators_allowed) - def test_load(self): + def test_load(): """ It should return the corresponding resource """ path_mock = "test_resource.txt" - with patch("importlib.resources.files") as files_mock, patch("importlib.resources.as_file") as as_file_mock: + with patch("importlib.resources.files") as files_mock: file_path_mock = MagicMock() - files_mock.return_value.joinpath.return_value = file_path_mock - as_file_mock.return_value.__enter__.return_value = file_path_mock file_path_mock.read_text.return_value = "mocked content" + files_mock.return_value.joinpath.return_value = file_path_mock + result = load(path_mock) files_mock.assert_called_once_with("flow_control.flow") From 4158ca52a15abb0fbae01343be4d20391588b3e1 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:53:57 -0500 Subject: [PATCH 07/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index eee0a43..5c08337 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -88,19 +88,10 @@ def test_load(): """ It should return the corresponding resource """ - path_mock = "test_resource.txt" - - with patch("importlib.resources.files") as files_mock: - file_path_mock = MagicMock() - file_path_mock.read_text.return_value = "mocked content" - - files_mock.return_value.joinpath.return_value = file_path_mock - - result = load(path_mock) - - files_mock.assert_called_once_with("flow_control.flow") - file_path_mock.read_text.assert_called_once_with(encoding="utf-8") - assert result == "mocked content" + path_mock = MagicMock() + with patch('pkg_resources.resource_string') as my_patch: + load(path_mock) + my_patch.assert_called_once_with('flow_control.flow', path_mock) @ddt.data( From b9e441b993b910ebd0180f305bb74704674474a2 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 01:55:59 -0500 Subject: [PATCH 08/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index 5c08337..6385e3c 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -84,15 +84,23 @@ def test_operators_generator(self): ] self.assertEqual(operators, operators_allowed) - def test_load(): + def test_load(self): """ It should return the corresponding resource """ - path_mock = MagicMock() - with patch('pkg_resources.resource_string') as my_patch: - load(path_mock) - my_patch.assert_called_once_with('flow_control.flow', path_mock) + path_mock = "test_resource.txt" + with patch("importlib.resources.files") as files_mock: + file_path_mock = MagicMock() + file_path_mock.read_text.return_value = "mocked content" + + files_mock.return_value.joinpath.return_value = file_path_mock + + result = load(path_mock) + + files_mock.assert_called_once_with("flow_control.flow") + file_path_mock.read_text.assert_called_once_with(encoding="utf-8") + self.assertEqual(result, "mocked content") @ddt.data( 'course-v1:Course+course+course', From 541eee0f88d0d49b0bca7600e7cca302e78d3b29 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 02:01:29 -0500 Subject: [PATCH 09/10] chore: migrate test_load --- flow_control/tests/test_flowcontrol.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/flow_control/tests/test_flowcontrol.py b/flow_control/tests/test_flowcontrol.py index 6385e3c..effbd03 100644 --- a/flow_control/tests/test_flowcontrol.py +++ b/flow_control/tests/test_flowcontrol.py @@ -5,7 +5,7 @@ import ddt import unittest -from mock import MagicMock, patch +from mock import MagicMock, patch, mock_open # from xblock.core import XBlock from xblock.field_data import DictFieldData from flow_control.flow import FlowCheckPointXblock @@ -85,22 +85,13 @@ def test_operators_generator(self): self.assertEqual(operators, operators_allowed) def test_load(self): - """ - It should return the corresponding resource - """ + """It should return the corresponding resource""" path_mock = "test_resource.txt" + mock_content = "mocked content" - with patch("importlib.resources.files") as files_mock: - file_path_mock = MagicMock() - file_path_mock.read_text.return_value = "mocked content" - - files_mock.return_value.joinpath.return_value = file_path_mock - + with patch("pathlib.Path.open", mock_open(read_data=mock_content)): result = load(path_mock) - - files_mock.assert_called_once_with("flow_control.flow") - file_path_mock.read_text.assert_called_once_with(encoding="utf-8") - self.assertEqual(result, "mocked content") + self.assertEqual(result, mock_content) @ddt.data( 'course-v1:Course+course+course', From 1768e5b2482d11bdc150fd665a8925e53fa44c76 Mon Sep 17 00:00:00 2001 From: Luis Felipe Castano Date: Wed, 5 Feb 2025 02:04:10 -0500 Subject: [PATCH 10/10] chore: update version and changelog --- CHANGELOG.rst | 11 +++++++++++ flow_control/__init__.py | 2 +- setup.cfg | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5fd6947..2639099 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,17 @@ Unreleased * +[2.0.1] - 2025-02-05 +********************************************** + +Changed +======= + +* **Replaced `pkg_resources` with `importlib.resources`** + - `pkg_resources` (from `setuptools`) is deprecated and may be removed in future Python versions. + - Now using `importlib.resources`, the recommended alternative for managing package resources. + - This improves performance and ensures better compatibility with modern Python versions. + [2.0.0] - 2025-01-23 ********************************************** diff --git a/flow_control/__init__.py b/flow_control/__init__.py index 84119fa..965b31a 100644 --- a/flow_control/__init__.py +++ b/flow_control/__init__.py @@ -2,4 +2,4 @@ Init for main Flow-Control XBlock """ from .flow import FlowCheckPointXblock -__version__ = '2.0.0' +__version__ = '2.0.1' diff --git a/setup.cfg b/setup.cfg index 592dcb8..4473f95 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 2.0.0 +current_version = 2.0.1 commit = True tag = True