Skip to content

Commit 6d0322c

Browse files
committed
OpenConceptLab/ocl_issues#960 | fixed pylint warnings
1 parent ebe4736 commit 6d0322c

38 files changed

Lines changed: 207 additions & 244 deletions

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[MESSAGES CONTROL]
2-
disable=missing-docstring,too-few-public-methods,no-member,too-many-ancestors,attribute-defined-outside-init,fixme,broad-except,import-outside-toplevel,too-many-instance-attributes,unsupported-membership-test,too-many-public-methods,too-many-lines
2+
disable=missing-docstring,too-few-public-methods,no-member,too-many-ancestors,attribute-defined-outside-init,fixme,broad-except,import-outside-toplevel,too-many-instance-attributes,unsupported-membership-test,too-many-public-methods,too-many-lines,unused-private-member,consider-using-with,duplicate-code
33
[MASTER]
44
ignore=migrations,fixtures,scripts,commands,documents.py,password_validation.py,v1_dump
55
[FORMAT]

core/client_configs/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def validate_home_tabs_config(self):
8282
self.errors = dict(tabs=[EMPTY_TABS_CONFIG])
8383
elif not isinstance(tabs, list):
8484
self.errors = dict(tabs=[NOT_LIST_TABS_CONFIG])
85-
elif any([not isinstance(tab, dict) for tab in tabs]):
85+
elif any(not isinstance(tab, dict) for tab in tabs):
8686
self.errors = dict(tabs=[INVALID_TABS_CONFIG])
8787
else:
8888
default_tabs = [tab for tab in tabs if tab.get('default', False)]
@@ -105,7 +105,7 @@ def validate_sort_attributes(self):
105105
return
106106

107107
def __get_resource_sortable_fields(self, tab_type):
108-
es_fields = self.__get_es_fields(tab_type) or dict()
108+
es_fields = self.__get_es_fields(tab_type) or {}
109109
return [field for field, config in es_fields.items() if config.get('sortable', False)]
110110

111111
@staticmethod

core/client_configs/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_is_home(self):
1717
self.assertFalse(ClientConfig(type='blah').is_home)
1818

1919
def test_home_config_validation(self):
20-
client_config = ClientConfig(config=dict())
20+
client_config = ClientConfig(config={})
2121

2222
with self.assertRaises(ValidationError) as ex:
2323
client_config.full_clean()
@@ -153,7 +153,7 @@ def tearDown(self):
153153
def test_post(self):
154154
response = self.client.post(
155155
self.org.url + 'client-configs/',
156-
dict(),
156+
{},
157157
HTTP_AUTHORIZATION='Token ' + self.token,
158158
format='json'
159159
)

core/collections/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def check_concept_uniqueness_in_collection_and_locale_by_name_attribute(
159159
if not other_concepts_in_collection.exists():
160160
return
161161

162-
matching_names_in_concept = dict()
162+
matching_names_in_concept = {}
163163
names = concept.names.filter(**{attribute: value})
164164

165165
for name in names:
@@ -229,7 +229,7 @@ def add_references_in_bulk(self, expressions, user=None): # pylint: disable=too
229229
new_expressions.discard(existing_expression)
230230
errors[existing_expression] = [REFERENCE_ALREADY_EXISTS]
231231

232-
added_references = list()
232+
added_references = []
233233
for expression in new_expressions:
234234
ref = CollectionReference(expression=expression)
235235
try:

core/collections/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Meta:
226226
def __init__(self, *args, **kwargs):
227227
params = get(kwargs, 'context.request.query_params')
228228

229-
self.query_params = dict()
229+
self.query_params = {}
230230
if params:
231231
self.query_params = params if isinstance(params, dict) else params.dict()
232232
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]

