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

isinstance(None, numba.types.NoneType) is False #3590

Closed
david-zwicker opened this issue Dec 12, 2018 · 5 comments
Closed

isinstance(None, numba.types.NoneType) is False #3590

david-zwicker opened this issue Dec 12, 2018 · 5 comments
Labels
question Notes an issue as a question

Comments

@david-zwicker
Copy link

While playing with generated_jit I stumbled upon this unexpected behavior:

>>> isinstance(None, numba.types.NoneType)

False

Clearly, standard python disagrees with this:

>>> isinstance(None, type(None))

True

Is there a rational for this behavior or is this simply a bug?

@stuartarchibald
Copy link
Contributor

Thanks for raising this. I think this is because Numba's type system mimics that of Python/NumPy but does not inherit from it (kind of indicated by MRO)

In [8]: numba.types.NoneType.__mro__                                                                                            
Out[8]: 
(numba.types.misc.NoneType,
 numba.types.common.Opaque,
 numba.types.abstract.Dummy,
 numba.types.abstract.Type,
 object)

Equally isinstance(int, numba.types.Integer) is False.

Relating this to @generated_jit, type inference will first try and compile the function with anything that can be represented as a literal value represented as such (integers, strings, slices and make_function [note: None is not in this set of types]), if this fails it will try to compile the function with everything represented by the Numba type system types. The 0.41.0 Release demo notebook (Numba_041_release_demo.ipynb) at https://mybinder.org/v2/gh/numba/numba-examples/master?filepath=notebooks has a demonstration under the Literal value support section.

Another user asked a similar/related question yesterday, an example is also available there #3585 (comment)

Hope this helps?

@stuartarchibald stuartarchibald added the question Notes an issue as a question label Dec 12, 2018
@david-zwicker
Copy link
Author

I don't quite get the analogy between isinstance(int, numba.types.Integer) and the original problem with None. I though int is a type itself and would therefore not be expected to be an instance of Integer. However, I interpret None as a (single) instance of NoneType and would thus expect the isinstance check to return True.

@stuartarchibald
Copy link
Contributor

Apologies, that's an error, should read something like "isinstance(1, numba.types.Integer) also returns False", same reason as above applies.

@stuartarchibald
Copy link
Contributor

stuartarchibald commented Mar 14, 2019

@david-zwicker you may be interested in the For Developers section of this notebook relating to the 0.43 release https://github.com/numba/numba-examples/blob/master/notebooks/Numba_043_release_demo.ipynb

@stuartarchibald
Copy link
Contributor

Closing this question as it seems to be resolved. Numba now has a discourse forum https://numba.discourse.group/ which is great for questions like this, please do consider posting there in future :) Thanks!

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

No branches or pull requests

2 participants