Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap converts strings #723

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/wrapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ Or in the decorator format:
Use None to skip conversion of any given element.
- **strict**: if `True` all convertible arguments must be a Quantity
and others will raise a ValueError (True by default)
`strict` also enables implicit string conversion. If
a string can be converted to a Quantity, it will be.



Strict Mode
-----------

By default, the function is wrapped in `strict` mode. In this mode,
the input arguments assigned to units must be a Quantities.
the input arguments assigned to units must be Quantities.

.. doctest::

Expand All @@ -100,6 +102,13 @@ the input arguments assigned to units must be a Quantities.
...
ValueError: A wrapped function using strict=True requires quantity for all arguments with not None units. (error found for meter, 1.0)

Strict mode also enables automatic conversion of strings into Quantities when possible.

.. doctest::

>>> mypp('1 m')
<Quantity(2.0064092925890407, 'second')>

To enable using non-Quantity numerical values, set strict to False`.

.. doctest::
Expand Down
15 changes: 12 additions & 3 deletions pint/registry_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,19 @@ def _converter(ureg, values, strict):
new_values[ndx] = ureg._convert(values[ndx]._magnitude,
values[ndx]._units,
args_as_uc[ndx][0])

# If the input is a string and strict is active, convert it to a quantity.
elif string_types in type(values[ndx]).mro() and strict:
implicit_quantity = ureg.Quantity(values[ndx])
new_values[ndx] = ureg._convert(implicit_quantity._magnitude,
implicit_quantity._units,
args_as_uc[ndx][0])


else:
if strict:
raise ValueError('A wrapped function using strict=True requires '
'quantity for all arguments with not None units. '
'quantity or string for all arguments with not None units. '
'(error found for {}, {})'.format(args_as_uc[ndx][0], new_values[ndx]))

return new_values, values_by_name
Expand All @@ -143,7 +152,7 @@ def _apply_defaults(func, args, kwargs):
if param.name not in bound_arguments.arguments:
bound_arguments.arguments[param.name] = param.default
args = [bound_arguments.arguments[key] for key in sig.parameters.keys()]
return args, {}
return args, {}

def wraps(ureg, ret, args, strict=True):
"""Wraps a function to become pint-aware.
Expand Down Expand Up @@ -186,7 +195,7 @@ def decorator(func):
def wrapper(*values, **kw):

values, kw = _apply_defaults(func, values, kw)

# In principle, the values are used as is
# When then extract the magnitudes when needed.
new_values, values_by_name = converter(ureg, values, strict)
Expand Down
13 changes: 13 additions & 0 deletions pint/testsuite/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,19 @@ def func(x):
self.assertEqual(f3(3. * ureg.centimeter), 0.03 * ureg.centimeter)
self.assertEqual(f3(3. * ureg.meter), 3. * ureg.centimeter)

# Test string conversion when strict=True.
f3 = ureg.wraps('centimeter', ['meter', ], strict=True)(func)
self.assertEqual(f3('3 cm'), .03 * ureg.centimeter)
self.assertEqual(f3('3 m'), 3. * ureg.centimeter)
# Test that string conversion also rejects incorrect units.
self.assertRaises(ValueError, f3, '3 s')

# Test that string conversion fails when strict=False
f3 = ureg.wraps('centimeter', ['meter', ], strict=False)(func)
# self.assertRaises(ValueError, f3, '3 cm')
self.assertEqual(f3('3 cm'), '3 cm' * ureg.centimeter)
# self.assertEqual(f3('3 m'), 3. * ureg.centimeter)

def gfunc(x, y):
return x + y

Expand Down