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

"E1130: bad operand type for unary -: ArrayImpl (invalid-unary-operand-type)" pylint errors in latest release (0.4.24) #19713

Closed
fabianp opened this issue Feb 8, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@fabianp
Copy link
Contributor

fabianp commented Feb 8, 2024

Description

Running pylint (pylint test.py) on this simple file

"""Test file for 'E1130: bad operand type for unary -:'."""
from jax import numpy as jnp

a = -jnp.ones([])

fails with the latest release of jax (0.4.24) :

❯ pylint test.py
************* Module test
test.py:4:4: E1130: bad operand type for unary -: ArrayImpl (invalid-unary-operand-type)

-------------------------------------

The same code doesn't raise any error with jax 0.4.23.

This has led to some tests failures in other packages such as optax

What jax/jaxlib version are you using?

0.4.24 0.4.24

Which accelerator(s) are you using?

CPU

Additional system info?

Tried on both linux and mac

NVIDIA GPU info

No response

@fabianp fabianp added the bug Something isn't working label Feb 8, 2024
@jakevdp
Copy link
Collaborator

jakevdp commented Feb 8, 2024

Hi - thanks for the report. Do you understand where pytype is inferring that ones returns ArrayImpl? The pyi file has not changed, and declares that ones returns Array unconditionally: https://github.com/google/jax/blob/4c505f8bac45517970149b13343988090fdaa920/jax/numpy/__init__.pyi#L610-L611

I just double-checked that the pyi file is included in the v0.4.24 distribution, so I'm not sure why pytype would be ignoring it. Do you have any ideas?

@fabianp
Copy link
Contributor Author

fabianp commented Feb 8, 2024

Thanks Jake for the prompt reply.

No, I don't understand why pylint is doing that inference. A couple of more observations:

  • changing -jnp.ones([]) to jnp.negative(jnp.ones([])) works fine (does not raise the error)
  • I don't think the error is specific to jnp.ones. In particular, -jnp.full([], 2.) has the same issue

fabianp added a commit to fabianp/optax that referenced this issue Feb 8, 2024
Workaround for jax-ml/jax#19713
fabianp added a commit to fabianp/optax that referenced this issue Feb 8, 2024
Workaround for jax-ml/jax#19713
fabianp added a commit to fabianp/optax that referenced this issue Feb 8, 2024
Workaround for jax-ml/jax#19713
@jakevdp
Copy link
Collaborator

jakevdp commented Feb 8, 2024

Chatting with @superbobry, it seems this is coming from pylint, not pytype. pylint has some level of type checking built-in, but it seems that it ignores pyi files and thus will not work correctly with dynamically-defined attributes. Here's a repro:

$ ls
mymodule.py  mymodule.pyi  test.py

$ cat mymodule.py
class Foo:
  pass
setattr(Foo, "__neg__", lambda self: self)

$ cat mymodule.pyi
class Foo:
  def __neg__(self) -> Foo: ...

$ cat test.py
"""Test file for incorrect E1130"""
from mymodule import Foo
result = -Foo()

$ mypy test.py
Success: no issues found in 1 source file

$ pylint test.py
************* Module test
test.py:3:9: E1130: bad operand type for unary -: Foo (invalid-unary-operand-type)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

So in summary, this looks like a bug in pylint – it's trying to do type checking without taking into account the interface files that we've defined for the sake of type checking.

What do you think?

@jakevdp jakevdp changed the title "E1130: bad operand type for unary -: ArrayImpl (invalid-unary-operand-type)" pytype errors in latest release (0.4.24) "E1130: bad operand type for unary -: ArrayImpl (invalid-unary-operand-type)" pylint errors in latest release (0.4.24) Feb 8, 2024
@fabianp
Copy link
Contributor Author

fabianp commented Feb 8, 2024

Thanks @jakevdp for reporting this upstream! I'll close the issue here since it does seem more of an issue in pylint

@fabianp fabianp closed this as completed Feb 8, 2024
WangHanSolo pushed a commit to WangHanSolo/optax that referenced this issue Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants