Skip to content

Commit e1810ea

Browse files
committed
Merge remote-tracking branch 'origin/master' into navin/fal-4077/fix-new-locators
2 parents e9f7d4a + 15ae68c commit e1810ea

File tree

6 files changed

+66
-56
lines changed

6 files changed

+66
-56
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.13.0
2+
3+
* Breaking change to the new LibraryContainerLocator and
4+
LibraryCollectionLocator keys: rename 'library_key' to 'lib_key'
5+
* Updated some imports now that we require python 3.11+
6+
17
# 2.12.0
28

39
* Refactor: Rename LibraryCollectionKey to LibraryItemKey.

opaque_keys/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
from abc import ABCMeta, abstractmethod
1111
from collections import defaultdict
1212
from functools import total_ordering
13+
from typing import Self
1314

1415
from stevedore.enabled import EnabledExtensionManager
15-
from typing_extensions import Self # For python 3.11 plus, can just use "from typing import Self"
1616

17-
__version__ = '2.12.0'
17+
__version__ = '2.13.0'
1818

1919

2020
class InvalidKeyError(Exception):

opaque_keys/edx/keys.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from __future__ import annotations
55
import json
66
from abc import abstractmethod
7-
from typing import TYPE_CHECKING
7+
from typing import TYPE_CHECKING, Self
88
import warnings
99

10-
from typing_extensions import Self # For python 3.11 plus, can just use "from typing import Self"
10+
from typing_extensions import deprecated # For python 3.13+ can use 'from warnings import deprecated'
1111

1212
from opaque_keys import OpaqueKey
1313

@@ -98,7 +98,7 @@ class LibraryItemKey(OpaqueKey):
9898
An :class:`opaque_keys.OpaqueKey` identifying a particular item in a library.
9999
"""
100100
KEY_TYPE = 'library_item_key'
101-
library_key: LibraryLocatorV2
101+
lib_key: LibraryLocatorV2
102102
__slots__ = ()
103103

104104
@property
@@ -109,6 +109,11 @@ def org(self) -> str | None: # pragma: no cover
109109
"""
110110
raise NotImplementedError()
111111

112+
@property
113+
@deprecated("Use lib_key instead")
114+
def library_key(self):
115+
return self.lib_key
116+
112117
@property
113118
@abstractmethod
114119
def context_key(self) -> LearningContextKey:

opaque_keys/edx/locator.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import inspect
66
import logging
77
import re
8-
from typing import Any
8+
from typing import Any, Self
99
import warnings
1010
from uuid import UUID
1111

1212
from bson.errors import InvalidId
1313
from bson.objectid import ObjectId
1414
from bson.son import SON
15-
from typing_extensions import Self # For python 3.11 plus, can just use "from typing import Self"
1615

1716
from opaque_keys import OpaqueKey, InvalidKeyError
1817
from opaque_keys.edx.keys import AssetKey, CourseKey, DefinitionKey, \
@@ -412,8 +411,8 @@ def _from_deprecated_string(cls, serialized: str) -> Self:
412411

413412
class LibraryLocator(BlockLocatorBase, CourseKey):
414413
"""
415-
Locates a library. Libraries are XBlock structures with a 'library' block
416-
at their root.
414+
Locates a legacy (v1) library. Legacy libraries are XBlock structures with a
415+
'library' block at their root.
417416
418417
Libraries are treated analogously to courses for now. Once opaque keys are
419418
better supported, they will no longer have the 'run' property, and may no
@@ -1065,8 +1064,8 @@ def _from_deprecated_son(cls, id_dict, run):
10651064

10661065
class LibraryUsageLocator(BlockUsageLocator):
10671066
"""
1068-
Just like BlockUsageLocator, but this points to a block stored in a library,
1069-
not a course.
1067+
Just like BlockUsageLocator, but this points to a block stored in a legacy
1068+
(v1) modulestore library.
10701069
"""
10711070
CANONICAL_NAMESPACE = 'lib-block-v1'
10721071
KEY_FIELDS = ('library_key', 'block_type', 'block_id')
@@ -1629,7 +1628,7 @@ class LibraryCollectionLocator(CheckFieldMixin, LibraryItemKey):
16291628
lib-collection:org:lib:collection-id
16301629
"""
16311630
CANONICAL_NAMESPACE = 'lib-collection'
1632-
KEY_FIELDS = ('library_key', 'collection_id')
1631+
KEY_FIELDS = ('lib_key', 'collection_id')
16331632
collection_id: str
16341633

