hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.4k stars 473 forks source link

overloading of copy() breaks without numpy. #777

Open jettero opened 5 years ago

jettero commented 5 years ago

This is a pretty trivial problem, but I lost about an hour of my life to tracking it down. If you try to copy a Quantity when numpy isn't in play, the overloading breaks.

If you're curious why I ever noticed this, I'm sub-classing Quantity and fiddling with the internals of the Quantity class a little to allow for some improper operations that a human might expect to work in some natural language processing I'm messing around with.

In my test virtualenv, it never occurred to me to install numpy, but I happened to have it installed for something else in my regular dev environment.

With numpy:

In [3]: import pint
   ...: x = pint.UnitRegistry().m * 3
   ...: y = x.copy()
   ...: assert x is not y
   ...: assert x == y

In [4]: 

Without numpy:

[ins] In [5]: import pint
         ...: x = pint.UnitRegistry().m * 3
         ...: y = x.copy()
         ...: assert x is not y
         ...: assert x == y
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-8971ee710317> in <module>
      1 import pint
      2 x = pint.UnitRegistry().m * 3
----> 3 y = x.copy()
      4 assert x is not y
      5 assert x == y

~/code/python/space/venv/lib/python3.6/site-packages/pint/quantity.py in __getattr__(self, item)
   1386             if not isinstance(self._magnitude, ndarray):
   1387                 self._magnitude = _to_magnitude(self._magnitude, True)
-> 1388             attr = getattr(self._magnitude, item)
   1389             if callable(attr):
   1390                 return functools.partial(self.__numpy_method_wrap, attr)

AttributeError: 'int' object has no attribute 'copy'
jettero commented 5 years ago

I may try to submit a pull request for a possible fix and test. Normally I'd submit a PR instead of an issue, but I'm in a rush (for the moment) today.