Skip to content

Commit f996a31

Browse files
committed
fix: pylint issues for problem xblock
1 parent 32b7f27 commit f996a31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2479
-1693
lines changed

lms/djangoapps/courseware/tests/test_submitting_problems.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
OptionResponseXMLFactory,
2929
SchematicResponseXMLFactory
3030
)
31-
from xmodule.capa.tests.test_util import use_unsafe_codejail
31+
from xmodule.capa.tests.test_util import UseUnsafeCodejail
3232
from xmodule.capa.xqueue_interface import XQueueInterface
3333
from common.djangoapps.course_modes.models import CourseMode
3434
from lms.djangoapps.courseware.models import BaseStudentModuleHistory, StudentModule
@@ -811,7 +811,7 @@ def test_three_files(self, mock_xqueue_post):
811811
self.assertEqual(list(kwargs['files'].keys()), filenames.split())
812812

813813

814-
@use_unsafe_codejail()
814+
@UseUnsafeCodejail()
815815
class TestPythonGradedResponse(TestSubmittingProblems):
816816
"""
817817
Check that we can submit a schematic and custom response, and it answers properly.

lms/djangoapps/instructor_task/tests/test_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from xmodule.capa.responsetypes import StudentInputError
2424
from xmodule.capa.tests.response_xml_factory import CodeResponseXMLFactory, CustomResponseXMLFactory
25-
from xmodule.capa.tests.test_util import use_unsafe_codejail
25+
from xmodule.capa.tests.test_util import UseUnsafeCodejail
2626
from lms.djangoapps.courseware.model_data import StudentModule
2727
from lms.djangoapps.grades.api import CourseGradeFactory
2828
from lms.djangoapps.instructor_task.api import (
@@ -73,7 +73,7 @@ def _assert_task_failure(self, entry_id, task_type, problem_url_name, expected_m
7373

7474
@ddt.ddt
7575
@override_settings(RATELIMIT_ENABLE=False)
76-
@use_unsafe_codejail()
76+
@UseUnsafeCodejail()
7777
class TestRescoringTask(TestIntegrationTask):
7878
"""
7979
Integration-style tests for rescoring problems in a background task.

openedx/core/djangolib/tests/test_markup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class FormatHtmlTest(unittest.TestCase):
2626
("<a>нтмℓ-єѕ¢αρє∂</a>", "&lt;a&gt;нтмℓ-єѕ¢αρє∂&lt;/a&gt;"),
2727
)
2828
def test_simple(self, before_after):
29+
"""Verify that plain text is safely HTML-escaped."""
2930
(before, after) = before_after
3031
assert str(Text(_(before))) == after # pylint: disable=translation-of-non-string
3132
assert str(Text(before)) == after
3233

