Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Feb 16, 2024
1 parent 7442706 commit 410f9c6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/property_utils/exceptions/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=missing-class-docstring
from typing import Optional


Expand All @@ -9,7 +10,7 @@ class PropertyUtilsException(Exception):

description: str

def __init__(self, msg: Optional[str] = None, *args) -> None:
def __init__(self, *args, msg: Optional[str] = None) -> None:
if msg is None:
msg = self.description
super().__init__(msg, *args)
Expand Down
113 changes: 56 additions & 57 deletions src/property_utils/units/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ class UnitDescriptor(Protocol):
Descriptor for a property unit that has a specific unit, e.g. cm^2 or ft^2.
"""

def isinstance(self, generic: GenericUnitDescriptor) -> bool: ...
def isinstance(self, generic: GenericUnitDescriptor) -> bool:
"""
Returns True if the UnitDescriptor is an instance of the generic, False
otherwise.
"""

def to_generic(self) -> GenericUnitDescriptor: ...
def to_generic(self) -> GenericUnitDescriptor:
"""
Create a generic descriptor from this UnitDescriptor.
"""

def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension": ...

Expand All @@ -76,7 +83,7 @@ class MeasurementUnitMeta(EnumMeta):
GenericUnitDescriptor(s).
"""

def __mul__(unit_cls, other: GenericUnitDescriptor) -> "GenericCompositeDimension":
def __mul__(cls, other: GenericUnitDescriptor) -> "GenericCompositeDimension":
"""
Defines multiplication between MeasurementUnit types and other generic
descriptors.
Expand All @@ -88,28 +95,22 @@ def __mul__(unit_cls, other: GenericUnitDescriptor) -> "GenericCompositeDimensio
if isinstance(other, GenericCompositeDimension):
numerator = other.numerator.copy()
denominator = other.denominator.copy()
numerator.append(GenericDimension(unit_cls))
numerator.append(GenericDimension(cls))
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)
elif isinstance(other, GenericDimension):
return GenericCompositeDimension(
numerator=[GenericDimension(unit_cls), other]
)
elif type(other) == MeasurementUnitType:
if isinstance(other, GenericDimension):
return GenericCompositeDimension(numerator=[GenericDimension(cls), other])
if isinstance(other, MeasurementUnitType):
return GenericCompositeDimension(
numerator=[
GenericDimension(unit_cls),
GenericDimension(cls),
GenericDimension(other),
]
)
raise InvalidDescriptorBinaryOperation(
f"cannot multiply {unit_cls} with {other}. "
)
raise InvalidDescriptorBinaryOperation(f"cannot multiply {cls} with {other}. ")

def __truediv__(
unit_cls, other: GenericUnitDescriptor
) -> "GenericCompositeDimension":
def __truediv__(cls, other: GenericUnitDescriptor) -> "GenericCompositeDimension":
"""
Defines division between MeasurementUnit types and other generic
descriptors.
Expand All @@ -121,37 +122,35 @@ def __truediv__(
if isinstance(other, GenericCompositeDimension):
numerator = other.denominator.copy()
denominator = other.numerator.copy()
numerator.append(GenericDimension(unit_cls))
numerator.append(GenericDimension(cls))
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)
elif isinstance(other, GenericDimension):
if isinstance(other, GenericDimension):
return GenericCompositeDimension(
numerator=[GenericDimension(unit_cls)], denominator=[other]
numerator=[GenericDimension(cls)], denominator=[other]
)
elif type(other) == MeasurementUnitType:
if isinstance(other, MeasurementUnitType):
return GenericCompositeDimension(
numerator=[GenericDimension(unit_cls)],
numerator=[GenericDimension(cls)],
denominator=[GenericDimension(other)],
)
raise InvalidDescriptorBinaryOperation(
f"cannot divide {unit_cls} with {other}. "
)
raise InvalidDescriptorBinaryOperation(f"cannot divide {cls} with {other}. ")

def __pow__(unit_cls, power: float) -> "GenericDimension":
def __pow__(cls, power: float) -> "GenericDimension":
"""
Defines exponentiation of MeasurementUnit types.
>>> class TimeUnit(MeasurementUnit): ...
>>> assert type(TimeUnit**3) == GenericDimension
"""
return GenericDimension(unit_cls, power)
return GenericDimension(cls, power)

def __str__(self) -> str:
return f"<MeasurementUnit: {self.__name__}>"
def __str__(cls) -> str:
return f"<MeasurementUnit: {cls.__name__}>"

def __repr__(self) -> str:
return str(self)
def __repr__(cls) -> str:
return str(cls)


