Skip to content

Commit

Permalink
fix: upgrade to flexparser>=0.4, exceptions are no longer dataclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Nov 7, 2024
1 parent 2839f6e commit d592aff
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
9 changes: 6 additions & 3 deletions pint/delegates/txt_defparser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

from __future__ import annotations

from dataclasses import dataclass, field
from dataclasses import dataclass

import flexparser as fp

from ... import errors
from ..base_defparser import ParserConfig


@dataclass(frozen=True)
class DefinitionSyntaxError(errors.DefinitionSyntaxError, fp.ParsingError):
"""A syntax error was found in a definition. Combines:
Expand All @@ -30,7 +29,11 @@ class DefinitionSyntaxError(errors.DefinitionSyntaxError, fp.ParsingError):
and an extra location attribute in which the filename or reseource is stored.
"""

location: str = field(init=False, default="")
msg: str

def __init__(self, msg: str, location: str = ""):
self.msg = msg
self.location = location

def __str__(self) -> str:
msg = (
Expand Down
98 changes: 65 additions & 33 deletions pint/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from __future__ import annotations

import typing as ty
from dataclasses import dataclass, fields

OFFSET_ERROR_DOCS_HTML = "https://pint.readthedocs.io/en/stable/user/nonmult.html"
LOG_ERROR_DOCS_HTML = "https://pint.readthedocs.io/en/stable/user/log_units.html"
Expand Down Expand Up @@ -81,82 +80,87 @@ def def_err(self, msg: str):
return DefinitionError(self.name, self.__class__, msg)


@dataclass(frozen=False)
class PintError(Exception):
"""Base exception for all Pint errors."""


@dataclass(frozen=False)
class DefinitionError(ValueError, PintError):
"""Raised when a definition is not properly constructed."""

name: str
definition_type: type
msg: str

def __init__(self, name: str, definition_type: type, msg: str):
self.name = name
self.definition_type = definition_type
self.msg = msg

def __str__(self):
msg = f"Cannot define '{self.name}' ({self.definition_type}): {self.msg}"
return msg

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.name, self.definition_type, self.msg)


@dataclass(frozen=False)
class DefinitionSyntaxError(ValueError, PintError):
"""Raised when a textual definition has a syntax error."""

msg: str

def __init__(self, msg: str):
self.msg = msg

def __str__(self):
return self.msg

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.msg,)


@dataclass(frozen=False)
class RedefinitionError(ValueError, PintError):
"""Raised when a unit or prefix is redefined."""

name: str
definition_type: type

def __init__(self, name: str, definition_type: type):
self.name = name
self.definition_type = definition_type

def __str__(self):
msg = f"Cannot redefine '{self.name}' ({self.definition_type})"
return msg

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.name, self.definition_type)


@dataclass(frozen=False)
class UndefinedUnitError(AttributeError, PintError):
"""Raised when the units are not defined in the unit registry."""

unit_names: str | tuple[str, ...]
unit_names: tuple[str, ...]

def __init__(self, unit_names: str | ty.Iterable[str]):
if isinstance(unit_names, str):
self.unit_names = (unit_names,)
else:
self.unit_names = tuple(unit_names)

def __str__(self):
if isinstance(self.unit_names, str):
return f"'{self.unit_names}' is not defined in the unit registry"
if (
isinstance(self.unit_names, (tuple, list, set))
and len(self.unit_names) == 1
):
if len(self.unit_names) == 1:
return f"'{tuple(self.unit_names)[0]}' is not defined in the unit registry"
return f"{tuple(self.unit_names)} are not defined in the unit registry"

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.unit_names,)


@dataclass(frozen=False)
class PintTypeError(TypeError, PintError):
def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
pass


@dataclass(frozen=False)
class DimensionalityError(PintTypeError):
"""Raised when trying to convert between incompatible units."""

Expand All @@ -166,6 +170,20 @@ class DimensionalityError(PintTypeError):
dim2: str = ""
extra_msg: str = ""

def __init__(
self,
units1: ty.Any,
units2: ty.Any,
dim1: str = "",
dim2: str = "",
extra_msg: str = "",
) -> None:
self.units1 = units1
self.units2 = units2
self.dim1 = dim1
self.dim2 = dim2
self.extra_msg = extra_msg

def __str__(self):
if self.dim1 or self.dim2:
dim1 = f" ({self.dim1})"
Expand All @@ -180,16 +198,25 @@ def __str__(self):
)

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (
self.units1,
self.units2,
self.dim1,
self.dim2,
self.extra_msg,
)


@dataclass(frozen=False)
class OffsetUnitCalculusError(PintTypeError):
"""Raised on ambiguous operations with offset units."""

units1: ty.Any
units2: ty.Optional[ty.Any] = None

def __init__(self, units1: ty.Any, units2: ty.Optional[ty.Any] = None) -> None:
self.units1 = units1
self.units2 = units2

def yield_units(self):
yield self.units1
if self.units2:
Expand All @@ -205,16 +232,19 @@ def __str__(self):
)

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.units1, self.units2)


@dataclass(frozen=False)
class LogarithmicUnitCalculusError(PintTypeError):
"""Raised on inappropriate operations with logarithmic units."""

units1: ty.Any
units2: ty.Optional[ty.Any] = None

def __init__(self, units1: ty.Any, units2: ty.Optional[ty.Any] = None) -> None:
self.units1 = units1
self.units2 = units2

def yield_units(self):
yield self.units1
if self.units2:
Expand All @@ -230,26 +260,28 @@ def __str__(self):
)

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.units1, self.units2)


@dataclass(frozen=False)
class UnitStrippedWarning(UserWarning, PintError):
msg: str

def __init__(self, msg: str):
self.msg = msg

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.msg,)


@dataclass(frozen=False)
class UnexpectedScaleInContainer(Exception):
def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
pass


@dataclass(frozen=False)
class UndefinedBehavior(UserWarning, PintError):
msg: str

def __init__(self, msg: str):
self.msg = msg

def __reduce__(self):
return self.__class__, tuple(getattr(self, f.name) for f in fields(self))
return self.__class__, (self.msg,)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platformdirs>=2.1.0
typing_extensions>=4.0.0
flexcache>=0.3
flexparser>=0.3
flexparser>=0.4

0 comments on commit d592aff

Please sign in to comment.