16351634
__slots__ = KEY_FIELDS
@@ -1638,16 +1637,16 @@ class LibraryCollectionLocator(CheckFieldMixin, LibraryItemKey):
16381637
# Allow collection IDs to contian unicode characters
16391638
COLLECTION_ID_REGEXP = re.compile(r'^[\w\-.]+$', flags=re.UNICODE)
16401639

1641-
def __init__(self, library_key: LibraryLocatorV2, collection_id: str):
1640+
def __init__(self, lib_key: LibraryLocatorV2, collection_id: str):
16421641
"""
16431642
Construct a CollectionLocator
16441643
"""
1645-
if not isinstance(library_key, LibraryLocatorV2):
1646-
raise TypeError("library_key must be a LibraryLocatorV2")
1644+
if not isinstance(lib_key, LibraryLocatorV2):
1645+
raise TypeError("lib_key must be a LibraryLocatorV2")
16471646

16481647
self._check_key_string_field("collection_id", collection_id, regexp=self.COLLECTION_ID_REGEXP)
16491648
super().__init__(
1650-
library_key=library_key,
1649+
lib_key=lib_key,
16511650
collection_id=collection_id,
16521651
)
16531652

@@ -1656,13 +1655,13 @@ def org(self) -> str | None: # pragma: no cover
16561655
"""
16571656
The organization that this Collection belongs to.
16581657
"""
1659-
return self.library_key.org
1658+
return self.lib_key.org
16601659

16611660
def _to_string(self) -> str:
16621661
"""
16631662
Serialize this key as a string
16641663
"""
1665-
return ":".join((self.library_key.org, self.library_key.slug, self.collection_id))
1664+
return ":".join((self.lib_key.org, self.lib_key.slug, self.collection_id))
16661665

16671666
@classmethod
16681667
def _from_string(cls, serialized: str) -> Self:
@@ -1671,14 +1670,14 @@ def _from_string(cls, serialized: str) -> Self:
16711670
"""
16721671
try:
16731672
(org, lib_slug, collection_id) = serialized.split(':')
1674-
library_key = LibraryLocatorV2(org, lib_slug)
1675-
return cls(library_key, collection_id)
1673+
lib_key = LibraryLocatorV2(org, lib_slug)
1674+
return cls(lib_key, collection_id)
16761675
except (ValueError, TypeError) as error:
16771676
raise InvalidKeyError(cls, serialized) from error
16781677

16791678
@property
16801679
def context_key(self) -> LibraryLocatorV2:
1681-
return self.library_key
1680+
return self.lib_key
16821681

16831682

16841683
class LibraryContainerUsageLocator(LibraryUsageLocatorV2):
@@ -1694,7 +1693,7 @@ class LibraryContainerLocator(CheckFieldMixin, LibraryItemKey):
16941693
lct:org:lib:ct-type:ct-id
16951694
"""
16961695
CANONICAL_NAMESPACE = 'lct' # "Library Container"
1697-
KEY_FIELDS = ('library_key', 'container_type', 'container_id')
1696+
KEY_FIELDS = ('lib_key', 'container_type', 'container_id')
16981697
container_type: str
16991698
container_id: str
17001699

@@ -1704,17 +1703,17 @@ class LibraryContainerLocator(CheckFieldMixin, LibraryItemKey):
17041703
# Allow container IDs to contian unicode characters
17051704
CONTAINER_ID_REGEXP = re.compile(r'^[\w\-.]+$', flags=re.UNICODE)
17061705

