From b1b3ba9c811f8a2be5ee49d519fb79812411df5f Mon Sep 17 00:00:00 2001 From: Usama Sadiq Date: Fri, 12 Mar 2021 12:16:46 +0500 Subject: [PATCH] refactor: ran pyugprade on lms/djangoapps/courseware (#26738) --- .../management/commands/clean_xml.py | 13 +++--- .../management/commands/dump_course_ids.py | 3 +- .../commands/dump_course_structure.py | 11 +++-- .../courseware/management/commands/import.py | 10 ++--- .../commands/tests/test_dump_course.py | 45 +++++++++---------- .../courseware/migrations/0001_initial.py | 15 +++---- ...ion_dynamicupgradedeadlineconfiguration.py | 3 -- .../migrations/0003_auto_20170825_0935.py | 3 -- .../migrations/0004_auto_20171010_1639.py | 3 -- ..._orgdynamicupgradedeadlineconfiguration.py | 3 -- .../migrations/0006_remove_module_id_index.py | 1 - .../migrations/0007_remove_done_index.py | 3 +- .../migrations/0008_move_idde_to_edx_when.py | 1 - .../migrations/0009_auto_20190703_1955.py | 1 - .../migrations/0010_auto_20190709_1559.py | 1 - .../migrations/0011_csm_id_bigint.py | 1 - .../migrations/0012_adjust_fields.py | 1 - .../migrations/0013_auto_20191001_1858.py | 1 - .../0014_fix_nan_value_for_global_speed.py | 1 - 19 files changed, 44 insertions(+), 76 deletions(-) diff --git a/lms/djangoapps/courseware/management/commands/clean_xml.py b/lms/djangoapps/courseware/management/commands/clean_xml.py index 83692f2490..2dd247de4d 100644 --- a/lms/djangoapps/courseware/management/commands/clean_xml.py +++ b/lms/djangoapps/courseware/management/commands/clean_xml.py @@ -11,7 +11,6 @@ import lxml.etree from django.core.management.base import BaseCommand from fs.osfs import OSFS from path import Path as path -from six.moves import map from xmodule.modulestore.xml import XMLModuleStore @@ -35,7 +34,7 @@ def export(course, export_dir): """ fs = OSFS(export_dir, create=True) if not fs.isdirempty('.'): # lint-amnesty, pylint: disable=no-member - print(u'WARNING: Directory {dir} not-empty. May clobber/confuse things'.format(dir=export_dir)) + print(f'WARNING: Directory {export_dir} not-empty. May clobber/confuse things') try: course.runtime.export_fs = fs @@ -55,7 +54,7 @@ def export(course, export_dir): def import_with_checks(course_dir): # lint-amnesty, pylint: disable=missing-function-docstring all_ok = True - print(u'Attempting to load "{}"'.format(course_dir)) + print(f'Attempting to load "{course_dir}"') course_dir = path(course_dir) data_dir = course_dir.dirname() @@ -71,13 +70,13 @@ def import_with_checks(course_dir): # lint-amnesty, pylint: disable=missing-fun def str_of_err(tpl): (msg, exc_str) = tpl - return '{msg}\n{exc}'.format(msg=msg, exc=exc_str) + return f'{msg}\n{exc_str}' courses = modulestore.get_courses() n = len(courses) if n != 1: - print(u'ERROR: Expect exactly 1 course. Loaded {n}: {lst}'.format(n=n, lst=courses)) + print(f'ERROR: Expect exactly 1 course. Loaded {n}: {courses}') return (False, None) course = courses[0] @@ -102,7 +101,7 @@ def import_with_checks(course_dir): # lint-amnesty, pylint: disable=missing-fun print('Running validators...') for validate in validators: - print(u'Running {}'.format(validate.__name__)) + print(f'Running {validate.__name__}') all_ok = validate(course) and all_ok if all_ok: @@ -130,7 +129,7 @@ def check_roundtrip(course_dir): # diff = dircmp(course_dir, export_dir, ignore=[], hide=[]) print('======== Roundtrip diff: =========') sys.stdout.flush() # needed to make diff appear in the right place - os.system(u'diff -r {} {}'.format(course_dir, export_dir)) + os.system(f'diff -r {course_dir} {export_dir}') print('======== ideally there is no diff above this =======') diff --git a/lms/djangoapps/courseware/management/commands/dump_course_ids.py b/lms/djangoapps/courseware/management/commands/dump_course_ids.py index 3b4c22c317..773579a083 100644 --- a/lms/djangoapps/courseware/management/commands/dump_course_ids.py +++ b/lms/djangoapps/courseware/management/commands/dump_course_ids.py @@ -8,7 +8,6 @@ Output is UTF-8 encoded by default. from textwrap import dedent from django.core.management.base import BaseCommand -from six import text_type from openedx.core.djangoapps.content.course_overviews.models import CourseOverview @@ -22,6 +21,6 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst help='name of the modulestore to use') def handle(self, *args, **options): - output = '\n'.join(text_type(course_overview.id) for course_overview in CourseOverview.get_all_courses()) + '\n' + output = '\n'.join(str(course_overview.id) for course_overview in CourseOverview.get_all_courses()) + '\n' return output diff --git a/lms/djangoapps/courseware/management/commands/dump_course_structure.py b/lms/djangoapps/courseware/management/commands/dump_course_structure.py index e04bd72546..e84f38e340 100644 --- a/lms/djangoapps/courseware/management/commands/dump_course_structure.py +++ b/lms/djangoapps/courseware/management/commands/dump_course_structure.py @@ -20,7 +20,6 @@ The resulting JSON object has one entry for each module in the course: import json from textwrap import dedent -import six from django.core.management.base import BaseCommand, CommandError from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import CourseKey @@ -76,7 +75,7 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst info = dump_module(course, inherited=options['inherited'], defaults=options['inherited_defaults']) - return json.dumps(info, indent=2, sort_keys=True, default=six.text_type) + return json.dumps(info, indent=2, sort_keys=True, default=str) def dump_module(module, destination=None, inherited=False, defaults=False): @@ -93,11 +92,11 @@ def dump_module(module, destination=None, inherited=False, defaults=False): if isinstance(module, DiscussionXBlock) and 'discussion_id' not in items: items['discussion_id'] = module.discussion_id - filtered_metadata = {k: v for k, v in six.iteritems(items) if k not in FILTER_LIST} + filtered_metadata = {k: v for k, v in items.items() if k not in FILTER_LIST} - destination[six.text_type(module.location)] = { + destination[str(module.location)] = { 'category': module.location.block_type, - 'children': [six.text_type(child) for child in getattr(module, 'children', [])], + 'children': [str(child) for child in getattr(module, 'children', [])], 'metadata': filtered_metadata, } @@ -118,7 +117,7 @@ def dump_module(module, destination=None, inherited=False, defaults=False): return field.values != field.default inherited_metadata = {field.name: field.read_json(module) for field in module.fields.values() if is_inherited(field)} # lint-amnesty, pylint: disable=line-too-long - destination[six.text_type(module.location)]['inherited_metadata'] = inherited_metadata + destination[str(module.location)]['inherited_metadata'] = inherited_metadata for child in module.get_children(): dump_module(child, destination, inherited, defaults) diff --git a/lms/djangoapps/courseware/management/commands/import.py b/lms/djangoapps/courseware/management/commands/import.py index e52bc90dea..fc50bc264b 100644 --- a/lms/djangoapps/courseware/management/commands/import.py +++ b/lms/djangoapps/courseware/management/commands/import.py @@ -50,10 +50,10 @@ class Command(BaseCommand): python_lib_filename = options.get('python_lib_filename') output = ( - u"Importing...\n" - u" data_dir={data}, source_dirs={courses}\n" - u" Importing static content? {import_static}\n" - u" Importing python lib? {import_python_lib}" + "Importing...\n" + " data_dir={data}, source_dirs={courses}\n" + " Importing static content? {import_static}\n" + " Importing python lib? {import_python_lib}" ).format( data=data_dir, courses=source_dirs, @@ -74,5 +74,5 @@ class Command(BaseCommand): for course in course_items: course_id = course.id if not are_permissions_roles_seeded(course_id): - self.stdout.write(u'Seeding forum roles for course {0}\n'.format(course_id)) + self.stdout.write(f'Seeding forum roles for course {course_id}\n') seed_permissions_roles(course_id) diff --git a/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py b/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py index 4e02eeba46..92c0124156 100644 --- a/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py +++ b/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py @@ -1,18 +1,14 @@ -# coding=utf-8 - """ Tests for Django management commands """ import json -from six import StringIO +from io import StringIO import factory -import six from django.conf import settings from django.core.management import call_command -from six import text_type from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.django import modulestore @@ -42,7 +38,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): @classmethod def setUpClass(cls): - super(CommandsTestBase, cls).setUpClass() + super().setUpClass() cls.test_course_key = modulestore().make_course_key("edX", "simple", "2012_Fall") cls.loaded_courses = cls.load_courses() @@ -59,7 +55,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): org=unique_org, course='simple', run="run", - display_name=u'2012_Fáĺĺ', + display_name='2012_Fáĺĺ', modulestore=store ) @@ -88,12 +84,12 @@ class CommandsTestBase(SharedModuleStoreTestCase): def test_dump_course_ids(self): output = self.call_command('dump_course_ids') dumped_courses = output.strip().split('\n') - course_ids = {text_type(course_id) for course_id in self.loaded_courses} + course_ids = {str(course_id) for course_id in self.loaded_courses} dumped_ids = set(dumped_courses) assert course_ids == dumped_ids def test_correct_course_structure_metadata(self): - course_id = text_type(self.test_course_key) + course_id = str(self.test_course_key) args = [course_id] kwargs = {'modulestore': 'default'} @@ -106,7 +102,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): assert len(list(dump.values())) > 0 def test_dump_course_structure(self): - args = [text_type(self.test_course_key)] + args = [str(self.test_course_key)] kwargs = {'modulestore': 'default'} output = self.call_command('dump_course_structure', *args, **kwargs) @@ -114,7 +110,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): # check that all elements in the course structure have metadata, # but not inherited metadata: - for element in six.itervalues(dump): + for element in dump.values(): assert 'metadata' in element assert 'children' in element assert 'category' in element @@ -122,7 +118,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): # Check a few elements in the course dump test_course_key = self.test_course_key - parent_id = text_type(test_course_key.make_usage_key('chapter', 'Overview')) + parent_id = str(test_course_key.make_usage_key('chapter', 'Overview')) assert dump[parent_id]['category'] == 'chapter' assert len(dump[parent_id]['children']) == 3 @@ -130,12 +126,11 @@ class CommandsTestBase(SharedModuleStoreTestCase): assert dump[child_id]['category'] == 'videosequence' assert len(dump[child_id]['children']) == 2 - video_id = text_type(test_course_key.make_usage_key('video', 'Welcome')) + video_id = str(test_course_key.make_usage_key('video', 'Welcome')) assert dump[video_id]['category'] == 'video' video_metadata = dump[video_id]['metadata'] video_metadata.pop('edx_video_id', None) - six.assertCountEqual( - self, + self.assertCountEqual( list(video_metadata.keys()), ['youtube_id_0_75', 'youtube_id_1_0', 'youtube_id_1_25', 'youtube_id_1_5'] ) @@ -146,13 +141,13 @@ class CommandsTestBase(SharedModuleStoreTestCase): assert len(dump) == 17 def test_dump_inherited_course_structure(self): - args = [text_type(self.test_course_key)] + args = [str(self.test_course_key)] kwargs = {'modulestore': 'default', 'inherited': True} output = self.call_command('dump_course_structure', *args, **kwargs) dump = json.loads(output) # check that all elements in the course structure have inherited metadata, # and that it contains a particular value as well: - for element in six.itervalues(dump): + for element in dump.values(): assert 'metadata' in element assert 'children' in element assert 'category' in element @@ -161,13 +156,13 @@ class CommandsTestBase(SharedModuleStoreTestCase): assert 'due' not in element['inherited_metadata'] def test_dump_inherited_course_structure_with_defaults(self): - args = [text_type(self.test_course_key)] + args = [str(self.test_course_key)] kwargs = {'modulestore': 'default', 'inherited': True, 'inherited_defaults': True} output = self.call_command('dump_course_structure', *args, **kwargs) dump = json.loads(output) # check that all elements in the course structure have inherited metadata, # and that it contains a particular value as well: - for element in six.itervalues(dump): + for element in dump.values(): assert 'metadata' in element assert 'children' in element assert 'category' in element @@ -176,16 +171,16 @@ class CommandsTestBase(SharedModuleStoreTestCase): assert element['inherited_metadata']['due'] is None def test_export_discussion_ids(self): - output = self.call_command('dump_course_structure', text_type(self.course.id)) + output = self.call_command('dump_course_structure', str(self.course.id)) dump = json.loads(output) - dumped_id = dump[text_type(self.discussion.location)]['metadata']['discussion_id'] + dumped_id = dump[str(self.discussion.location)]['metadata']['discussion_id'] assert dumped_id == self.discussion.discussion_id def test_export_discussion_id_custom_id(self): - output = self.call_command('dump_course_structure', text_type(self.test_course_key)) + output = self.call_command('dump_course_structure', str(self.test_course_key)) dump = json.loads(output) - discussion_key = text_type(self.test_course_key.make_usage_key('discussion', 'custom_id')) - dumped_id = dump[text_type(discussion_key)]['metadata']['discussion_id'] + discussion_key = str(self.test_course_key.make_usage_key('discussion', 'custom_id')) + dumped_id = dump[str(discussion_key)]['metadata']['discussion_id'] assert dumped_id == 'custom' def check_export_file(self, tar_file): # pylint: disable=missing-function-docstring @@ -198,7 +193,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): assert_in = self.assertIn assert_in('edX-simple-2012_Fall', names) - assert_in('edX-simple-2012_Fall/policies/{}/policy.json'.format(self.url_name), names) + assert_in(f'edX-simple-2012_Fall/policies/{self.url_name}/policy.json', names) assert_in('edX-simple-2012_Fall/html/toylab.html', names) assert_in('edX-simple-2012_Fall/videosequence/A_simple_sequence.xml', names) assert_in('edX-simple-2012_Fall/sequential/Lecture_2.xml', names) diff --git a/lms/djangoapps/courseware/migrations/0001_initial.py b/lms/djangoapps/courseware/migrations/0001_initial.py index 7820e27894..56fed9178c 100644 --- a/lms/djangoapps/courseware/migrations/0001_initial.py +++ b/lms/djangoapps/courseware/migrations/0001_initial.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - import django.utils.timezone import model_utils.fields from django.conf import settings @@ -120,26 +117,26 @@ class Migration(migrations.Migration): ), migrations.AlterUniqueTogether( name='xmoduleuserstatesummaryfield', - unique_together=set([('usage_id', 'field_name')]), + unique_together={('usage_id', 'field_name')}, ), migrations.AlterUniqueTogether( name='xmodulestudentprefsfield', - unique_together=set([('student', 'module_type', 'field_name')]), + unique_together={('student', 'module_type', 'field_name')}, ), migrations.AlterUniqueTogether( name='xmodulestudentinfofield', - unique_together=set([('student', 'field_name')]), + unique_together={('student', 'field_name')}, ), migrations.AlterUniqueTogether( name='studentmodule', - unique_together=set([('student', 'module_state_key', 'course_id')]), + unique_together={('student', 'module_state_key', 'course_id')}, ), migrations.AlterUniqueTogether( name='studentfieldoverride', - unique_together=set([('course_id', 'field', 'location', 'student')]), + unique_together={('course_id', 'field', 'location', 'student')}, ), migrations.AlterUniqueTogether( name='offlinecomputedgrade', - unique_together=set([('user', 'course_id')]), + unique_together={('user', 'course_id')}, ), ] diff --git a/lms/djangoapps/courseware/migrations/0002_coursedynamicupgradedeadlineconfiguration_dynamicupgradedeadlineconfiguration.py b/lms/djangoapps/courseware/migrations/0002_coursedynamicupgradedeadlineconfiguration_dynamicupgradedeadlineconfiguration.py index ad7264d731..319b810e87 100644 --- a/lms/djangoapps/courseware/migrations/0002_coursedynamicupgradedeadlineconfiguration_dynamicupgradedeadlineconfiguration.py +++ b/lms/djangoapps/courseware/migrations/0002_coursedynamicupgradedeadlineconfiguration_dynamicupgradedeadlineconfiguration.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - import django.db.models.deletion from django.conf import settings from django.db import migrations, models diff --git a/lms/djangoapps/courseware/migrations/0003_auto_20170825_0935.py b/lms/djangoapps/courseware/migrations/0003_auto_20170825_0935.py index 47ee7c0b9c..7fdc511484 100644 --- a/lms/djangoapps/courseware/migrations/0003_auto_20170825_0935.py +++ b/lms/djangoapps/courseware/migrations/0003_auto_20170825_0935.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - from django.db import migrations, models diff --git a/lms/djangoapps/courseware/migrations/0004_auto_20171010_1639.py b/lms/djangoapps/courseware/migrations/0004_auto_20171010_1639.py index 6a561c557d..4669676b67 100644 --- a/lms/djangoapps/courseware/migrations/0004_auto_20171010_1639.py +++ b/lms/djangoapps/courseware/migrations/0004_auto_20171010_1639.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - from django.db import migrations, models diff --git a/lms/djangoapps/courseware/migrations/0005_orgdynamicupgradedeadlineconfiguration.py b/lms/djangoapps/courseware/migrations/0005_orgdynamicupgradedeadlineconfiguration.py index c8d7ee4552..e887c38d55 100644 --- a/lms/djangoapps/courseware/migrations/0005_orgdynamicupgradedeadlineconfiguration.py +++ b/lms/djangoapps/courseware/migrations/0005_orgdynamicupgradedeadlineconfiguration.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - import django.db.models.deletion from django.conf import settings from django.db import migrations, models diff --git a/lms/djangoapps/courseware/migrations/0006_remove_module_id_index.py b/lms/djangoapps/courseware/migrations/0006_remove_module_id_index.py index 2877bc6491..7f2d23f0db 100644 --- a/lms/djangoapps/courseware/migrations/0006_remove_module_id_index.py +++ b/lms/djangoapps/courseware/migrations/0006_remove_module_id_index.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.13 on 2018-05-27 16:27 diff --git a/lms/djangoapps/courseware/migrations/0007_remove_done_index.py b/lms/djangoapps/courseware/migrations/0007_remove_done_index.py index 26f6e3182d..5bedbb773f 100644 --- a/lms/djangoapps/courseware/migrations/0007_remove_done_index.py +++ b/lms/djangoapps/courseware/migrations/0007_remove_done_index.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.13 on 2018-05-29 15:18 @@ -15,6 +14,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='studentmodule', name='done', - field=models.CharField(choices=[(u'na', u'NOT_APPLICABLE'), (u'f', u'FINISHED'), (u'i', u'INCOMPLETE')], default=u'na', max_length=8), + field=models.CharField(choices=[('na', 'NOT_APPLICABLE'), ('f', 'FINISHED'), ('i', 'INCOMPLETE')], default='na', max_length=8), ), ] diff --git a/lms/djangoapps/courseware/migrations/0008_move_idde_to_edx_when.py b/lms/djangoapps/courseware/migrations/0008_move_idde_to_edx_when.py index 77d9cbf0e4..632409025c 100644 --- a/lms/djangoapps/courseware/migrations/0008_move_idde_to_edx_when.py +++ b/lms/djangoapps/courseware/migrations/0008_move_idde_to_edx_when.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.20 on 2019-03-21 15:25 diff --git a/lms/djangoapps/courseware/migrations/0009_auto_20190703_1955.py b/lms/djangoapps/courseware/migrations/0009_auto_20190703_1955.py index 3521009b1c..ad10ef3544 100644 --- a/lms/djangoapps/courseware/migrations/0009_auto_20190703_1955.py +++ b/lms/djangoapps/courseware/migrations/0009_auto_20190703_1955.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.22 on 2019-07-03 19:55 diff --git a/lms/djangoapps/courseware/migrations/0010_auto_20190709_1559.py b/lms/djangoapps/courseware/migrations/0010_auto_20190709_1559.py index 935f787b8d..477ef7dd64 100644 --- a/lms/djangoapps/courseware/migrations/0010_auto_20190709_1559.py +++ b/lms/djangoapps/courseware/migrations/0010_auto_20190709_1559.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.22 on 2019-07-09 15:59 diff --git a/lms/djangoapps/courseware/migrations/0011_csm_id_bigint.py b/lms/djangoapps/courseware/migrations/0011_csm_id_bigint.py index c03696d239..abcebcf0b4 100644 --- a/lms/djangoapps/courseware/migrations/0011_csm_id_bigint.py +++ b/lms/djangoapps/courseware/migrations/0011_csm_id_bigint.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.23 on 2019-08-28 15:50 diff --git a/lms/djangoapps/courseware/migrations/0012_adjust_fields.py b/lms/djangoapps/courseware/migrations/0012_adjust_fields.py index 51abea36a7..9abefa6f5c 100644 --- a/lms/djangoapps/courseware/migrations/0012_adjust_fields.py +++ b/lms/djangoapps/courseware/migrations/0012_adjust_fields.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.23 on 2019-09-08 04:54 # # This migration does not produce any actual database changes; it only affects diff --git a/lms/djangoapps/courseware/migrations/0013_auto_20191001_1858.py b/lms/djangoapps/courseware/migrations/0013_auto_20191001_1858.py index a9062b020c..d365267374 100644 --- a/lms/djangoapps/courseware/migrations/0013_auto_20191001_1858.py +++ b/lms/djangoapps/courseware/migrations/0013_auto_20191001_1858.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.25 on 2019-10-01 18:58 diff --git a/lms/djangoapps/courseware/migrations/0014_fix_nan_value_for_global_speed.py b/lms/djangoapps/courseware/migrations/0014_fix_nan_value_for_global_speed.py index 0d7beeb9bd..39028887f4 100644 --- a/lms/djangoapps/courseware/migrations/0014_fix_nan_value_for_global_speed.py +++ b/lms/djangoapps/courseware/migrations/0014_fix_nan_value_for_global_speed.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.29 on 2020-03-11 10:07