Skip to content

Commit d01125e

Browse files
chore: misc typing improvements + type-check helpers.py in CMS
1 parent 1a9833d commit d01125e

5 files changed

Lines changed: 23 additions & 18 deletions

File tree

cms/djangoapps/contentstore/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from openedx.core.djangoapps.content_tagging.types import TagValuesByObjectIdDict
4040
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
4141
from openedx.core.djangoapps.video_config.transcripts_utils import Transcript, build_components_import_path
42+
from openedx.core.types import AuthUser as UserType
4243
from xmodule.contentstore.content import StaticContent
4344
from xmodule.contentstore.django import contentstore
4445
from xmodule.exceptions import NotFoundError
@@ -454,7 +455,7 @@ def _fetch_and_set_upstream_link(
454455
copied_from_block: str,
455456
copied_from_version_num: int,
456457
temp_xblock: XBlock,
457-
user: User
458+
user: UserType,
458459
):
459460
"""
460461
Fetch and set upstream link for the given xblock which is being pasted. This function handles following cases:
@@ -515,7 +516,7 @@ def _import_xml_node_to_parent(
515516
# The modulestore we're using
516517
store,
517518
# The user who is performing this operation
518-
user: User,
519+
user: UserType,
519520
# Hint to use as usage ID (block_id) for the new XBlock
520521
slug_hint: str | None = None,
521522
# Content tags applied to the source XBlock(s)

mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ files =
99
cms/lib/xblock,
1010
cms/djangoapps/contentstore/rest_api/v2/views,
1111
cms/djangoapps/contentstore/xblock_storage_handlers,
12+
cms/djangoapps/contentstore/helpers.py,
1213
cms/djangoapps/modulestore_migrator,
1314
openedx/core/djangoapps/content/learning_sequences,
1415
# FIXME: need to solve type issues and add 'search' app here:

openedx/core/djangoapps/content_staging/tests/test_clipboard.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ClipboardTestCase(ModuleStoreTestCase):
3535
Test Clipboard functionality
3636
"""
3737

38-
def test_empty_clipboard(self):
38+
def test_empty_clipboard(self) -> None:
3939
"""
4040
When a user has no content on their clipboard, we get an empty 200 response
4141
"""
@@ -70,7 +70,7 @@ def _setup_course(self):
7070

7171
return (course_key, client)
7272

73-
def test_copy_video(self):
73+
def test_copy_video(self) -> None:
7474
"""
7575
Test copying a video from the course, and retrieve it using the REST API
7676
"""
@@ -103,7 +103,7 @@ def test_copy_video(self):
103103
# Now if we GET the clipboard again, the GET response should exactly equal the last POST response:
104104
assert client.get(CLIPBOARD_ENDPOINT).json() == response_data
105105

106-
def test_copy_video_python_get(self):
106+
def test_copy_video_python_get(self) -> None:
107107
"""
108108
Test copying a video from the course, and retrieve it using the python API
109109
"""
@@ -126,9 +126,10 @@ def test_copy_video_python_get(self):
126126
assert clipboard_data.content.display_name == "default"
127127
# Test the actual OLX in the clipboard:
128128
olx_data = python_api.get_staged_content_olx(clipboard_data.content.id)
129+
assert olx_data is not None
129130
self.assertXmlEqual(olx_data, SAMPLE_VIDEO_OLX)
130131

131-
def test_copy_html(self):
132+
def test_copy_html(self) -> None:
132133
"""
133134
Test copying an HTML XBlock from the course
134135
"""
@@ -166,7 +167,7 @@ def test_copy_html(self):
166167
# Now if we GET the clipboard again, the GET response should exactly equal the last POST response:
167168
assert client.get(CLIPBOARD_ENDPOINT).json() == response_data
168169

169-
def test_copy_unit(self):
170+
def test_copy_unit(self) -> None:
170171
"""
171172
Test copying a unit (vertical block) from the course
172173
"""
@@ -242,7 +243,7 @@ def test_copy_unit(self):
242243
# Now if we GET the clipboard again, the GET response should exactly equal the last POST response:
243244
assert client.get(CLIPBOARD_ENDPOINT).json() == response_data
244245