1707-
def __init__(self, library_key: LibraryLocatorV2, container_type: str, container_id: str):
1706+
def __init__(self, lib_key: LibraryLocatorV2, container_type: str, container_id: str):
17081707
"""
17091708
Construct a CollectionLocator
17101709
"""
1711-
if not isinstance(library_key, LibraryLocatorV2):
1712-
raise TypeError("library_key must be a LibraryLocatorV2")
1710+
if not isinstance(lib_key, LibraryLocatorV2):
1711+
raise TypeError("lib_key must be a LibraryLocatorV2")
17131712

17141713
self._check_key_string_field("container_type", container_type)
17151714
self._check_key_string_field("container_id", container_id, regexp=self.CONTAINER_ID_REGEXP)
17161715
super().__init__(
1717-
library_key=library_key,
1716+
lib_key=lib_key,
17181717
container_type=container_type,
17191718
container_id=container_id,
17201719
)
@@ -1724,11 +1723,11 @@ def org(self) -> str | None: # pragma: no cover
17241723
"""
17251724
The organization that this Container belongs to.
17261725
"""
1727-
return self.library_key.org
1726+
return self.lib_key.org
17281727

17291728
@property
17301729
def context_key(self) -> LibraryLocatorV2:
1731-
return self.library_key
1730+
return self.lib_key
17321731

17331732
@property
17341733
def lib_usage_key(self) -> LibraryContainerUsageLocator:
@@ -1754,8 +1753,8 @@ def _to_string(self) -> str:
17541753
Serialize this key as a string
17551754
"""
17561755
return ":".join((
1757-
self.library_key.org,
1758-
self.library_key.slug,
1756+
self.lib_key.org,
1757+
self.lib_key.slug,
17591758
self.container_type,
17601759
self.container_id
17611760
))
@@ -1767,7 +1766,7 @@ def _from_string(cls, serialized: str) -> Self:
17671766
"""
17681767
try:
17691768
(org, lib_slug, container_type, container_id) = serialized.split(':')
1770-
library_key = LibraryLocatorV2(org, lib_slug)
1771-
return cls(library_key, container_type, container_id)
1769+
lib_key = LibraryLocatorV2(org, lib_slug)
1770+
return cls(lib_key, container_type, container_id)
17721771
except (ValueError, TypeError) as error:
17731772
raise InvalidKeyError(cls, serialized) from error

opaque_keys/edx/tests/test_collection_locators.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,35 @@ def test_coll_key_constructor(self):
2929
org = 'TestX'
3030
lib = 'LibraryX'
3131
code = 'test-problem-bank'
32-
library_key = LibraryLocatorV2(org=org, slug=lib)
33-
coll_key = LibraryCollectionLocator(library_key=library_key, collection_id=code)
34-
library_key = coll_key.library_key
32+
lib_key = LibraryLocatorV2(org=org, slug=lib)
33+
coll_key = LibraryCollectionLocator(lib_key=lib_key, collection_id=code)
34+
lib_key = coll_key.lib_key
3535
self.assertEqual(str(coll_key), "lib-collection:TestX:LibraryX:test-problem-bank")
3636
self.assertEqual(coll_key.org, org)
3737
self.assertEqual(coll_key.collection_id, code)
38-
self.assertEqual(library_key.org, org)
39-
self.assertEqual(library_key.slug, lib)
38+
self.assertEqual(lib_key.org, org)
39+
self.assertEqual(lib_key.slug, lib)
4040

4141
def test_coll_key_constructor_bad_ids(self):
42-
library_key = LibraryLocatorV2(org="TestX", slug="lib1")
42+
lib_key = LibraryLocatorV2(org="TestX", slug="lib1")
4343

4444
with self.assertRaises(ValueError):
45-
LibraryCollectionLocator(library_key=library_key, collection_id='usage-!@#{$%^&*}')
45+
LibraryCollectionLocator(lib_key=lib_key, collection_id='usage-!@#{$%^&*}')
4646
with self.assertRaises(TypeError):
47-
LibraryCollectionLocator(library_key=None, collection_id='usage')
47+
LibraryCollectionLocator(lib_key=None, collection_id='usage')
4848

