mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
replace try/except with context manager
This commit is contained in:
parent
b9a57af6d4
commit
68d0ca6e7e
|
@ -1,3 +1,4 @@
|
||||||
|
import contextlib
|
||||||
import copy
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
|
@ -690,15 +691,13 @@ class BooleanField(Field):
|
||||||
NULL_VALUES = {'null', 'Null', 'NULL', '', None}
|
NULL_VALUES = {'null', 'Null', 'NULL', '', None}
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
try:
|
with contextlib.suppress(TypeError):
|
||||||
if data in self.TRUE_VALUES:
|
if data in self.TRUE_VALUES:
|
||||||
return True
|
return True
|
||||||
elif data in self.FALSE_VALUES:
|
elif data in self.FALSE_VALUES:
|
||||||
return False
|
return False
|
||||||
elif data in self.NULL_VALUES and self.allow_null:
|
elif data in self.NULL_VALUES and self.allow_null:
|
||||||
return None
|
return None
|
||||||
except TypeError: # Input is an unhashable type
|
|
||||||
pass
|
|
||||||
self.fail('invalid', input=data)
|
self.fail('invalid', input=data)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user