from rest_framework import serializers
from rest_framework.exceptions import ValidationError
class MySerializer(serializers.Serializer):
param = serializers.ListField()
s = MySerializer(data={}) # is not valid data
try:
s.is_valid(raise_exception=True)
except ValueError as exc:
print('value error')
except ValidationError as exc:
print('validation error')
Sourcery incorrectly marks the 2nd except handler (for ValidationError) as unreachable:
Yet when I run this program, it prints out "validation error" indicating that handler was reached. Furthermore after applying the refactor (removing the ValidationError handler) the program now errors with a ValidationError exception:
240
241 if self._errors and raise_exception:
--> 242 raise ValidationError(self.errors)
243
244 return not bool(self._errors)
ValidationError: {'param': [ErrorDetail(string='This field is required.', code='required')]}
Issue description or question
With this snippet:
Sourcery incorrectly marks the 2nd except handler (for
ValidationError
) as unreachable:Yet when I run this program, it prints out "validation error" indicating that handler was reached. Furthermore after applying the refactor (removing the ValidationError handler) the program now errors with a ValidationError exception:
rest_framework
is the Django Rest FrameworkSourcery Version
VS Code extension v0.12.6
Code editor or IDE name and version
VS Code:
I ran the snippet in a Python 3.9 venv, and the version of DRF installed is 3.11.0.
OS name and version
OSX Monterey 12.4