Skip to content

Commit

Permalink
ruff check --select UP
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaasRostock committed Feb 3, 2025
1 parent 39607ad commit fced0e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/component_model/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, logger, *args, **kwargs):
self._out = sys.stdout
except Exception: # noqa: BLE001
self._out = sys.stderr
super(MsgCounterHandler, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.levelcount = {"DEBUG": 0, "INFO": 0, "WARNING": 0, "ERROR": 0}
self.ideType = None
if "idlelib.run" in sys.modules: # if idlelib.run exists
Expand Down
24 changes: 12 additions & 12 deletions src/component_model/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def start(self):

@start.setter
def start(self, val):
if isinstance(val, (str, int, float, bool, Enum)):
if isinstance(val, str | int | float | bool | Enum):
self._start = (val,)
elif isinstance(val, (tuple, list, np.ndarray)):
elif isinstance(val, tuple | list | np.ndarray):
self._start = tuple(val)
else:
raise VariableInitError(f"Unallowed start value setting {val} for variable {self.name}") from None
Expand All @@ -241,7 +241,7 @@ def unit(self):

@unit.setter
def unit(self, val):
if isinstance(val, (tuple, list)):
if isinstance(val, tuple | list):
self._unit = tuple(val)
elif isinstance(val, str):
self._unit = (val,)
Expand Down Expand Up @@ -269,7 +269,7 @@ def range(self):
def range(self, val):
if isinstance(val, tuple) and isinstance(val[0], tuple): # compound variable
self._range = val
elif isinstance(val, tuple) and all(isinstance(val[i], (int, float, bool, Enum, str)) for i in range(2)):
elif isinstance(val, tuple) and all(isinstance(val[i], int | float | bool | Enum | str) for i in range(2)):
self._range = (val,)

@property
Expand Down Expand Up @@ -305,7 +305,7 @@ def setter( # noqa: C901, PLR0912
Alternatively, single elements can be set by providing the index explicitly.
"""
assert self._typ is not None, "Need a proper type at this stage"
if self._len == 1 and not isinstance(value, (tuple, list, np.ndarray)):
if self._len == 1 and not isinstance(value, tuple | list | np.ndarray):
value = [value]
assert idx is None or idx == 0, f"Invalid idx {idx} for scalar"

Expand Down Expand Up @@ -474,12 +474,12 @@ def check_range( # noqa: C901, PLR0911, PLR0912
if isinstance(value, str): # no range checking on strings
return self._typ is str
if self._len > 1 and idx is None: # check all components
assert isinstance(value, (tuple, list, np.ndarray)), f"{value} is not a tuple, list, or ndarray"
assert isinstance(value, tuple | list | np.ndarray), f"{value} is not a tuple, list, or ndarray"
assert len(value) == self._len, f"{value} has no elements"
return all(self.check_range(value=value[i], idx=i, disp=disp) for i in range(self._len))
# single component check
assert idx is not None, "Need a proper idx here"
if isinstance(value, (tuple, list, np.ndarray)):
if isinstance(value, tuple | list | np.ndarray):
if self._len == 1:
idx = 0
value = value[idx]
Expand All @@ -498,7 +498,7 @@ def check_range( # noqa: C901, PLR0911, PLR0912
if isinstance(value, Enum):
return isinstance(value, self._typ)

if isinstance(value, (int, float)) and all(isinstance(x, (int, float)) for x in self._range[idx]):
if isinstance(value, int | float) and all(isinstance(x, int | float) for x in self._range[idx]):
if not disp and self._display[idx] is not None: # check an internal unit value
_val = self._display[idx]
assert isinstance(_val, tuple)
Expand Down Expand Up @@ -528,7 +528,7 @@ def auto_type( # noqa: C901, PLR0912
Therefore int Variables must be explicitly specified.
"""
assert val is not None, "'val is None'!"
if isinstance(val, (tuple, list, np.ndarray)):
if isinstance(val, tuple | list | np.ndarray):
types = [cls.auto_type(val=x, allow_int=allow_int) for x in val]
typ = None
for t in types:
Expand Down Expand Up @@ -556,7 +556,7 @@ def auto_type( # noqa: C901, PLR0912
return bool
if allow_int:
return type(val)
if not allow_int and isinstance(val, (int, float)):
if not allow_int and isinstance(val, int | float):
return float
return type(val)

Expand Down Expand Up @@ -590,7 +590,7 @@ def _disect_unit(self, quantity: PyType | Compound) -> tuple:
the magnitude in base units, the base unit and the unit as given (display units),
together with the conversion functions between the units.
"""
if isinstance(quantity, (tuple, list, np.ndarray)): # handle composit values
if isinstance(quantity, tuple | list | np.ndarray): # handle composit values
_val, _ub, _disp = [], [], []
for q in quantity: # disect components and collect results
v, u, d = self._disect_unit(q)
Expand All @@ -603,7 +603,7 @@ def _disect_unit(self, quantity: PyType | Compound) -> tuple:
assert self.model.ureg is not None, f"UnitRegistry not found, while providing units: {quantity}"
try:
q = self.model.ureg(quantity) # parse the quantity-unit and return a Pint Quantity object
if isinstance(q, (int, float)):
if isinstance(q, int | float):
return q, "", None # integer or float variable with no units provided
if isinstance(q, Quantity): # pint.Quantity object
# transform to base units ('SI' units). All internal calculations will be performed with these
Expand Down

0 comments on commit fced0e8

Please sign in to comment.