diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index cb31fbe5c6..9125451352 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -1560,7 +1560,7 @@ class AnnotationInput(InputTypeBase): def _unpack(self, json_value): """ Unpacks the json input state into a dict. """ d = json.loads(json_value) - if type(d) != dict: + if not isinstance(d, dict): d = {} comment_value = d.get('comment', '') diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 9678af75cb..ec23d4ba9c 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -2505,7 +2505,7 @@ class FormulaResponse(LoncapaResponse): converted to float. Used so we can safely use Python contexts. """ inp_d = dict([(k, numpy.complex(inp_d[k])) - for k in inp_d if type(k) == str and + for k in inp_d if isinstance(k, str) and k.isalnum() and isinstance(inp_d[k], numbers.Number)]) return inp_d @@ -2683,7 +2683,7 @@ class ImageResponse(LoncapaResponse): if correct_map[aid]['correctness'] != 'correct' and regions[aid]: parsed_region = json.loads(regions[aid]) if parsed_region: - if type(parsed_region[0][0]) != list: + if not isinstance(parsed_region[0][0], list): # we have [[1,2],[3,4],[5,6]] - single region # instead of [[[1,2],[3,4],[5,6], [[1,2],[3,4],[5,6]]] # or [[[1,2],[3,4],[5,6]]] - multiple regions syntax @@ -2833,7 +2833,7 @@ class AnnotationResponse(LoncapaResponse): def _unpack(self, json_value): """Unpacks a student response value submitted as JSON.""" json_d = json.loads(json_value) - if type(json_d) != dict: + if not isinstance(json_d, dict): json_d = {} comment_value = json_d.get('comment', '') diff --git a/common/lib/capa/capa/tests/response_xml_factory.py b/common/lib/capa/capa/tests/response_xml_factory.py index ba4af2bbe8..3ddccbb0d5 100644 --- a/common/lib/capa/capa/tests/response_xml_factory.py +++ b/common/lib/capa/capa/tests/response_xml_factory.py @@ -823,7 +823,7 @@ class ChoiceTextResponseXMLFactory(ResponseXMLFactory): choice_inputs = [] # Ensure that the first element of choices is an ordered # collection. It will start as a list, a tuple, or not a Container. - if type(choices[0]) not in [list, tuple]: + if not isinstance(choices[0], (list, tuple)): choices = [choices] for choice in choices: @@ -838,7 +838,7 @@ class ChoiceTextResponseXMLFactory(ResponseXMLFactory): # Make sure that `answers` is an ordered collection for # convenience. - if type(answers) not in [list, tuple]: + if not isinstance(answers, (list, tuple)): answers = [answers] numtolerance_inputs = [ diff --git a/common/lib/chem/chem/chemcalc.py b/common/lib/chem/chem/chemcalc.py index 119b558fe4..b8424e96dc 100644 --- a/common/lib/chem/chem/chemcalc.py +++ b/common/lib/chem/chem/chemcalc.py @@ -91,7 +91,7 @@ def _clean_parse_tree(tree): 'paren_group_square': lambda x: nltk.tree.Tree(x.node, x[1]), 'paren_group_round': lambda x: nltk.tree.Tree(x.node, x[1])} - if type(tree) == str: + if isinstance(tree, str): return tree old_node = None @@ -124,7 +124,7 @@ def _merge_children(tree, tags): # Haven't grokked the code to tell if this is indeed the right thing to do. raise ParseException("Shouldn't have empty trees") - if type(tree) == str: + if isinstance(tree, str): return tree merged_children = [] @@ -134,7 +134,7 @@ def _merge_children(tree, tags): while not done: done = True for child in tree: - if type(child) == nltk.tree.Tree and child.node == tree.node and tree.node in tags: + if isinstance(child, nltk.tree.Tree) and child.node == tree.node and tree.node in tags: merged_children = merged_children + list(child) done = False else: @@ -182,7 +182,7 @@ def _render_to_html(tree): 'paren_group_round': round_brackets, 'paren_group_square': square_brackets} - if type(tree) == str: + if isinstance(tree, str): return tree else: children = "".join(map(_render_to_html, tree)) diff --git a/common/lib/symmath/symmath/formula.py b/common/lib/symmath/symmath/formula.py index 0c6aa05a24..c9166233a1 100644 --- a/common/lib/symmath/symmath/formula.py +++ b/common/lib/symmath/symmath/formula.py @@ -96,7 +96,7 @@ def my_evalf(expr, chop=False): Enhanced sympy evalf to handle lists of expressions and catch eval failures without dropping out. """ - if type(expr) == list: + if isinstance(expr, list): try: return [x.evalf(chop=chop) for x in expr] except: @@ -140,7 +140,7 @@ def my_sympify(expr, normphase=False, matrix=False, abcsym=False, do_qubit=False sexpr = sympify(expr, locals=varset) if normphase: # remove overall phase if sexpr is a list - if type(sexpr) == list: + if isinstance(sexpr, list): if sexpr[0].is_number: ophase = sympy.sympify('exp(-I*arg(%s))' % sexpr[0]) sexpr = [sympy.Mul(x, ophase) for x in sexpr] @@ -150,10 +150,10 @@ def my_sympify(expr, normphase=False, matrix=False, abcsym=False, do_qubit=False Convert a list, or list of lists to a matrix. """ # if expr is a list of lists, and is rectangular, then return Matrix(expr) - if not type(expr) == list: + if not isinstance(expr, list): return expr for row in expr: - if (not type(row) == list): + if not isinstance(row, list): return expr rdim = len(expr[0]) for row in expr: @@ -230,7 +230,7 @@ class formula(object): it, if possible... """ - if type(xml) == str or type(xml) == unicode: + if isinstance(xml, (str, unicode)): xml = etree.fromstring(xml) # TODO: wrap in try xml = self.fix_greek_in_mathml(xml) # convert greek utf letters to greek spelled out in ascii diff --git a/common/lib/symmath/symmath/symmath_check.py b/common/lib/symmath/symmath/symmath_check.py index e38da60b72..d39afb35fb 100644 --- a/common/lib/symmath/symmath/symmath_check.py +++ b/common/lib/symmath/symmath/symmath_check.py @@ -287,7 +287,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None if fexpect == fsym: return {'ok': True, 'msg': msg} - if type(fexpect) == list: + if isinstance(fexpect, list): try: xgiven = my_evalf(fsym, chop=True) dm = my_evalf(sympy.Matrix(fexpect) - sympy.Matrix(xgiven), chop=True) diff --git a/common/lib/xmodule/xmodule/lti_2_util.py b/common/lib/xmodule/xmodule/lti_2_util.py index 2067ee09d8..da52ed4392 100644 --- a/common/lib/xmodule/xmodule/lti_2_util.py +++ b/common/lib/xmodule/xmodule/lti_2_util.py @@ -321,8 +321,8 @@ class LTI20ModuleMixin(object): # the standard supports a list of objects, who knows why. It must contain at least 1 element, and the # first element must be a dict - if type(json_obj) != dict: - if type(json_obj) == list and len(json_obj) >= 1 and type(json_obj[0]) == dict: + if not isinstance(json_obj, dict): + if isinstance(json_obj, list) and len(json_obj) >= 1 and isinstance(json_obj[0], dict): json_obj = json_obj[0] else: msg = ("Supplied JSON string is a list that does not contain an object as the first element. {}" diff --git a/common/lib/xmodule/xmodule/tabs.py b/common/lib/xmodule/xmodule/tabs.py index 9020f058f0..d4b986d3b6 100644 --- a/common/lib/xmodule/xmodule/tabs.py +++ b/common/lib/xmodule/xmodule/tabs.py @@ -126,7 +126,7 @@ class CourseTab(object): was implemented). """ - if type(other) is dict and not self.validate(other, raise_error=False): + if isinstance(other, dict) and not self.validate(other, raise_error=False): # 'other' is a dict-type tab and did not validate return False diff --git a/lms/djangoapps/foldit/tests.py b/lms/djangoapps/foldit/tests.py index f8c0f75e09..2ff6da98f5 100644 --- a/lms/djangoapps/foldit/tests.py +++ b/lms/djangoapps/foldit/tests.py @@ -51,9 +51,9 @@ class FolditTestCase(TestCase): Given lists of puzzle_ids and best_scores (must have same length), make a SetPlayerPuzzleScores request and return the response. """ - if not(type(best_scores) == list): + if not isinstance(best_scores, list): best_scores = [best_scores] - if not(type(puzzle_ids) == list): + if not isinstance(puzzle_ids, list): puzzle_ids = [puzzle_ids] user = self.user if not user else user diff --git a/lms/djangoapps/lms_migration/migrate.py b/lms/djangoapps/lms_migration/migrate.py index eb33a14773..889a077702 100644 --- a/lms/djangoapps/lms_migration/migrate.py +++ b/lms/djangoapps/lms_migration/migrate.py @@ -139,7 +139,7 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): for field in dumpfields: data = getattr(course, field, None) html += '