Implement getattr builtin.#7884
Conversation
As title.
| def impl(obj, name, default): | ||
| try: | ||
| return resolve_getattr(obj, name) | ||
| except Exception: |
There was a problem hiding this comment.
This is not strictly correct, it should only accept AttributeError, but try-except support cannot handle this. Given the only place an exception can be raised is from the intrinsic resolve_getattr and Numba controls this I'm reasonably sure that this is safe.
There was a problem hiding this comment.
Worth leaving this note into the code as comment
There was a problem hiding this comment.
I removed the exception handling impl in 5886a8c as it was causing the OptionalType problem noted below.
|
I got a test failure when I run |
|
Nevermind, the problem exist on |
sklam
left a comment
There was a problem hiding this comment.
Looks good.
I'll do a second pass review to try breaking it.
What was the issue and is there a ticket tracking it? |
sklam
left a comment
There was a problem hiding this comment.
Also, the 3-arg version of getattr has unexpected typing for the return type. (This is very likely a problem beyond this PR):
from numba import njit
import numpy as np
@njit
def foo(obj, name):
return getattr(obj, name)
@njit
def fake_hash():
return 12345
@njit
def bar():
ndim = foo(np.ones(1), "ndim")
hasher = foo("a_string", "__hash__")
print(ndim, hasher())
array_hash = getattr(np.ones(1), "__hash__", fake_hash) # this is ok
print(array_hash()) # optional type?
# No type info available for OptionalType(type(CPUDispatcher(<function fake_hash at 0x7fe36843e430>))) as a callable.
# During: resolving callee type: OptionalType(type(CPUDispatcher(<function fake_hash at 0x7fe36843e430>)))
#
# File "chk.py", line 20:
# def bar():
# <source elided>
# array_hash = getattr(np.ones(1), "__hash__", fake_hash)
# print(array_hash())
# ^
bar()| * :func:`enumerate` | ||
| * :func:`filter` | ||
| * :class:`float` | ||
| * :func:`getattr` |
There was a problem hiding this comment.
Should there be note saying that this is not a complete support? e.g. getattr(numpy, array) is not supported because it returns a Function object.
There was a problem hiding this comment.
Also, the name must be a literal string not passed from Python.
Example:
from numba import njit
import numpy as np
@njit
def foo(name):
a = np.ones(10)
fn = getattr(a, name)
print(fn)
@njit
def bar():
foo("ndim")
bar() # ok
foo("ndim") # not okThere was a problem hiding this comment.
Both the literal string requirement and the lack of support for Function type are noted in the docs as part of 5886a8c.
Added #7891 |
I've got a fix for this which involves making the intrinsic implementation always take 3 args, and then in the "no default" case a specific dummy |
As title. Also updates the docs to note restrictions on what is supported.
To resolve conflicts in: numba/tests/test_builtins.py
|
Conflicts resolved in 5151bbd. |
|
5886a8c is smart |
sklam
left a comment
There was a problem hiding this comment.
Just one more merge conflict to resolve.
Otherwise, everything looks good.
Resolves conflicts in: numba/tests/test_builtins.py The conflict was in imports from numba.tests.support.
Thanks for the review, conflict should be resolved in 3d9981d. |
As title.