core/collections/views.py

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydash import get
1010
from rest_framework import status, mixins
1111
from rest_framework.generics import (
12-
RetrieveAPIView, DestroyAPIView, UpdateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView
12+
RetrieveAPIView, DestroyAPIView, UpdateAPIView, ListAPIView
1313
)
1414
from rest_framework.permissions import IsAuthenticated
1515
from rest_framework.response import Response
@@ -31,12 +31,12 @@
3131
CollectionVersionSummaryDetailSerializer, CollectionReferenceDetailSerializer)
3232
from core.collections.utils import is_version_specified
3333
from core.common.constants import (
34-
HEAD, RELEASED_PARAM, PROCESSING_PARAM, OK_MESSAGE, NOT_FOUND, MUST_SPECIFY_EXTRA_PARAM_IN_BODY
34+
HEAD, RELEASED_PARAM, PROCESSING_PARAM, OK_MESSAGE
3535
)
3636
from core.common.mixins import (
3737
ConceptDictionaryCreateMixin, ListWithHeadersMixin, ConceptDictionaryUpdateMixin,
3838
ConceptContainerExportMixin,
39-
ConceptContainerProcessingMixin)
39+
ConceptContainerProcessingMixin, ConceptContainerExtraRetrieveUpdateDestroyView)
4040
from core.common.permissions import (
4141
CanViewConceptDictionary, CanEditConceptDictionary, HasAccessToVersionedObject,
4242
CanViewConceptDictionaryVersion
@@ -535,51 +535,10 @@ def list(self, request, *args, **kwargs):
535535
return Response(get(self.get_object(), 'extras', {}))
536536

537537

538-
class CollectionExtraRetrieveUpdateDestroyView(CollectionExtrasBaseView, RetrieveUpdateDestroyAPIView):
538+
class CollectionExtraRetrieveUpdateDestroyView(CollectionExtrasBaseView,
539+
ConceptContainerExtraRetrieveUpdateDestroyView):
539540
serializer_class = CollectionDetailSerializer
540541

541-
def retrieve(self, request, *args, **kwargs):
542-
key = kwargs.get('extra')
543-
instance = self.get_object()
544-
extras = get(instance, 'extras', {})
545-
if key in extras:
546-
return Response({key: extras[key]})
547-
548-
return Response(dict(detail=NOT_FOUND), status=status.HTTP_404_NOT_FOUND)
549-
550-
def update(self, request, **kwargs): # pylint: disable=arguments-differ
551-
key = kwargs.get('extra')
552-
value = request.data.get(key)
553-
if not value:
554-
return Response([MUST_SPECIFY_EXTRA_PARAM_IN_BODY.format(key)], status=status.HTTP_400_BAD_REQUEST)
555-
556-
instance = self.get_object()
557-
instance.extras = get(instance, 'extras', {})
558-
instance.extras[key] = value
559-
instance.comment = 'Updated extras: %s=%s.' % (key, value)
560-
head = instance.get_head()
561-
head.extras = get(head, 'extras', {})
562-
head.extras.update(instance.extras)
563-
instance.save()
564-
head.save()
565-
return Response({key: value})
566-
567-
def delete(self, request, *args, **kwargs):
568-
key = kwargs.get('extra')
569-
instance = self.get_object()
570-
instance.extras = get(instance, 'extras', {})
571-
if key in instance.extras:
572-
del instance.extras[key]
573-
instance.comment = 'Deleted extra %s.' % key
574-
head = instance.get_head()
575-
head.extras = get(head, 'extras', {})
576-
del head.extras[key]
577-
instance.save()
578-
head.save()
579-
return Response(status=status.HTTP_204_NO_CONTENT)
580-
581-
return Response(dict(detail=NOT_FOUND), status=status.HTTP_404_NOT_FOUND)
582-
583542

584543
class CollectionVersionProcessingView(CollectionBaseView, ConceptContainerProcessingMixin):
585544
serializer_class = CollectionVersionDetailSerializer

core/common/healthcheck/healthcheck.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def check_status(self):
2727
if not response.ok:
2828
raise ServiceUnavailable('Flower Unavailable')
2929
except Exception as ex:
30-
raise ServiceUnavailable(ex.args)
30+
raise ServiceUnavailable(ex.args) from ex
3131

3232
def identifier(self):
3333
return "Flower"
@@ -45,7 +45,7 @@ def check_status(self):
4545
if not is_ok:
4646
raise ServiceReturnedUnexpectedResult("Status {}".format(status))
4747
except Exception as ex:
48-
raise ServiceReturnedUnexpectedResult(ex.args)
48+
raise ServiceReturnedUnexpectedResult(ex.args) from ex
4949

5050
def identifier(self):
5151
return 'ElasticSearch'

core/common/mixins.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
from django.utils.functional import cached_property
1212
from pydash import compact, get
1313
from rest_framework import status
14+
from rest_framework.generics import RetrieveUpdateDestroyAPIView
1415
from rest_framework.mixins import ListModelMixin, CreateModelMixin
1516
from rest_framework.response import Response
1617

1718
from core.common.constants import HEAD, ACCESS_TYPE_EDIT, ACCESS_TYPE_VIEW, ACCESS_TYPE_NONE, INCLUDE_FACETS, \
18-
LIST_DEFAULT_LIMIT, HTTP_COMPRESS_HEADER, CSV_DEFAULT_LIMIT, FACETS_ONLY
19+
LIST_DEFAULT_LIMIT, HTTP_COMPRESS_HEADER, CSV_DEFAULT_LIMIT, FACETS_ONLY, NOT_FOUND, \
20+
MUST_SPECIFY_EXTRA_PARAM_IN_BODY
1921
from core.common.permissions import HasPrivateAccess, HasOwnership, CanViewConceptDictionary
2022
from core.common.services import S3
2123
from .utils import write_csv_to_s3, get_csv_from_s3, get_query_params_from_url_string, compact_dict_by_values
@@ -128,7 +130,7 @@ def list(self, request, *args, **kwargs): # pylint:disable=too-many-locals
128130

129131
sorted_list = self.object_list
130132

131-
headers = dict()
133+
headers = {}
132134
results = sorted_list
133135
if not compress:
134136
paginator = CustomPaginator(
@@ -460,7 +462,7 @@ def from_uri_queryset(cls, uri):
460462
queryset = cls.objects.none()
461463

462464
try:
463-
kwargs = get(resolve(uri), 'kwargs', dict())
465+
kwargs = get(resolve(uri), 'kwargs', {})
464466
query_params = get_query_params_from_url_string(uri) # parsing query parameters
465467
kwargs.update(query_params)
466468
if 'concept' in kwargs:
@@ -484,7 +486,7 @@ def global_listing_queryset(cls, params, user):
484486

485487
@staticmethod
486488
def get_parent_and_owner_filters_from_uri(uri):
487-
filters = dict()
489+
filters = {}
488490
if not uri:
489491
return filters
490492

@@ -639,3 +641,47 @@ def post(self, request, *args, **kwargs): # pylint: disable=unused-argument
639641
version.clear_processing()
640642

641643
return Response(status=status.HTTP_200_OK)
644+
645+
646+
class ConceptContainerExtraRetrieveUpdateDestroyView(RetrieveUpdateDestroyAPIView):
647+
def retrieve(self, request, *args, **kwargs):
648+
key = kwargs.get('extra')
649+
instance = self.get_object()
650+
extras = get(instance, 'extras', {})
651+
if key in extras:
652+
return Response({key: extras[key]})
653+
654+
return Response(dict(detail=NOT_FOUND), status=status.HTTP_404_NOT_FOUND)
655+
656+
def update(self, request, **kwargs): # pylint: disable=arguments-differ
657+
key = kwargs.get('extra')
658+
value = request.data.get(key)
659+
if not value:
660+
return Response([MUST_SPECIFY_EXTRA_PARAM_IN_BODY.format(key)], status=status.HTTP_400_BAD_REQUEST)
661+
662+
instance = self.get_object()
663+
instance.extras = get(instance, 'extras', {})
664+
instance.extras[key] = value
665+
instance.comment = 'Updated extras: %s=%s.' % (key, value)
666+
head = instance.get_head()
667+
head.extras = get(head, 'extras', {})
668+
head.extras.update(instance.extras)
669+
instance.save()
670+
head.save()
671+
return Response({key: value})
672+
673+
def delete(self, request, *args, **kwargs):
674+
key = kwargs.get('extra')
675+
instance = self.get_object()
676+
instance.extras = get(instance, 'extras', {})
677+
if key in instance.extras:
678+
del instance.extras[key]
679+
instance.comment = 'Deleted extra %s.' % key
680+
head = instance.get_head()
681+
head.extras = get(head, 'extras', {})
682+
del head.extras[key]
683+
instance.save()
684+
head.save()
685+
return Response(status=status.HTTP_204_NO_CONTENT)
686+
687+
return Response(dict(detail=NOT_FOUND), status=status.HTTP_404_NOT_FOUND)

core/common/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def versioned_object_url(self):
300300
@classmethod
301301
def get_version(cls, mnemonic, version=HEAD, filters=None):
302302
if not filters:
303-
filters = dict()
303+
filters = {}
304304
return cls.objects.filter(**{cls.mnemonic_attr: mnemonic, **filters}, version=version).first()
305305

306306
def get_latest_version(self):
@@ -502,7 +502,7 @@ def seed_references():
502502

503503
@classmethod
504504
def persist_new(cls, obj, created_by, **kwargs):
505-
errors = dict()
505+
errors = {}
506506
parent_resource = kwargs.pop('parent_resource', None) or obj.parent
507507
if not parent_resource:
508508
errors['parent'] = PARENT_RESOURCE_CANNOT_BE_NONE
@@ -541,7 +541,7 @@ def persist_new_version(cls, obj, user=None, **kwargs):
541541
from core.collections.serializers import CollectionDetailSerializer
542542
from core.sources.serializers import SourceDetailSerializer
543543

544-
errors = dict()
544+
errors = {}
545545

546546
obj.is_active = True
547547
if user:
@@ -566,7 +566,7 @@ def persist_new_version(cls, obj, user=None, **kwargs):
566566

567567
@classmethod
568568
def persist_changes(cls, obj, updated_by, original_schema, **kwargs):
569-
errors = dict()
569+
errors = {}
570570
parent_resource = kwargs.pop('parent_resource', obj.parent)
571571
if not parent_resource:
572572
errors['parent'] = SOURCE_PARENT_CANNOT_BE_NONE
@@ -708,7 +708,7 @@ def is_processing(self):
708708
return False
709709

710710
def clear_processing(self):
711-
self._background_process_ids = list()
711+
self._background_process_ids = []
712712
self.save(update_fields=['_background_process_ids'])
713713

714714
@property

core/common/services.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def generate_signed_url(cls, accessor, key, metadata=None):
4949
except NoCredentialsError: # pragma: no cover
5050
pass
5151

52+
return None
53+
5254
@classmethod
5355
def upload(cls, file_path, file_content, headers=None, metadata=None):
5456
url = cls.generate_signed_url(cls.PUT, file_path, metadata)
@@ -67,7 +69,7 @@ def upload_file(
6769
): # pylint: disable=too-many-arguments
6870
read_directive = 'rb' if binary else 'r'
6971
file_path = file_path if file_path else key
70-
return cls.upload(key, open(file_path, read_directive).read(), headers, metadata)
72+
return cls.upload(key, open(file_path, read_directive, encoding='utf-8').read(), headers, metadata)
7173

7274
@classmethod
7375
def upload_public(cls, file_path, file_content):
@@ -82,6 +84,8 @@ def upload_public(cls, file_path, file_content):
8284
except NoCredentialsError: # pragma: no cover
8385
pass
8486

87+
return None
88+
8589
@classmethod
8690
def upload_base64( # pylint: disable=too-many-arguments,inconsistent-return-statements
8791
cls, doc_base64, file_name, append_extension=True, public_read=False, headers=None
@@ -173,7 +177,7 @@ def missing_objects(cls, objects, prefix_path, sub_paths): # pragma: no cover
173177

174178
for obj in objects:
175179
paths = [obj.pdf_path(path) for path in sub_paths]
176-
if not all([path in s3_keys for path in paths]):
180+
if not all(path in s3_keys for path in paths):
177181
missing_objects.append(obj)
178182

179183
return missing_objects
@@ -189,6 +193,8 @@ def remove(cls, key):
189193
except NoCredentialsError: # pragma: no cover
190194
pass
191195

196+
return None
197+
192198

193199
class RedisService: # pragma: no cover
194200
def __init__(self):

0 commit comments

Comments
 (0)