3334
def test_formatting(self):
35+
"""Ensure Text.format correctly mixes escaped text with raw HTML."""
3436
# The whole point of this function is to make sure this works:
3537
out = Text(_("Point & click {start}here{end}!")).format(
3638
start=HTML("<a href='http://edx.org'>"),
@@ -39,6 +41,7 @@ def test_formatting(self):
3941
assert str(out) == "Point &amp; click <a href='http://edx.org'>here</a>!"
4042

4143
def test_nested_formatting(self):
44+
"""Validate nested formatting where HTML contains formatted text."""
4245
# Sometimes, you have plain text, with html inserted, and the html has
4346
# plain text inserted. It gets twisty...
4447
out = Text(_("Send {start}email{end}")).format(
@@ -48,6 +51,7 @@ def test_nested_formatting(self):
4851
assert str(out) == "Send <a href='mailto:A&amp;B'>email</a>"
4952

5053
def test_mako(self):
54+
"""Confirm Mako templates format Text/HTML objects with expected filters."""
5155
# The default_filters used here have to match the ones in edxmako.
5256
template = Template(
5357
"""
@@ -64,6 +68,7 @@ def test_mako(self):
6468
assert out.strip() == "A &amp; B & C"
6569

6670
def test_ungettext(self):
71+
"""Check that ngettext output is properly formatted and HTML-escaped."""
6772
for i in [1, 2]:
6873
out = Text(ngettext("1 & {}", "2 & {}", i)).format(HTML("<>"))
6974
assert out == f"{i} &amp; <>"

openedx/core/lib/safe_lxml/xmlparser.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,46 @@ class RestrictedElement(_etree.ElementBase):
2222
__slots__ = ()
2323
blacklist = (_etree._Entity, _etree._ProcessingInstruction, _etree._Comment) # pylint: disable=protected-access
2424

25-
def _filter(self, iterator): # pylint: disable=missing-function-docstring
25+
def _filter(self, iterator):
26+
"""Yield only elements not in the blacklist from the given iterator."""
2627
blacklist = self.blacklist
2728
for child in iterator:
2829
if isinstance(child, blacklist):
2930
continue
3031
yield child
3132

3233
def __iter__(self):
33-
iterator = super(RestrictedElement, self).__iter__() # pylint: disable=super-with-arguments
34+
iterator = super().__iter__()
3435
return self._filter(iterator)
3536

3637
def iterchildren(self, tag=None, reversed=False): # pylint: disable=redefined-builtin
37-
iterator = super(RestrictedElement, self).iterchildren( # pylint: disable=super-with-arguments
38-
tag=tag, reversed=reversed
39-
)
38+
"""Iterate over child elements while excluding blacklisted nodes."""
39+
iterator = super().iterchildren(tag=tag, reversed=reversed)
4040
return self._filter(iterator)
4141

4242
def iter(self, tag=None, *tags): # pylint: disable=keyword-arg-before-vararg
43-
iterator = super(RestrictedElement, self).iter(tag=tag, *tags) # pylint: disable=super-with-arguments
43+
"""Iterate over the element tree excluding blacklisted nodes."""
44+
iterator = super().iter(tag=tag, *tags)
4445
return self._filter(iterator)
4546

4647
def iterdescendants(self, tag=None, *tags): # pylint: disable=keyword-arg-before-vararg
47-
iterator = super(RestrictedElement, self).iterdescendants( # pylint: disable=super-with-arguments
48-
tag=tag, *tags
49-
)
48+
"""Iterate over descendants while filtering out blacklisted nodes."""
49+
iterator = super().iterdescendants(tag=tag, *tags)
5050
return self._filter(iterator)
5151

5252
def itersiblings(self, tag=None, preceding=False):
53-
iterator = super(RestrictedElement, self).itersiblings( # pylint: disable=super-with-arguments
54-
tag=tag, preceding=preceding
55-
)
53+
"""Iterate over siblings excluding blacklisted node types."""
54+
iterator = super().itersiblings(tag=tag, preceding=preceding)
5655
return self._filter(iterator)
5756

5857
def getchildren(self):
59-
iterator = super(RestrictedElement, self).__iter__() # pylint: disable=super-with-arguments
58+
"""Return a list of non-blacklisted child elements."""
59+
iterator = super().__iter__()
6060
return list(self._filter(iterator))
6161

6262
def getiterator(self, tag=None):
63-
iterator = super(RestrictedElement, self).getiterator(tag) # pylint: disable=super-with-arguments
63+
"""Iterate over the tree with blacklisted nodes filtered out."""
64+
iterator = super().getiterator(tag)
6465
return self._filter(iterator)
6566

6667

@@ -73,27 +74,30 @@ class GlobalParserTLS(threading.local):
7374

7475
element_class = RestrictedElement
7576

76-
def createDefaultParser(self): # pylint: disable=missing-function-docstring
77+
def create_default_parser(self):
78+
"""Create a secure XMLParser using the restricted element class."""
7779
parser = _etree.XMLParser(**self.parser_config)
7880
element_class = self.element_class
7981
if self.element_class is not None:
8082
lookup = _etree.ElementDefaultClassLookup(element=element_class)
8183
parser.set_element_class_lookup(lookup)
8284
return parser
8385

84-
def setDefaultParser(self, parser):
86+
def set_default_parser(self, parser):
87+
"""Store a thread-local default XML parser instance."""
8588
self._default_parser = parser # pylint: disable=attribute-defined-outside-init
8689

87-
def getDefaultParser(self): # pylint: disable=missing-function-docstring
90+
def get_default_parser(self):
91+
"""Return the thread-local default parser, creating it if missing."""
8892
parser = getattr(self, "_default_parser", None)
8993
if parser is None:
90-
parser = self.createDefaultParser()
91-
self.setDefaultParser(parser)
94+
parser = self.create_default_parser()
95+
self.set_default_parser(parser)
9296
return parser
9397

9498

9599
_parser_tls = GlobalParserTLS()
96-
getDefaultParser = _parser_tls.getDefaultParser
100+
get_default_parser = _parser_tls.get_default_parser
97101

98102

99103
def check_docinfo(elementtree, forbid_dtd=False, forbid_entities=True):
@@ -107,9 +111,7 @@ def check_docinfo(elementtree, forbid_dtd=False, forbid_entities=True):
107111
raise DTDForbidden(docinfo.doctype, docinfo.system_url, docinfo.public_id)
108112
if forbid_entities and not LXML3:
109113
# lxml < 3 has no iterentities()
110-
raise NotSupportedError(
111-
"Unable to check for entity declarations in lxml 2.x"
112-
) # pylint: disable=implicit-str-concat
114+
raise NotSupportedError("Unable to check for entity declarations in lxml 2.x")
113115

114116
if forbid_entities:
115117
for dtd in docinfo.internalDTD, docinfo.externalDTD:
@@ -119,29 +121,28 @@ def check_docinfo(elementtree, forbid_dtd=False, forbid_entities=True):
119121
raise EntitiesForbidden(entity.name, entity.content, None, None, None, None)
120122

121123

122-
def parse(
123-
source, parser=None, base_url=None, forbid_dtd=False, forbid_entities=True
124-
): # pylint: disable=missing-function-docstring
124+
def parse(source, parser=None, base_url=None, forbid_dtd=False, forbid_entities=True):
125+
"""Securely parse XML from a source and enforce DTD/entity restrictions."""
125126
if parser is None:
126-
parser = getDefaultParser()
127+
parser = get_default_parser()
127128
elementtree = _etree.parse(source, parser, base_url=base_url)
128129
check_docinfo(elementtree, forbid_dtd, forbid_entities)
129130
return elementtree
130131

131132

132-
def fromstring(
133-
text, parser=None, base_url=None, forbid_dtd=False, forbid_entities=True
134-
): # pylint: disable=missing-function-docstring
133+
def fromstring(text, parser=None, base_url=None, forbid_dtd=False, forbid_entities=True):
134+
"""Securely parse XML from a string and validate docinfo."""
135135
if parser is None:
136-
parser = getDefaultParser()
136+
parser = get_default_parser()
137137
rootelement = _etree.fromstring(text, parser, base_url=base_url)
138138
elementtree = rootelement.getroottree()
139139
check_docinfo(elementtree, forbid_dtd, forbid_entities)
140140
return rootelement
141141

142142

143-
XML = fromstring
143+
XML = fromstring # pylint: disable=invalid-name
144144

145145

146146
def iterparse(*args, **kwargs):
147+
"""Disabled XML iterparse function that always raises NotSupportedError."""
147148
raise NotSupportedError("iterparse not available")

openedx/envs/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ def _make_locale_paths(settings):
16761676
# .. toggle_warning: Not production-ready until https://github.com/openedx/edx-platform/issues/34841 is done.
16771677
# .. toggle_creation_date: 2024-11-10
16781678
# .. toggle_target_removal_date: 2025-06-01
1679-
USE_EXTRACTED_ANNOTATABLE_BLOCK = True
1679+
USE_EXTRACTED_ANNOTATABLE_BLOCK = False
16801680

16811681
# .. toggle_name: USE_EXTRACTED_POLL_QUESTION_BLOCK
16821682
# .. toggle_default: False
@@ -1686,7 +1686,7 @@ def _make_locale_paths(settings):
16861686
# .. toggle_warning: Not production-ready until https://github.com/openedx/edx-platform/issues/34839 is done.
16871687
# .. toggle_creation_date: 2024-11-10
16881688
# .. toggle_target_removal_date: 2025-06-01
1689-
USE_EXTRACTED_POLL_QUESTION_BLOCK = True
1689+
USE_EXTRACTED_POLL_QUESTION_BLOCK = False
16901690

16911691
# .. toggle_name: USE_EXTRACTED_LTI_BLOCK
16921692
# .. toggle_default: False

0 commit comments

Comments
 (0)