245-
def test_copy_several_things(self):
246+
def test_copy_several_things(self) -> None:
246247
"""
247248
Test that the clipboard only holds one thing at a time.
248249
"""
@@ -275,7 +276,7 @@ def test_copy_several_things(self):
275276
# The OLX link from the video will no longer work:
276277
assert client.get(old_olx_url).status_code == 404
277278

278-
def test_copy_static_assets(self):
279+
def test_copy_static_assets(self) -> None:
279280
"""
280281
Test copying an HTML from the course that references a static asset file.
281282
"""
@@ -297,6 +298,7 @@ def test_copy_static_assets(self):
297298
response_data = response.json()
298299
staged_content_id = cast(python_api.StagedContentID, response_data["content"]["id"])
299300
olx_str = python_api.get_staged_content_olx(staged_content_id)
301+
assert olx_str is not None
300302
assert '<img src="/static/foo_bar.jpg" />' in olx_str
301303
static_assets = python_api.get_staged_content_static_files(staged_content_id)
302304

@@ -307,7 +309,7 @@ def test_copy_static_assets(self):
307309
data=None,
308310
)]
309311

310-
def test_copy_static_assets_nonexistent(self):
312+
def test_copy_static_assets_nonexistent(self) -> None:
311313
"""
312314
Test copying a HTML block which references non-existent static assets.
313315
"""
@@ -332,11 +334,12 @@ def test_copy_static_assets_nonexistent(self):
332334
response_data = response.json()
333335
staged_content_id = response_data["content"]["id"]
334336
olx_str = python_api.get_staged_content_olx(staged_content_id)
337+
assert olx_str is not None
335338
assert '<a href="/static/nonexistent1.jpg">' in olx_str
336339
static_assets = python_api.get_staged_content_static_files(staged_content_id)
337340
assert static_assets == []
338341

339-
def test_no_course_permission(self):
342+
def test_no_course_permission(self) -> None:
340343
"""
341344
Test that a user without read access cannot copy items in a course
342345
"""
@@ -353,7 +356,7 @@ def test_no_course_permission(self):
353356
response = nonstaff_client.get(CLIPBOARD_ENDPOINT)
354357
assert response.json()["content"] is None
355358

356-
def test_no_stealing_clipboard_content(self):
359+
def test_no_stealing_clipboard_content(self) -> None:
357360
"""
358361
Test that a user cannot see another user's clipboard
359362
"""
@@ -370,7 +373,7 @@ def test_no_stealing_clipboard_content(self):
370373
response = nonstaff_client.get(olx_url)
371374
assert response.status_code == 403
372375

373-
def assertXmlEqual(self, xml_str_a: str, xml_str_b: str):
376+
def assertXmlEqual(self, xml_str_a: str, xml_str_b: str) -> None:
374377
""" Assert that the given XML strings are equal, ignoring attribute order and some whitespace variations. """
375378
a = ElementTree.canonicalize(xml_str_a, strip_text=True)
376379
b = ElementTree.canonicalize(xml_str_b, strip_text=True)

openedx/core/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"""
44
from django.contrib.admin import display as admin_display # noqa: F401
55

6-
from .user import User # noqa: F401
6+
from .user import AuthUser, User # noqa: F401

openedx/core/types/user.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"""
44
from __future__ import annotations
55

6-
import typing as t
7-
86
import django.contrib.auth.models
97

10-
AuthUser: t.TypeAlias = django.contrib.auth.models.User # noqa: UP040
11-
User: t.TypeAlias = django.contrib.auth.models.User | django.contrib.auth.models.AnonymousUser # noqa: UP040
8+
# base type for an authenticated user
9+
type AuthUser = django.contrib.auth.models.User
10+
# base type for a generic user making an HTTP request, which may or may not be authenticated:
11+
type User = AuthUser | django.contrib.auth.models.AnonymousUser

0 commit comments

Comments
 (0)