class MeasurementUnit(Enum, metaclass=MeasurementUnitMeta):
Expand Down Expand Up @@ -196,7 +195,7 @@ def from_descriptor(descriptor: UnitDescriptor) -> "MeasurementUnit":
"""
if isinstance(descriptor, Dimension):
return descriptor.unit
elif isinstance(descriptor, MeasurementUnit):
if isinstance(descriptor, MeasurementUnit):
return descriptor
raise WrongUnitDescriptorType(
f"cannot create MeasurementUnit from descriptor: {descriptor}"
Expand All @@ -219,7 +218,7 @@ def isinstance(self, generic: GenericUnitDescriptor) -> bool:
>>> TemperatureUnit.CELCIUS.isinstance(LengthUnit)
False
"""
return type(self) == generic
return type(self) == generic # pylint: disable=unidiomatic-typecheck

def to_generic(self) -> GenericUnitDescriptor:
"""
Expand All @@ -245,7 +244,7 @@ def __mul__(self, descriptor: UnitDescriptor) -> "CompositeDimension":
"""
if isinstance(descriptor, MeasurementUnit):
return Dimension(self) * Dimension(descriptor)
elif isinstance(descriptor, (Dimension, CompositeDimension)):
if isinstance(descriptor, (Dimension, CompositeDimension)):
return Dimension(self) * descriptor
raise InvalidDescriptorBinaryOperation(
f"cannot multiply {self} with {descriptor}. "
Expand All @@ -263,7 +262,7 @@ def __truediv__(self, descriptor: UnitDescriptor) -> "CompositeDimension":
"""
if isinstance(descriptor, MeasurementUnit):
return Dimension(self) / Dimension(descriptor)
elif isinstance(descriptor, (Dimension, CompositeDimension)):
if isinstance(descriptor, (Dimension, CompositeDimension)):
return Dimension(self) / descriptor
raise InvalidDescriptorBinaryOperation(
f"cannot divide {self} with {descriptor}. "
Expand Down Expand Up @@ -342,7 +341,7 @@ def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit:
descriptor.unit, AliasMeasurementUnit
):
return descriptor.unit
elif isinstance(descriptor, AliasMeasurementUnit):
if isinstance(descriptor, AliasMeasurementUnit):
return descriptor
raise WrongUnitDescriptorType(
f"cannot create AliasMeasurementUnit from descriptor {descriptor}"
Expand Down Expand Up @@ -379,7 +378,7 @@ class GenericDimension:
power: float = 1

def __init__(self, unit_type: MeasurementUnitType, power: float = 1) -> None:
if not (isinstance(power, float) or isinstance(power, int)):
if not isinstance(power, (float, int)):
raise InvalidDescriptorExponent
self.unit_type = unit_type
self.power = power
Expand All @@ -400,9 +399,9 @@ def __mul__(self, generic: GenericUnitDescriptor) -> "GenericCompositeDimension"
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)
elif isinstance(generic, GenericDimension):
if isinstance(generic, GenericDimension):
return GenericCompositeDimension(numerator=[self, generic])
elif type(generic) == MeasurementUnitType:
if isinstance(generic, MeasurementUnitType):
return GenericCompositeDimension(
numerator=[self, GenericDimension(generic)]
)
Expand All @@ -427,9 +426,9 @@ def __truediv__(
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)
elif isinstance(generic, GenericDimension):
if isinstance(generic, GenericDimension):
return GenericCompositeDimension(numerator=[self], denominator=[generic])
elif type(generic) == MeasurementUnitType:
if isinstance(generic, MeasurementUnitType):
return GenericCompositeDimension(
numerator=[self], denominator=[GenericDimension(generic)]
)
Expand All @@ -442,7 +441,7 @@ def __pow__(self, power: float) -> "GenericDimension":
>>> class TimeUnit(MeasurementUnit): ...
>>> assert type((TimeUnit**2)**3) == GenericDimension
"""
if not (isinstance(power, float) or isinstance(power, int)):
if not isinstance(power, (float, int)):
raise InvalidDescriptorExponent
self.power *= power
return self
Expand Down Expand Up @@ -491,7 +490,7 @@ class Dimension:
power: float = 1

def __init__(self, unit: MeasurementUnit, power: float = 1) -> None:
if not (isinstance(power, float) or isinstance(power, int)):
if not isinstance(power, (float, int)):
raise InvalidDescriptorExponent
self.unit = unit
self.power = power
Expand All @@ -510,7 +509,7 @@ def from_descriptor(descriptor: UnitDescriptor) -> "Dimension":
"""
if isinstance(descriptor, Dimension):
return descriptor
elif isinstance(descriptor, MeasurementUnit):
if isinstance(descriptor, MeasurementUnit):
return Dimension(descriptor)
raise WrongUnitDescriptorType(
f"cannot create Dimension from descriptor: {descriptor}"
Expand All @@ -530,7 +529,7 @@ def isinstance(self, generic: GenericUnitDescriptor) -> bool:
>>> Dimension(TemperatureUnit.CELCIUS).isinstance(TemperatureUnit**2)
False
"""
if type(generic) == MeasurementUnitType:
if isinstance(generic, MeasurementUnitType):
generic = GenericDimension(generic)
if not isinstance(generic, GenericDimension):
return False
Expand Down Expand Up @@ -565,9 +564,9 @@ def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
denominator = descriptor.denominator.copy()
numerator.append(self)
return CompositeDimension(numerator=numerator, denominator=denominator)
elif isinstance(descriptor, Dimension):
if isinstance(descriptor, Dimension):
return CompositeDimension(numerator=[self, descriptor])
elif isinstance(descriptor, MeasurementUnit):
if isinstance(descriptor, MeasurementUnit):
return CompositeDimension(numerator=[self, Dimension(descriptor)])
raise InvalidDescriptorBinaryOperation(
f"cannot multiply {self} with {descriptor}. "
Expand All @@ -588,9 +587,9 @@ def __truediv__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
denominator = descriptor.numerator.copy()
numerator.append(self)
return CompositeDimension(numerator=numerator, denominator=denominator)
elif isinstance(descriptor, Dimension):
if isinstance(descriptor, Dimension):
return CompositeDimension(numerator=[self], denominator=[descriptor])
elif isinstance(descriptor, MeasurementUnit):
if isinstance(descriptor, MeasurementUnit):
return CompositeDimension(
numerator=[self], denominator=[Dimension(descriptor)]
)
Expand All @@ -606,7 +605,7 @@ def __pow__(self, power: float) -> "Dimension":
... SECOND = "s"
>>> assert type((TimeUnit.SECOND**2)**3) == Dimension
"""
if not (isinstance(power, float) or isinstance(power, int)):
if not isinstance(power, (float, int)):
raise InvalidDescriptorExponent
self.power *= power
return self
Expand Down Expand Up @@ -679,13 +678,13 @@ def __mul__(self, generic: GenericUnitDescriptor) -> "GenericCompositeDimension"
numerator=numerator, denominator=denominator
)

elif isinstance(generic, GenericDimension):
if isinstance(generic, GenericDimension):
numerator.append(generic)
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)

elif type(generic) == MeasurementUnitType:
if isinstance(generic, MeasurementUnitType):
numerator.append(GenericDimension(generic))
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
Expand Down Expand Up @@ -714,12 +713,12 @@ def __truediv__(
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)
elif isinstance(generic, GenericDimension):
if isinstance(generic, GenericDimension):
denominator.append(generic)
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
)
elif type(generic) == MeasurementUnitType:
if isinstance(generic, MeasurementUnitType):
denominator.append(GenericDimension(generic))
return GenericCompositeDimension(
numerator=numerator, denominator=denominator
Expand Down Expand Up @@ -966,10 +965,10 @@ def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
numerator.extend(descriptor.numerator)
denominator.extend(descriptor.denominator)
return CompositeDimension(numerator=numerator, denominator=denominator)
elif isinstance(descriptor, Dimension):
if isinstance(descriptor, Dimension):
numerator.append(descriptor)
return CompositeDimension(numerator=numerator, denominator=denominator)
elif isinstance(descriptor, MeasurementUnit):
if isinstance(descriptor, MeasurementUnit):
numerator.append(Dimension(descriptor))
return CompositeDimension(numerator=numerator, denominator=denominator)
raise InvalidDescriptorBinaryOperation(
Expand All @@ -994,10 +993,10 @@ def __truediv__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
numerator.extend(descriptor.denominator)
denominator.extend(descriptor.numerator)
return CompositeDimension(numerator=numerator, denominator=denominator)
elif isinstance(descriptor, Dimension):
if isinstance(descriptor, Dimension):
denominator.append(descriptor)
return CompositeDimension(numerator=numerator, denominator=denominator)
elif isinstance(descriptor, MeasurementUnit):
if isinstance(descriptor, MeasurementUnit):
denominator.append(Dimension(descriptor))
return CompositeDimension(numerator=numerator, denominator=denominator)
raise InvalidDescriptorBinaryOperation(
Expand Down
2 changes: 1 addition & 1 deletion src/property_utils/units/units.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# pylint: disable=missing-class-docstring
from property_utils.units.descriptors import (
MeasurementUnit,
AliasMeasurementUnit,
GenericCompositeDimension,
UnitDescriptor,
)


Expand Down

0 comments on commit 410f9c6

Please sign in to comment.