Skip to content

Commit

Permalink
type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mivanit committed Jun 9, 2024
1 parent 05f6b0e commit 21fe5d5
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions muutils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ def str_to_numeric(
pass

# mapping
_mapping: dict[str, int|float]
if mapping is True or mapping is None:
mapping = _REVERSE_SHORTEN_MAP
_mapping = _REVERSE_SHORTEN_MAP
else:
_mapping = mapping # type: ignore[assignment]

quantity_original: str = quantity

Expand All @@ -203,14 +206,14 @@ def str_to_numeric(
multiplier: int|float = 1

# detect if it has a suffix
suffixes_detected: list[bool] = [suffix in quantity for suffix in mapping]
suffixes_detected: list[bool] = [suffix in quantity for suffix in _mapping]
match sum(suffixes_detected):
case 0:
# no suffix
pass
case 1:
# find multiplier
for suffix, mult in mapping.items():
for suffix, mult in _mapping.items():
if quantity.endswith(suffix):
# remove suffix, store multiplier, and break
quantity = quantity.removesuffix(suffix).strip()
Expand Down Expand Up @@ -290,12 +293,6 @@ def pop(self, index=-1):

def clear(self):
raise AttributeError("list is frozen")

def __iadd__(self, other):
raise AttributeError("list is frozen")

def __imul__(self, other):
raise AttributeError("list is frozen")



Expand All @@ -311,11 +308,11 @@ def freeze(instance: object) -> object:
# mark as frozen
if hasattr(instance, "_IS_FROZEN"):
if instance._IS_FROZEN:
return
return instance

# try to mark as frozen
try:
instance._IS_FROZEN = True
instance._IS_FROZEN = True # type: ignore[attr-defined]
except AttributeError:
pass

Expand Down Expand Up @@ -349,10 +346,10 @@ def freeze(instance: object) -> object:
# handle custom classes
else:
# set everything in the __dict__ to frozen
instance.__dict__ = freeze(instance.__dict__)
instance.__dict__ = freeze(instance.__dict__) # type: ignore[assignment]

# create a new class which inherits from the original class
class FrozenClass(instance.__class__):
class FrozenClass(instance.__class__): # type: ignore[name-defined]
def __setattr__(self, name, value):
raise AttributeError("class is frozen")

Expand Down

0 comments on commit 21fe5d5

Please sign in to comment.