|
3 | 3 | **scopes** to associate each field with particular sets of blocks and users. |
4 | 4 | The hosting runtime application decides what actual storage mechanism to use |
5 | 5 | for each scope. |
6 | | -
|
7 | 6 | """ |
| 7 | +from __future__ import annotations |
| 8 | + |
8 | 9 | from collections import namedtuple |
9 | 10 | import copy |
10 | 11 | import datetime |
|
15 | 16 | import traceback |
16 | 17 | import warnings |
17 | 18 |
|
| 19 | +from opaque_keys.edx.keys import UsageKey, DefinitionKey |
| 20 | + |
18 | 21 | import dateutil.parser |
19 | 22 | from lxml import etree |
20 | 23 | import pytz |
@@ -242,6 +245,27 @@ class ScopeIds(namedtuple('ScopeIds', 'user_id block_type def_id usage_id')): |
242 | 245 | """ |
243 | 246 | __slots__ = () |
244 | 247 |
|
| 248 | + def validate_types(self): |
| 249 | + """ |
| 250 | + Raise an AssertionError if any of the ids are an unexpected type. |
| 251 | +
|
| 252 | + Originally, these fields were all freely-typed; but in practice, |
| 253 | + edx-platform's XBlock runtime would fail if the ids did not match the |
| 254 | + types below. In order to make the XBlock library reflect the |
| 255 | + edx-platform reality and improve type-safety, we've decided to actually |
| 256 | + enforce the types here, per: |
| 257 | + https://github.com/openedx/XBlock/issues/708 |
| 258 | + """ |
| 259 | + if self.user_id is not None: |
| 260 | + if not isinstance(self.user_id, (int, str)): |
| 261 | + raise TypeError(f"got {self.user_id=}; should be an int, str, or None") |
| 262 | + if not isinstance(self.block_type, str): |
| 263 | + raise TypeError(f"got {self.block_type=}; should be a str") |
| 264 | + if not isinstance(self.def_id, DefinitionKey): |
| 265 | + raise TypeError(f"got {self.def_id=}; should be a DefinitionKey") |
| 266 | + if not isinstance(self.usage_id, UsageKey): |
| 267 | + raise TypeError(f"got {self.usage_id=}; should be a UsageKey") |
| 268 | + |
245 | 269 |
|
246 | 270 | # Define special reference that can be used as a field's default in field |
247 | 271 | # definition to signal that the field should default to a unique string value |
|
0 commit comments