Skip to content

Commit e08fc2c

Browse files
authored
Fix mypy test failure (#634)
* Fix mypy test failure * Status 400 for unable to create frame * Lint fixes * Use literal 0.0 * More informative error message * Fix APIException code param
1 parent 0e4583b commit e08fc2c

6 files changed

Lines changed: 22 additions & 7 deletions

File tree

miqa/core/rest/frame.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from guardian.shortcuts import get_objects_for_user, get_perms
99
from rest_framework import mixins, serializers, status
1010
from rest_framework.decorators import action
11-
from rest_framework.exceptions import APIException
11+
from rest_framework.exceptions import APIException, ValidationError
1212
from rest_framework.permissions import IsAuthenticated
1313
from rest_framework.response import Response
1414
from rest_framework.viewsets import GenericViewSet
@@ -125,13 +125,17 @@ def create(self, request, *args, **kwargs):
125125
scan.save()
126126
elif 'scan' in serializer.data:
127127
scan = Scan.objects.get(id=serializer.data['scan'])
128+
if not scan:
129+
raise ValidationError('Could not create new Frame; Scan not found.')
128130
if not get_perms(request.user, scan.experiment.project):
129131
Response(status=status.HTTP_403_FORBIDDEN)
130132
else:
131133
raise APIException(
132134
'Provide either a parent scan or grandparent experiment for this frame.'
133135
)
134136

137+
if not scan:
138+
raise ValidationError('Could not create new Frame; Scan not found.')
135139
content_serializer = FrameContentSerializer(data=dict(request.data, scan=scan.id))
136140
content_serializer.is_valid(raise_exception=True)
137141
new_frame = content_serializer.save()

miqa/core/tasks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ def perform_import(import_dict):
214214
creator = None
215215
note = ''
216216
created = (
217-
datetime.now() if settings.REPLACE_NULL_CREATION_DATETIMES else None
217+
datetime.now().strftime('%Y-%m-%d %H:%M')
218+
if settings.REPLACE_NULL_CREATION_DATETIMES
219+
else None
218220
)
219221
location = {}
220222
if last_decision_dict['note']:
@@ -342,7 +344,13 @@ def perform_export(project_id: Optional[str]):
342344
frame_object.scan.session_id,
343345
frame_object.scan.scan_link,
344346
]
345-
last_decision = frame_object.scan.decisions.order_by('created').last()
347+
last_decision = (
348+
frame_object.scan.decisions.exclude(created__isnull=True)
349+
.order_by('created')
350+
.last()
351+
)
352+
if not last_decision:
353+
last_decision = frame_object.scan.decisions.order_by('created').last()
346354
if last_decision:
347355
location = ''
348356
if last_decision.location:

miqa/core/tests/test_import.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def test_import_with_relative_path(project_factory):
224224
import_data(project.id)
225225

226226
frame = Frame.objects.first()
227-
assert frame.raw_path == str(Path(__file__).parent / 'data' / 'example.nii.gz')
227+
assert frame and frame.raw_path == str(Path(__file__).parent / 'data' / 'example.nii.gz')
228228

229229

230230
@pytest.mark.django_db
@@ -234,7 +234,7 @@ def test_import_s3_preserves_path(project_factory):
234234
import_data(project.id)
235235

236236
frame = Frame.objects.first()
237-
assert (
237+
assert frame and (
238238
frame.raw_path
239239
== 's3://miqa-sample/IXI_small/Guys/IXI002/0828-DTI/IXI002-Guys-0828-DTI-00.nii.gz'
240240
)

miqa/learning/nn_training.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def train_and_save_model(df, count_train, save_path, num_epochs, val_interval, o
588588
logger.info('-' * 25)
589589
logger.info(f'epoch {epoch + 1}/{num_epochs}')
590590
model.train()
591-
epoch_loss = 0
591+
epoch_loss = 0.0
592592
step = 0
593593
epoch_len = len(train_loader.dataset) // train_loader.batch_size
594594
logger.info(f'epoch_len: {epoch_len}')

miqa/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
HerokuProductionBaseConfiguration,
1111
HttpsMixin,
1212
ProductionBaseConfiguration,
13+
S3StorageMixin,
1314
SmtpEmailMixin,
1415
TestingBaseConfiguration,
15-
S3StorageMixin,
1616
)
1717
from composed_configuration._configuration import _BaseConfiguration
1818
from configurations import values

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
strict = True
3+
disable_error_code = misc, annotation-unchecked, import, no-untyped-def, no-untyped-call, var-annotated, type-arg, no-any-return, list-item

0 commit comments

Comments
 (0)