Skip to content

Commit

Permalink
Fixes: "EM101" raw-string-in-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ukmo-ccbunney committed Nov 19, 2024
1 parent d57a90c commit 2f6736e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
42 changes: 28 additions & 14 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,8 @@ def offset_by_time(self, origin):
"""
if not isinstance(origin, float | int):
raise TypeError("a numeric type for the origin argument is required")
msg = "a numeric type for the origin argument is required"
raise TypeError(msg)
try:
ut_unit = _ud.offset_by_time(self.ut_unit, origin)
except _ud.UdunitsError as exception:
Expand Down Expand Up @@ -1244,7 +1245,8 @@ def invert(self):
if self.is_unknown():
result = self
elif self.is_no_unit():
raise ValueError("Cannot invert a 'no-unit'.")
msg = "Cannot invert a 'no-unit'."
raise ValueError(msg)
else:
ut_unit = _ud.invert(self.ut_unit)
result = Unit._new_from_existing_ut(
Expand Down Expand Up @@ -1276,11 +1278,13 @@ def root(self, root):
"""
if round(root) != root:
raise TypeError("An integer for the root argument is required")
msg = "An integer for the root argument is required"
raise TypeError(msg)
if self.is_unknown():
result = self
elif self.is_no_unit():
raise ValueError("Cannot take the root of a 'no-unit'.")
msg = "Cannot take the root of a 'no-unit'."
raise ValueError(msg)
# only update the unit if it is not scalar
elif self == Unit("1"):
result = self
Expand Down Expand Up @@ -1320,12 +1324,14 @@ def log(self, base):
if self.is_unknown():
result = self
elif self.is_no_unit():
raise ValueError("Cannot take the logarithm of a 'no-unit'.")
msg = "Cannot take the logarithm of a 'no-unit'."
raise ValueError(msg)
else:
try:
ut_unit = _ud.log(base, self.ut_unit)
except TypeError:
raise TypeError("A numeric type for the base argument is required")
msg = "A numeric type for the base argument is required"
raise TypeError(msg)
except _ud.UdunitsError as exception:
value_err = _ud_value_error(
exception,
Expand Down Expand Up @@ -1378,7 +1384,8 @@ def _offset_common(self, offset):
if self.is_unknown():
result = self
elif self.is_no_unit():
raise ValueError("Cannot offset a 'no-unit'.")
msg = "Cannot offset a 'no-unit'."
raise ValueError(msg)
else:
try:
ut_unit = _ud.offset(self.ut_unit, offset)
Expand Down Expand Up @@ -1540,12 +1547,14 @@ def __pow__(self, power):
try:
power = float(power)
except ValueError:
raise TypeError("A numeric value is required for the power argument.")
msg = "A numeric value is required for the power argument."
raise TypeError(msg)

if self.is_unknown():
result = self
elif self.is_no_unit():
raise ValueError("Cannot raise the power of a 'no-unit'.")
msg = "Cannot raise the power of a 'no-unit'."
raise ValueError(msg)
elif self == Unit("1"):
# 1 ** N -> 1
result = self
Expand All @@ -1555,7 +1564,8 @@ def __pow__(self, power):
# root.
elif not math.isclose(power, 0.0) and abs(power) < 1:
if not math.isclose(1 / power, round(1 / power)):
raise ValueError("Cannot raise a unit by a decimal.")
msg = "Cannot raise a unit by a decimal."
raise ValueError(msg)
root = int(round(1 / power))
result = self.root(root)
else:
Expand Down Expand Up @@ -1652,7 +1662,8 @@ def change_calendar(self, calendar):
""" # NOQA E501
if not self.is_time_reference():
raise ValueError("unit is not a time reference")
msg = "unit is not a time reference"
raise ValueError(msg)

ref_date = self.num2date(0)
new_ref_date = ref_date.change_calendar(calendar)
Expand Down Expand Up @@ -1760,11 +1771,12 @@ def convert(self, value, other, ctype=FLOAT64, inplace=False): # noqa: FBT002
# with endianness other than native.
if result.dtype.byteorder != "=":
if inplace:
raise ValueError(
msg = str(
"Unable to convert non-native byte ordered "
"array in-place. Consider byte-swapping "
"first."
)
raise ValueError(msg)
result = result.astype(result.dtype.type)
# Strict type check of numpy array.
if result.dtype.type not in (np.float32, np.float64):
Expand All @@ -1788,10 +1800,11 @@ def convert(self, value, other, ctype=FLOAT64, inplace=False): # noqa: FBT002
result[...] = result_tmp
else:
if ctype not in _cv_convert_scalar:
raise ValueError(
msg = str(
"Invalid target type. Can only "
"convert to float or double."
)
raise ValueError(msg)
# Utilise global convenience dictionary
# _cv_convert_scalar
result = _cv_convert_scalar[ctype](ut_converter, result)
Expand All @@ -1805,7 +1818,8 @@ def cftime_unit(self):
"""
if self.calendar is None:
raise ValueError("Unit has undefined calendar")
msg = "Unit has undefined calendar"
raise ValueError(msg)

#
# ensure to strip out non-parsable 'UTC' postfix, which
Expand Down
3 changes: 2 additions & 1 deletion cf_units/_udunits2_parser/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
try:
import jinja2
except ImportError:
raise ImportError("Jinja2 needed to compile the grammar.")
msg = "Jinja2 needed to compile the grammar."
raise ImportError(msg)

ANTLR_VERSION = "4.11.1"
JAR_NAME = f"antlr-{ANTLR_VERSION}-complete.jar"
Expand Down
3 changes: 2 additions & 1 deletion cf_units/tests/test_coding_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def test_license_headers(self):
failed = True

if failed:
raise AssertionError("There were license header failures. See stdout.")
msg = "There were license header failures. See stdout."
raise AssertionError(msg)


@pytest.mark.skipif(not IS_GIT_REPO, reason="Not a git repository.")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ ignore = [
#"D404", # docstring-starts-with-this
"DTZ001", # call-datetime-without-tzinfo
"DTZ006", # call-datetime-fromtimestamp
"EM101", # raw-string-in-exception
#"EM101", # raw-string-in-exception
"EM102", # f-string-in-exception
"F403", # Wildcard imports
"F405", # Use of name from wildcard import
Expand Down

0 comments on commit 2f6736e

Please sign in to comment.