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

Comparing Quantity to list yields unhandled IndexError: list index out of range #783

Closed
alexbhandari opened this issue Mar 26, 2019 · 4 comments

Comments

@alexbhandari
Copy link
Contributor

alexbhandari commented Mar 26, 2019

To replicate run

import pint
ureg = pint.UnitRegistry()
ureg('g') == []

results in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alexbhandari-young/miniconda3/envs/django/lib/python3.7/site-packages/pint/quantity.py", line 73, in wrapped
    elif isinstance(other, list) and isinstance(other[0], type(self)):
IndexError: list index out of range

This is the case for comparing any Quantity with a list. Seems to be caused by the other[0] access in the check_implemented method used by Quantity.

@alexbhandari
Copy link
Contributor Author

I believe this is fixed by adding

elif isinstance(other, list) and other and isinstance(other[0], type(self)):

on line 73 in quantity.py

alexbhandari added a commit to alexbhandari/pint that referenced this issue Mar 26, 2019
@fedelaport
Copy link

I have similar problems here. I use extensively empty lists:

[] * ureg.mm

and now throws:

Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/braced-alB5xfao/lib/python3.6/site-packages/pint/unit.py", line 152, in __mul__
    return self._REGISTRY.Quantity(1, self._units) * other
  File "/root/.local/share/virtualenvs/braced-alB5xfao/lib/python3.6/site-packages/pint/quantity.py", line 900, in __mul__
    return self._mul_div(other, operator.mul)
  File "/root/.local/share/virtualenvs/braced-alB5xfao/lib/python3.6/site-packages/pint/quantity.py", line 73, in wrapped
    elif isinstance(other, list) and isinstance(other[0], type(self)):
IndexError: list index out of range

@hgrecco
Copy link
Owner

hgrecco commented Apr 11, 2019

I have just merge a PR. Let me know if this works

@fedelaport
Copy link

Works for me. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants