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

nb.optional doesn't work with nb.types.NamedTuple #9607

Open
ocmob opened this issue Jun 6, 2024 · 5 comments
Open

nb.optional doesn't work with nb.types.NamedTuple #9607

ocmob opened this issue Jun 6, 2024 · 5 comments

Comments

@ocmob
Copy link

ocmob commented Jun 6, 2024

It seems like nb.optional doesn't work with nb.types.NamedTuple (it does with nb.types.Tuple, though). Here is the minimum repro:

import numba as nb
from typing import NamedTuple

class Params(NamedTuple):
    param1: float
    param2: int

ParamsNumbaType = nb.types.NamedTuple((nb.f8, nb.i8), Params)

@nb.njit(ParamsNumbaType(ParamsNumbaType))
def func(my_tup: Params):
    return my_tup

tup = Params(3., 3)
func(tup)  # Works

class ParamsOptional(NamedTuple):
    param1: float
    param2: int | None = None

ParamsOptionalNumbaType = nb.types.NamedTuple((nb.f8, nb.optional(nb.i8)), ParamsOptional)

@nb.njit(ParamsOptionalNumbaType(ParamsOptionalNumbaType))
def func2(my_tup: ParamsOptional):
    return my_tup

tup = ParamsOptional(3., 3)
func2(tup)  # Fails
@ocmob ocmob changed the title Similar attribute names in nb.types.NamedTuple cause crashes nb.optional doesn't work with nb.types.NamedTuple Jun 6, 2024
@esc
Copy link
Member

esc commented Jun 7, 2024

@ocmob thank you for reporting this. I have updated the reproducer to include the import numba as nb.

@esc
Copy link
Member

esc commented Jun 7, 2024

The error message is

 💣 zsh» python issue_9607.py
Traceback (most recent call last):
  File "/Users/esc/git/numba/issue_9607.py", line 28, in <module>
    func2(tup)  # Fails
    ^^^^^^^^^^
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 658, in _explain_matching_error
    raise TypeError(msg)
TypeError: No matching definition for argument type(s) ParamsOptional(float64, int64)

@esc esc added the bug - typing Bugs: occuring at typing time label Jun 7, 2024
@esc
Copy link
Member

esc commented Jun 7, 2024

@ocmob looks like I can reproduce this. I am provisionally labelling this as a typing bug until a second maintainer can verify it, thank you for reporting.

@sklam
Copy link
Member

sklam commented Jun 11, 2024

I'd classify this as a feature request to expand support to typing.NamedTuple. All tuple/namedtuple support predates that.

@sklam sklam removed the bug - typing Bugs: occuring at typing time label Jun 11, 2024
@sklam
Copy link
Member

sklam commented Jun 11, 2024

Btw, the problem is happening at unboxing. Numba needs to read into the annotations in ParamsOptional to recognize that the second field should be a optional type.

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

No branches or pull requests

3 participants