From bddd44642dceefd083978c8f115f8284120a151b Mon Sep 17 00:00:00 2001 From: Maxim Zhiltsov Date: Tue, 5 Jul 2022 16:20:00 +0300 Subject: [PATCH] Fix pylint issues (#100) --- cvat/apps/dataset_manager/annotation.py | 4 +- cvat/apps/dataset_manager/bindings.py | 48 ++++++++++++------- cvat/apps/dataset_manager/formats/imagenet.py | 2 +- .../dataset_manager/formats/market1501.py | 2 +- cvat/apps/dataset_manager/formats/mot.py | 4 +- cvat/apps/dataset_manager/formats/mots.py | 2 +- .../dataset_manager/formats/velodynepoint.py | 11 ++--- cvat/apps/dataset_manager/util.py | 2 +- cvat/apps/dataset_repo/dataset_repo.py | 4 +- cvat/apps/dataset_repo/management/__init__.py | 2 +- .../management/commands/__init__.py | 2 +- cvat/apps/engine/media_extractors.py | 28 +++++------ cvat/apps/engine/parsers.py | 2 +- cvat/apps/engine/renderers.py | 2 +- cvat/apps/iam/__init__.py | 2 +- cvat/apps/iam/admin.py | 2 +- cvat/apps/iam/schema.py | 2 +- cvat/apps/iam/urls.py | 2 +- cvat/apps/iam/views.py | 10 ++-- cvat/apps/lambda_manager/views.py | 2 +- cvat/apps/organizations/views.py | 2 +- cvat/apps/profiler.py | 4 +- tests/rest_api/fixtures/data.py | 6 +-- tests/rest_api/test_invitations.py | 4 +- utils/dataset_manifest/__init__.py | 2 +- utils/dataset_manifest/core.py | 2 +- utils/dataset_manifest/create.py | 2 +- 27 files changed, 83 insertions(+), 74 deletions(-) diff --git a/cvat/apps/dataset_manager/annotation.py b/cvat/apps/dataset_manager/annotation.py index 5bb152ea..3b67c10c 100644 --- a/cvat/apps/dataset_manager/annotation.py +++ b/cvat/apps/dataset_manager/annotation.py @@ -70,8 +70,8 @@ class AnnotationIR: prev_shape = None for shape in track['shapes']: if prev_shape and not prev_shape['outside'] and \ - has_overlap(prev_shape['frame'], shape['frame']): - return True + has_overlap(prev_shape['frame'], shape['frame']): + return True prev_shape = shape if not prev_shape['outside'] and prev_shape['frame'] <= stop: diff --git a/cvat/apps/dataset_manager/bindings.py b/cvat/apps/dataset_manager/bindings.py index 3f673604..7739a405 100644 --- a/cvat/apps/dataset_manager/bindings.py +++ b/cvat/apps/dataset_manager/bindings.py @@ -1434,7 +1434,8 @@ def import_dm_annotations(dm_dataset: Dataset, instance_data: Union[TaskData, Pr if isinstance(instance_data, ProjectData): for sub_dataset, task_data in instance_data.split_dataset(dm_dataset): - # FIXME: temporary workaround for cvat format, will be removed after migration importer to datumaro + # FIXME: temporary workaround for cvat format + # will be removed after migration importer to datumaro sub_dataset._format = dm_dataset.format import_dm_annotations(sub_dataset, task_data) return @@ -1477,6 +1478,12 @@ def import_dm_annotations(dm_dataset: Dataset, instance_data: Union[TaskData, Pr try: if hasattr(ann, 'label') and ann.label is None: raise CvatImportError("annotation has no label") + + attributes = [ + instance_data.Attribute(name=n, value=str(v)) + for n, v in ann.attributes.items() + ] + if ann.type in shapes: if ann.type == datum_annotation.AnnotationType.cuboid_3d: try: @@ -1485,44 +1492,50 @@ def import_dm_annotations(dm_dataset: Dataset, instance_data: Union[TaskData, Pr ann.points = ann.points ann.z_order = 0 + # Use safe casting to bool instead of plain reading + # because in some formats return type can be different + # from bool / None + # https://github.com/openvinotoolkit/datumaro/issues/719 + occluded = cast(ann.attributes.pop('occluded', None), bool) is True + keyframe = cast(ann.attributes.get('keyframe', None), bool) is True + outside = cast(ann.attributes.pop('outside', None), bool) is True + track_id = ann.attributes.pop('track_id', None) + source = ann.attributes.pop('source').lower() \ + if ann.attributes.get('source', '').lower() in {'auto', 'manual'} else 'manual' + if track_id is None or dm_dataset.format != 'cvat' : instance_data.add_shape(instance_data.LabeledShape( type=shapes[ann.type], frame=frame_number, points=ann.points, label=label_cat.items[ann.label].name, - occluded=ann.attributes.pop('occluded', None) == True, + occluded=occluded, z_order=ann.z_order, group=group_map.get(ann.group, 0), - source=str(ann.attributes.pop('source')).lower() \ - if str(ann.attributes.get('source', None)).lower() in {'auto', 'manual'} else 'manual', - attributes=[instance_data.Attribute(name=n, value=str(v)) - for n, v in ann.attributes.items()], + source=source, + attributes=attributes, )) continue - if ann.attributes.get('keyframe', None) == True or ann.attributes.get('outside', None) == True: + if keyframe or outside: track = instance_data.TrackedShape( type=shapes[ann.type], frame=frame_number, - occluded=ann.attributes.pop('occluded', None) == True, - outside=ann.attributes.pop('outside', None) == True, - keyframe=ann.attributes.get('keyframe', None) == True, + occluded=occluded, + outside=outside, + keyframe=keyframe, points=ann.points, z_order=ann.z_order, - source=str(ann.attributes.pop('source')).lower() \ - if str(ann.attributes.get('source', None)).lower() in {'auto', 'manual'} else 'manual', - attributes=[instance_data.Attribute(name=n, value=str(v)) - for n, v in ann.attributes.items()], + source=source, + attributes=attributes, ) if track_id not in tracks: tracks[track_id] = instance_data.Track( label=label_cat.items[ann.label].name, group=group_map.get(ann.group, 0), - source=str(ann.attributes.pop('source')).lower() \ - if str(ann.attributes.get('source', None)).lower() in {'auto', 'manual'} else 'manual', + source=source, shapes=[], ) @@ -1534,8 +1547,7 @@ def import_dm_annotations(dm_dataset: Dataset, instance_data: Union[TaskData, Pr label=label_cat.items[ann.label].name, group=group_map.get(ann.group, 0), source='manual', - attributes=[instance_data.Attribute(name=n, value=str(v)) - for n, v in ann.attributes.items()], + attributes=attributes, )) except Exception as e: raise CvatImportError("Image {}: can't import annotation " diff --git a/cvat/apps/dataset_manager/formats/imagenet.py b/cvat/apps/dataset_manager/formats/imagenet.py index a84f487f..7556f456 100644 --- a/cvat/apps/dataset_manager/formats/imagenet.py +++ b/cvat/apps/dataset_manager/formats/imagenet.py @@ -38,4 +38,4 @@ def _import(src_file, instance_data, load_data_callback=None): dataset = Dataset.import_from(tmp_dir, 'imagenet', env=dm_env) if load_data_callback is not None: load_data_callback(dataset, instance_data) - import_dm_annotations(dataset, instance_data) \ No newline at end of file + import_dm_annotations(dataset, instance_data) diff --git a/cvat/apps/dataset_manager/formats/market1501.py b/cvat/apps/dataset_manager/formats/market1501.py index 272f7f15..0fe07585 100644 --- a/cvat/apps/dataset_manager/formats/market1501.py +++ b/cvat/apps/dataset_manager/formats/market1501.py @@ -50,7 +50,7 @@ class LabelAttrToAttr(ItemTransform): def transform_item(self, item): annotations = list(item.annotations) attributes = dict(item.attributes) - if self._label != None: + if self._label is not None: labels = [ann for ann in annotations if ann.type == AnnotationType.label \ and ann.label == self._label] diff --git a/cvat/apps/dataset_manager/formats/mot.py b/cvat/apps/dataset_manager/formats/mot.py index 3f0dfd3f..78a85d97 100644 --- a/cvat/apps/dataset_manager/formats/mot.py +++ b/cvat/apps/dataset_manager/formats/mot.py @@ -32,7 +32,7 @@ def _import_task(dataset, task_data): type='rectangle', label=label_cat.items[ann.label].name, points=ann.points, - occluded=ann.attributes.get('occluded') == True, + occluded=ann.attributes.get('occluded') is True, z_order=ann.z_order, group=0, frame=frame_number, @@ -44,7 +44,7 @@ def _import_task(dataset, task_data): shape = task_data.TrackedShape( type='rectangle', points=ann.points, - occluded=ann.attributes.get('occluded') == True, + occluded=ann.attributes.get('occluded') is True, outside=False, keyframe=True, z_order=ann.z_order, diff --git a/cvat/apps/dataset_manager/formats/mots.py b/cvat/apps/dataset_manager/formats/mots.py index d26e2237..d0b11d5a 100644 --- a/cvat/apps/dataset_manager/formats/mots.py +++ b/cvat/apps/dataset_manager/formats/mots.py @@ -52,7 +52,7 @@ def _import_task(dataset, task_data): shape = task_data.TrackedShape( type='polygon', points=ann.points, - occluded=ann.attributes.get('occluded') == True, + occluded=ann.attributes.get('occluded') is True, outside=False, keyframe=True, z_order=ann.z_order, diff --git a/cvat/apps/dataset_manager/formats/velodynepoint.py b/cvat/apps/dataset_manager/formats/velodynepoint.py index 9a7598c5..33266bd3 100644 --- a/cvat/apps/dataset_manager/formats/velodynepoint.py +++ b/cvat/apps/dataset_manager/formats/velodynepoint.py @@ -19,7 +19,6 @@ from .registry import exporter, importer @exporter(name='Kitti Raw Format', ext='ZIP', version='1.0', dimension=DimensionType.DIM_3D) def _export_images(dst_file, task_data, save_images=False): - dataset = Dataset.from_extractors(GetCVATDataExtractor( task_data, include_images=save_images, format_type="kitti_raw", dimension=DimensionType.DIM_3D), env=dm_env) @@ -33,14 +32,10 @@ def _export_images(dst_file, task_data, save_images=False): def _import(src_file, instance_data, load_data_callback=None): with TemporaryDirectory() as tmp_dir: if zipfile.is_zipfile(src_file): - zipfile.ZipFile(src_file).extractall(tmp_dir) - - dataset = Dataset.import_from( - tmp_dir, 'kitti_raw', env=dm_env) + zipfile.ZipFile(src_file).extractall(tmp_dir) + dataset = Dataset.import_from(tmp_dir, 'kitti_raw', env=dm_env) else: - - dataset = Dataset.import_from( - src_file.name, 'kitti_raw', env=dm_env) + dataset = Dataset.import_from(src_file.name, 'kitti_raw', env=dm_env) if load_data_callback is not None: load_data_callback(dataset, instance_data) import_dm_annotations(dataset, instance_data) diff --git a/cvat/apps/dataset_manager/util.py b/cvat/apps/dataset_manager/util.py index cd11ec2d..a2a93bac 100644 --- a/cvat/apps/dataset_manager/util.py +++ b/cvat/apps/dataset_manager/util.py @@ -34,4 +34,4 @@ def bulk_create(db_model, objects, flt_param): else: return db_model.objects.bulk_create(objects) - return [] \ No newline at end of file + return [] diff --git a/cvat/apps/dataset_repo/dataset_repo.py b/cvat/apps/dataset_repo/dataset_repo.py index 7b1d5b56..48fa4377 100644 --- a/cvat/apps/dataset_repo/dataset_repo.py +++ b/cvat/apps/dataset_repo/dataset_repo.py @@ -463,7 +463,9 @@ def update_states(): try: get(db_git.task_id, db_user) except Exception: - slogger.glob("Exception occurred during a status updating for db_git with tid: {}".format(db_git.task_id)) + slogger.glob.exception("Exception occurred during a status " + "updating for db_git with tid: {}".format(db_git.task_id), + exc_info=True) @transaction.atomic def _onsave(jid, data, action): diff --git a/cvat/apps/dataset_repo/management/__init__.py b/cvat/apps/dataset_repo/management/__init__.py index d89b5880..b66dde17 100644 --- a/cvat/apps/dataset_repo/management/__init__.py +++ b/cvat/apps/dataset_repo/management/__init__.py @@ -1,3 +1,3 @@ # Copyright (C) 2018 Intel Corporation # -# SPDX-License-Identifier: MIT \ No newline at end of file +# SPDX-License-Identifier: MIT diff --git a/cvat/apps/dataset_repo/management/commands/__init__.py b/cvat/apps/dataset_repo/management/commands/__init__.py index d89b5880..b66dde17 100644 --- a/cvat/apps/dataset_repo/management/commands/__init__.py +++ b/cvat/apps/dataset_repo/management/commands/__init__.py @@ -1,3 +1,3 @@ # Copyright (C) 2018 Intel Corporation # -# SPDX-License-Identifier: MIT \ No newline at end of file +# SPDX-License-Identifier: MIT diff --git a/cvat/apps/engine/media_extractors.py b/cvat/apps/engine/media_extractors.py index c4b52e3d..8b0aebb8 100644 --- a/cvat/apps/engine/media_extractors.py +++ b/cvat/apps/engine/media_extractors.py @@ -494,7 +494,7 @@ class FragmentMediaReader: if idx < self._start_chunk_frame_number: continue elif idx < self._end_chunk_frame_number and \ - not ((idx - self._start_chunk_frame_number) % self._step): + not (idx - self._start_chunk_frame_number) % self._step: frame_range.append(idx) elif (idx - self._start_chunk_frame_number) % self._step: continue @@ -654,20 +654,20 @@ class Mpeg4ChunkWriter(IChunkWriter): } def _create_av_container(self, path, w, h, rate, options, f='mp4'): - # x264 requires width and height must be divisible by 2 for yuv420p - if h % 2: - h += 1 - if w % 2: - w += 1 + # x264 requires width and height must be divisible by 2 for yuv420p + if h % 2: + h += 1 + if w % 2: + w += 1 - container = av.open(path, 'w',format=f) - video_stream = container.add_stream(self._codec_name, rate=rate) - video_stream.pix_fmt = "yuv420p" - video_stream.width = w - video_stream.height = h - video_stream.options = options + container = av.open(path, 'w',format=f) + video_stream = container.add_stream(self._codec_name, rate=rate) + video_stream.pix_fmt = "yuv420p" + video_stream.width = w + video_stream.height = h + video_stream.options = options - return container, video_stream + return container, video_stream def save_as_chunk(self, images, chunk_path): if not images: @@ -779,7 +779,7 @@ def _is_zip(path): # 'mode': 'annotation' or 'interpolation' - mode of task that should be created. # 'unique': True or False - describes how the type can be combined with other. # True - only one item of this type and no other is allowed -# False - this media types can be combined with other which have unique == False +# False - this media types can be combined with other which have unique is False MEDIA_TYPES = { 'image': { diff --git a/cvat/apps/engine/parsers.py b/cvat/apps/engine/parsers.py index 5b23c7c3..549fe722 100644 --- a/cvat/apps/engine/parsers.py +++ b/cvat/apps/engine/parsers.py @@ -14,4 +14,4 @@ class TusUploadParser(BaseParser): # exception because a parser for the request with the non-standard # content media type isn't defined. # https://github.com/imtapps/django-rest-framework/blob/master/docs/api-guide/parsers.md - return {} \ No newline at end of file + return {} diff --git a/cvat/apps/engine/renderers.py b/cvat/apps/engine/renderers.py index 32c78c24..f56eb4d3 100644 --- a/cvat/apps/engine/renderers.py +++ b/cvat/apps/engine/renderers.py @@ -5,4 +5,4 @@ from rest_framework.renderers import JSONRenderer class CVATAPIRenderer(JSONRenderer): - media_type = 'application/vnd.cvat+json' \ No newline at end of file + media_type = 'application/vnd.cvat+json' diff --git a/cvat/apps/iam/__init__.py b/cvat/apps/iam/__init__.py index 23dfb721..5d3c1b03 100644 --- a/cvat/apps/iam/__init__.py +++ b/cvat/apps/iam/__init__.py @@ -2,4 +2,4 @@ # # SPDX-License-Identifier: MIT -from .schema import * \ No newline at end of file +from .schema import * # force import of extensions diff --git a/cvat/apps/iam/admin.py b/cvat/apps/iam/admin.py index cbbe15a1..c6f88c25 100644 --- a/cvat/apps/iam/admin.py +++ b/cvat/apps/iam/admin.py @@ -23,4 +23,4 @@ class CustomGroupAdmin(GroupAdmin): admin.site.unregister(User) admin.site.unregister(Group) admin.site.register(User, CustomUserAdmin) -admin.site.register(Group, CustomGroupAdmin) \ No newline at end of file +admin.site.register(Group, CustomGroupAdmin) diff --git a/cvat/apps/iam/schema.py b/cvat/apps/iam/schema.py index 6cb7a321..5fab917d 100644 --- a/cvat/apps/iam/schema.py +++ b/cvat/apps/iam/schema.py @@ -41,4 +41,4 @@ class SignatureAuthenticationScheme(OpenApiAuthenticationExtension): 'type': 'apiKey', 'in': 'query', 'name': 'sign', - } \ No newline at end of file + } diff --git a/cvat/apps/iam/urls.py b/cvat/apps/iam/urls.py index 76b0d319..c20091ff 100644 --- a/cvat/apps/iam/urls.py +++ b/cvat/apps/iam/urls.py @@ -38,4 +38,4 @@ if settings.IAM_TYPE == 'BASIC': name='account_email_verification_sent'), ] -urlpatterns = [path('auth/', include(urlpatterns))] \ No newline at end of file +urlpatterns = [path('auth/', include(urlpatterns))] diff --git a/cvat/apps/iam/views.py b/cvat/apps/iam/views.py index f9d23d73..f65cbc41 100644 --- a/cvat/apps/iam/views.py +++ b/cvat/apps/iam/views.py @@ -32,13 +32,13 @@ def get_context(request): org_id = request.GET.get('org_id') org_header = request.headers.get('X-Organization') - if org_id != None and (org_slug != None or org_header != None): - raise BadRequest('You cannot specify "org_id" query parameter with ' + + if org_id is not None and (org_slug is not None or org_header is not None): + raise BadRequest('You cannot specify "org_id" query parameter with ' '"org" query parameter or "X-Organization" HTTP header at the same time.') - if org_slug != None and org_header != None and org_slug != org_header: - raise BadRequest('You cannot specify "org" query parameter and ' + + if org_slug is not None and org_header is not None and org_slug != org_header: + raise BadRequest('You cannot specify "org" query parameter and ' '"X-Organization" HTTP header with different values.') - org_slug = org_slug if org_slug != None else org_header + org_slug = org_slug if org_slug is not None else org_header org_filter = None if org_slug: diff --git a/cvat/apps/lambda_manager/views.py b/cvat/apps/lambda_manager/views.py index 08f91f53..5025ba10 100644 --- a/cvat/apps/lambda_manager/views.py +++ b/cvat/apps/lambda_manager/views.py @@ -382,7 +382,7 @@ class LambdaQueue: def fetch_job(self, pk): queue = self._get_queue() job = queue.fetch_job(pk) - if job == None or not job.meta.get("lambda"): + if job is None or not job.meta.get("lambda"): raise ValidationError("{} lambda job is not found".format(pk), code=status.HTTP_404_NOT_FOUND) diff --git a/cvat/apps/organizations/views.py b/cvat/apps/organizations/views.py index 6065810c..f84bc89e 100644 --- a/cvat/apps/organizations/views.py +++ b/cvat/apps/organizations/views.py @@ -199,4 +199,4 @@ class InvitationViewSet(viewsets.ModelViewSet): if 'accepted' in self.request.query_params: serializer.instance.accept() else: - super().perform_update(serializer) \ No newline at end of file + super().perform_update(serializer) diff --git a/cvat/apps/profiler.py b/cvat/apps/profiler.py index ac9e18c0..45ddbc95 100644 --- a/cvat/apps/profiler.py +++ b/cvat/apps/profiler.py @@ -1,7 +1,7 @@ from django.apps import apps if apps.is_installed('silk'): - from silk.profiling.profiler import silk_profile + from silk.profiling.profiler import silk_profile # pylint: disable=unused-import else: from functools import wraps def silk_profile(name=None): @@ -10,4 +10,4 @@ else: def wrapped(*args, **kwargs): return f(*args, **kwargs) return wrapped - return profile \ No newline at end of file + return profile diff --git a/tests/rest_api/fixtures/data.py b/tests/rest_api/fixtures/data.py index e4a1f738..7a84f890 100644 --- a/tests/rest_api/fixtures/data.py +++ b/tests/rest_api/fixtures/data.py @@ -213,7 +213,7 @@ def org_staff(memberships): return set() else: return set(m['user']['id'] for m in memberships - if m['role'] in ['maintainer', 'owner'] and m['user'] != None + if m['role'] in ['maintainer', 'owner'] and m['user'] is not None and m['organization'] == org_id) return find @@ -224,7 +224,7 @@ def is_org_member(memberships): return True else: return user_id in set(m['user']['id'] for m in memberships - if m['user'] != None and m['organization'] == org_id) + if m['user'] is not None and m['organization'] == org_id) return check @pytest.fixture(scope='session') @@ -273,4 +273,4 @@ def filter_tasks_with_shapes(annotations): @pytest.fixture(scope='session') def tasks_with_shapes(tasks, filter_tasks_with_shapes): - return filter_tasks_with_shapes(tasks) \ No newline at end of file + return filter_tasks_with_shapes(tasks) diff --git a/tests/rest_api/test_invitations.py b/tests/rest_api/test_invitations.py index fd327df5..fd538a1f 100644 --- a/tests/rest_api/test_invitations.py +++ b/tests/rest_api/test_invitations.py @@ -22,7 +22,7 @@ class TestCreateInvitations: @staticmethod def get_non_member_users(memberships, users): - organization_users = set(m['user']['id'] for m in memberships if m['user'] != None) + organization_users = set(m['user']['id'] for m in memberships if m['user'] is not None) non_member_users = [u for u in users if u['id'] not in organization_users] return non_member_users @@ -30,7 +30,7 @@ class TestCreateInvitations: @staticmethod def get_member(role, memberships, org_id): member = [m['user'] for m in memberships if m['role'] == role and - m['organization'] == org_id and m['user'] != None][0] + m['organization'] == org_id and m['user'] is not None][0] return member diff --git a/utils/dataset_manifest/__init__.py b/utils/dataset_manifest/__init__.py index 5192a14e..83f332af 100644 --- a/utils/dataset_manifest/__init__.py +++ b/utils/dataset_manifest/__init__.py @@ -1,4 +1,4 @@ # Copyright (C) 2021 Intel Corporation # # SPDX-License-Identifier: MIT -from .core import VideoManifestManager, ImageManifestManager, is_manifest \ No newline at end of file +from .core import VideoManifestManager, ImageManifestManager, is_manifest diff --git a/utils/dataset_manifest/core.py b/utils/dataset_manifest/core.py index 36d70eab..9ea7b675 100644 --- a/utils/dataset_manifest/core.py +++ b/utils/dataset_manifest/core.py @@ -522,7 +522,7 @@ class VideoManifestManager(_ManifestManager): @property def data(self): - return (self.video_name) + return self.video_name def get_subset(self, subset_names): raise NotImplementedError() diff --git a/utils/dataset_manifest/create.py b/utils/dataset_manifest/create.py index 3cad4ab1..df2d8480 100644 --- a/utils/dataset_manifest/create.py +++ b/utils/dataset_manifest/create.py @@ -57,7 +57,7 @@ def main(): # If the source is a glob expression, we need additional processing abs_root = source - while abs_root and re.search('[*?\[\]]', abs_root): + while abs_root and re.search(r'[*?\[\]]', abs_root): abs_root = os.path.split(abs_root)[0] related_images = detect_related_images(sources, abs_root)