Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testRaising():
with pytest.raises(TypeError):
CheckType("hellou", int)
with pytest.raises(TypeError):
CheckType("hellou", (int, float))
with pytest.raises(TypeError):
CheckType(99, (str, float))
def testIfCustomMessageIsAppendedToDefaultMessage():
message = "Zero is not unicode!"
with pytest.raises(TypeError) as exception:
CheckType(0, str, message)
assert message in str(exception.value)
def testRaising():
with pytest.raises(TypeError):
CheckType("hellou", int)
with pytest.raises(TypeError):
CheckType("hellou", (int, float))
with pytest.raises(TypeError):
CheckType(99, (str, float))
def testPassing():
class Foo:
pass
CheckType(Foo(), Foo)
CheckType(Foo(), (int, Foo))
CheckType(99, (int, Foo))
def SetNumber(self, number):
"""
Sets the number part.
:type number: float or int
:param number:
The integer part.
"""
CheckType(number, (int, float))
self._number = number
def SetFraction(self, fraction):
"""
Sets the fractional part of this object.
:type fraction: Fraction or tuple(float, float)
:param fraction:
The fraction for this FractionValue object. A pair (numerator, denominator)
is also accepted.
"""
CheckType(fraction, (Fraction, tuple))
# convert a tuple
if isinstance(fraction, tuple):
if len(fraction) != 2:
raise ValueError(f"Expected a tuple (numerator, denominator), got {fraction!r}")
fraction = Fraction(*fraction)
self._fraction = fraction
The quantity of this scalar.
:param FractionValue value:
The initial value
"""
# Considering fraction values values are easily coerced from float values (though it is
# important to note the opposite is not true) if the input value is not a fraction already
# try to convert value to float. This also makes this subclass SetValue interface compatible
# with superclass interface.
try:
if type(value) != FractionValue:
value = FractionValue(number=float(value))
except Exception:
# If not a fraction and coercion to float fails, use CheckType to provide a better error
# message.
CheckType(value, (FractionValue, float))
self._value = value
self._quantity = quantity
self._unit_database = unit_database or UnitDatabase.GetSingleton()
:param bool is_min_exclusive:
If the min_value given is exclusive.
:param bool is_max_exclusive:
If the max_value given is exclusive.
:param str caption:
User friendly caption for this category.
:param str from_category:
Category to copy other parameters from.
:raises UnitsError:
If the category was already added and override is not set to True
"""
CheckType(category, str)
if from_category and quantity_type:
raise ValueError("cannot pass both quantity_type and from_category")
if not override and category in self.categories_to_quantity_types:
raise UnitsError("category %r already registered" % category)
if min_value is not None and max_value is not None:
if max_value < min_value:
raise ValueError(
"min_value (%s) must be >= than min_value (%s)" % (min_value, max_value)
)
if from_category:
category_info = self.GetCategoryInfo(from_category)
quantity_type = category_info.quantity_type