From e048432336cad94554a4096edc6f42a23d8d7969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Wed, 5 Feb 2025 17:40:18 -0300 Subject: [PATCH 1/2] fix: throws InvalidScopeError if the FieldData for the scope is None --- xblock/field_data.py | 6 ++++-- xblock/test/test_field_data.py | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/xblock/field_data.py b/xblock/field_data.py index 7d3d8abbf..faa66e394 100644 --- a/xblock/field_data.py +++ b/xblock/field_data.py @@ -145,10 +145,12 @@ def _field_data(self, block, name): """Return the field data for the field `name` on the :class:`~xblock.core.XBlock` `block`""" scope = block.fields[name].scope - if scope not in self._scope_mappings: + scope_mapping = self._scope_mappings.get(scope) + + if scope_mapping is None: raise InvalidScopeError(scope) - return self._scope_mappings[scope] + return scope_mapping def get(self, block, name): return self._field_data(block, name).get(block, name) diff --git a/xblock/test/test_field_data.py b/xblock/test/test_field_data.py index ce568ba3e..eaf12281e 100644 --- a/xblock/test/test_field_data.py +++ b/xblock/test/test_field_data.py @@ -40,6 +40,11 @@ def setup_method(self): Scope.content: self.content, Scope.settings: self.settings }) + self.split_empty = SplitFieldData({ + Scope.content: self.content, + Scope.settings: self.settings, + Scope.user_state: None, + }) self.runtime = TestRuntime(services={'field-data': self.split}) self.block = TestingBlock( runtime=self.runtime, @@ -76,6 +81,10 @@ def test_invalid_scope(self): with pytest.raises(InvalidScopeError): self.split.get(self.block, 'user_state') + def test_empty_scope(self): + with pytest.raises(InvalidScopeError): + self.split_empty.get(self.block, 'user_state') + def test_default(self): self.split.default(self.block, 'content') self.content.default.assert_called_once_with(self.block, 'content') From 40cf6470e83abe199672a4aa5fb317ee3bad0554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Thu, 6 Feb 2025 16:49:27 -0300 Subject: [PATCH 2/2] docs: add entry on changelog --- CHANGELOG.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6a5eff3a9..a67ab843e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,8 @@ Change history for XBlock Unreleased ---------- +* throws InvalidScopeError if the scope is not defined + 5.1.0 - 2024-08-07 ------------------