4949
def test_coll_key_from_string(self):
5050
org = 'TestX'
5151
lib = 'LibraryX'
5252
code = 'test-problem-bank'
5353
str_key = f"lib-collection:{org}:{lib}:{code}"
5454
coll_key = LibraryCollectionLocator.from_string(str_key)
55-
library_key = coll_key.library_key
55+
lib_key = coll_key.lib_key
5656
self.assertEqual(str(coll_key), str_key)
5757
self.assertEqual(coll_key.org, org)
5858
self.assertEqual(coll_key.collection_id, code)
59-
self.assertEqual(library_key.org, org)
60-
self.assertEqual(library_key.slug, lib)
59+
self.assertEqual(lib_key.org, org)
60+
self.assertEqual(lib_key.slug, lib)
6161

6262
def test_coll_key_invalid_from_string(self):
6363
with self.assertRaises(InvalidKeyError):

opaque_keys/edx/tests/test_container_locators.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,34 @@ def test_key_constructor(self):
3030
lib = 'LibraryX'
3131
container_type = 'unit'
3232
container_id = 'test-container'
33-
library_key = LibraryLocatorV2(org=org, slug=lib)
33+
lib_key = LibraryLocatorV2(org=org, slug=lib)
3434
container_key = LibraryContainerLocator(
35-
library_key=library_key,
35+
lib_key=lib_key,
3636
container_type=container_type,
3737
container_id=container_id,
3838
)
39-
library_key = container_key.library_key
39+
lib_key = container_key.lib_key
4040
self.assertEqual(str(container_key), "lct:TestX:LibraryX:unit:test-container")
4141
self.assertEqual(container_key.org, org)
4242
self.assertEqual(container_key.container_type, container_type)
4343
self.assertEqual(container_key.container_id, container_id)
44-
self.assertEqual(library_key.org, org)
45-
self.assertEqual(library_key.slug, lib)
44+
self.assertEqual(lib_key.org, org)
45+
self.assertEqual(lib_key.slug, lib)
4646

4747
def test_key_constructor_bad_ids(self):
48-
library_key = LibraryLocatorV2(org="TestX", slug="lib1")
48+
lib_key = LibraryLocatorV2(org="TestX", slug="lib1")
4949

5050
with self.assertRaises(TypeError):
51-
LibraryContainerLocator(library_key=None, container_type='unit', container_id='usage')
51+
LibraryContainerLocator(lib_key=None, container_type='unit', container_id='usage')
5252

5353
with self.assertRaises(ValueError):
54-
LibraryContainerLocator(library_key=library_key, container_type='unit', container_id='usage-!@#{$%^&*}')
54+
LibraryContainerLocator(lib_key=lib_key, container_type='unit', container_id='usage-!@#{$%^&*}')
5555

5656
def test_key_constructor_bad_type(self):
57-
library_key = LibraryLocatorV2(org="TestX", slug="lib1")
57+
lib_key = LibraryLocatorV2(org="TestX", slug="lib1")
5858

5959
with self.assertRaises(ValueError):
60-
LibraryContainerLocator(library_key=library_key, container_type='unit-!@#{$%^&*}', container_id='usage')
60+
LibraryContainerLocator(lib_key=lib_key, container_type='unit-!@#{$%^&*}', container_id='usage')
6161

6262
def test_key_from_string(self):
6363
org = 'TestX'
@@ -66,10 +66,10 @@ def test_key_from_string(self):
6666
container_id = 'test-container'
6767
str_key = f"lct:{org}:{lib}:{container_type}:{container_id}"
6868
container_key = LibraryContainerLocator.from_string(str_key)
69-
library_key = container_key.library_key
69+
lib_key = container_key.lib_key
7070
self.assertEqual(str(container_key), str_key)
7171
self.assertEqual(container_key.org, org)
7272
self.assertEqual(container_key.container_type, container_type)
7373
self.assertEqual(container_key.container_id, container_id)
74-
self.assertEqual(library_key.org, org)
75-
self.assertEqual(library_key.slug, lib)
74+
self.assertEqual(lib_key.org, org)
75+
self.assertEqual(lib_key.slug, lib)

0 commit comments

Comments
 (0)