Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Implement getattr builtin.#7884

Merged
sklam merged 4 commits into
numba:mainfrom
stuartarchibald:wip/builtin_getattr
Aug 3, 2022
Merged

Implement getattr builtin.#7884
sklam merged 4 commits into
numba:mainfrom
stuartarchibald:wip/builtin_getattr

Conversation

@stuartarchibald
Copy link
Copy Markdown
Contributor

As title.

Comment thread numba/cpython/builtins.py Outdated
def impl(obj, name, default):
try:
return resolve_getattr(obj, name)
except Exception:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth leaving this note into the code as comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the exception handling impl in 5886a8c as it was causing the OptionalType problem noted below.

@sklam
Copy link
Copy Markdown
Member

sklam commented Mar 4, 2022

I got a test failure when I run python runtests.py numba/tests/test_builtins.py

FAIL: test_experimental_warning (numba.tests.test_builtins.TestIsinstanceBuiltin)

@sklam
Copy link
Copy Markdown
Member

sklam commented Mar 4, 2022

Nevermind, the problem exist on main

Copy link
Copy Markdown
Member

@sklam sklam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

I'll do a second pass review to try breaking it.

@stuartarchibald
Copy link
Copy Markdown
Contributor Author

Nevermind, the problem exist on main

What was the issue and is there a ticket tracking it?

Copy link
Copy Markdown
Member

@sklam sklam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Comment thread docs/source/reference/pysupported.rst Outdated
* :func:`enumerate`
* :func:`filter`
* :class:`float`
* :func:`getattr`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ok

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the literal string requirement and the lack of support for Function type are noted in the docs as part of 5886a8c.

@sklam
Copy link
Copy Markdown
Member

sklam commented Mar 7, 2022

Nevermind, the problem exist on main

What was the issue and is there a ticket tracking it?

Added #7891

@stuartarchibald
Copy link
Copy Markdown
Contributor Author

Also, the 3-arg version of getattr has unexpected typing for the return type. (This is very likely a problem beyond this PR):

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 namedtuple is used as a marker to signal that no default has been supplied. I think this preserves the semantics and avoids the OptionalType problem. Does that sound acceptable?

As title.

Also updates the docs to note restrictions on what is supported.
@stuartarchibald stuartarchibald added 4 - Waiting on reviewer Waiting for reviewer to respond to author and removed 4 - Waiting on author Waiting for author to respond to review labels Mar 17, 2022
To resolve conflicts in:
	numba/tests/test_builtins.py
@stuartarchibald
Copy link
Copy Markdown
Contributor Author

Conflicts resolved in 5151bbd.

@sklam
Copy link
Copy Markdown
Member

sklam commented Jul 28, 2022

5886a8c is smart

sklam
sklam previously approved these changes Jul 28, 2022
Copy link
Copy Markdown
Member

@sklam sklam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one more merge conflict to resolve.
Otherwise, everything looks good.

@sklam sklam added this to the Numba 0.57 RC milestone Jul 28, 2022
@sklam sklam self-assigned this Jul 28, 2022
@sklam sklam added 4 - Waiting on author Waiting for author to respond to review and removed 4 - Waiting on reviewer Waiting for reviewer to respond to author labels Jul 28, 2022
Resolves conflicts in:
	numba/tests/test_builtins.py

The conflict was in imports from numba.tests.support.
@stuartarchibald
Copy link
Copy Markdown
Contributor Author

Just one more merge conflict to resolve. Otherwise, everything looks good.

Thanks for the review, conflict should be resolved in 3d9981d.

@stuartarchibald stuartarchibald added 4 - Waiting on reviewer Waiting for reviewer to respond to author and removed 4 - Waiting on author Waiting for author to respond to review labels Jul 29, 2022
@sklam sklam added 5 - Ready to merge Review and testing done, is ready to merge and removed 4 - Waiting on reviewer Waiting for reviewer to respond to author labels Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge Effort - medium Medium size effort needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants