Skip to content

Commit

Permalink
Fix linting issue E721: type-comparison
Browse files Browse the repository at this point in the history
... for an exact type match, use is or is not.

https://docs.astral.sh/ruff/rules/type-comparison/#type-comparison-e721

If you want to check for an exact type match, use is or is not.
  • Loading branch information
Josef-Friedrich committed Nov 9, 2024
1 parent e1de364 commit 4831bb7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _safe_cast(out_type, val):
if val is None:
return None

if out_type == int:
if out_type is int:
if isinstance(val, int) or isinstance(val, float):
# Just a number.
return int(val)
Expand All @@ -203,22 +203,22 @@ def _safe_cast(out_type, val):
match = re.match(r'[\+-]?[0-9]+', val.strip())
return int(match.group(0)) if match else 0

elif out_type == bool:
elif out_type is bool:
try:
# Should work for strings, bools, ints:
return bool(int(val))
except ValueError:
return False

elif out_type == str:
elif out_type is str:
if isinstance(val, bytes):
return val.decode('utf-8', 'ignore')
elif isinstance(val, str):
return val
else:
return str(val)

elif out_type == float:
elif out_type is float:
if isinstance(val, int) or isinstance(val, float):
return float(val)
else:
Expand Down Expand Up @@ -1271,13 +1271,13 @@ def _none_value(self):
"""Get an appropriate "null" value for this field's type. This
is used internally when setting the field to None.
"""
if self.out_type == int:
if self.out_type is int:
return 0
elif self.out_type == float:
elif self.out_type is float:
return 0.0
elif self.out_type == bool:
elif self.out_type is bool:
return False
elif self.out_type == str:
elif self.out_type is str:
return u''


Expand Down

0 comments on commit 4831bb7

Please sign in to comment.