Skip to content

ENH: Adding default_xp context manager for xp_assert functions#747

Open
prady0t wants to merge 1 commit into
data-apis:mainfrom
prady0t:xp-parameter
Open

ENH: Adding default_xp context manager for xp_assert functions#747
prady0t wants to merge 1 commit into
data-apis:mainfrom
prady0t:xp-parameter

Conversation

@prady0t
Copy link
Copy Markdown
Contributor

@prady0t prady0t commented May 18, 2026

No description provided.

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
@prady0t prady0t changed the title Adding default_xp context manager for xp_assert functions ENH: Adding default_xp context manager for xp_assert functions May 18, 2026
Copy link
Copy Markdown
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

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

thanks Pradyot! Could you update the SciPy branch to use this branch?

@prady0t
Copy link
Copy Markdown
Contributor Author

prady0t commented May 19, 2026

Could you update the SciPy branch to use this branch?

Will have to merge this first right?

@lucascolley
Copy link
Copy Markdown
Member

Could you update the SciPy branch to use this branch?

Will have to merge this first right?

Ah, no! I think the easiest way is to run gh pr checkout 747 from within the submodule.

@prady0t
Copy link
Copy Markdown
Contributor Author

prady0t commented May 20, 2026

We till can't remove these parts from scipy for two reasons:

  • We need a fallback to find XP if not given before converting scalar to array (something we do not do here)
  • xp_assert_close_nulp exists and uses it too.

@lucascolley What do you think?

@lucascolley
Copy link
Copy Markdown
Member

We need a fallback to find XP if not given before converting scalar to array (something we do not do here)

Could you explain this in more detail?

@prady0t
Copy link
Copy Markdown
Contributor Author

prady0t commented May 20, 2026

Currently, we do not allow for scalar or Python lists as input, but Scipy uses them everywhere. For this reason, we have to convert them to their respective xp.asarray, but to do that, we need to know' xp' in advance. Hence, we use this block before _convert_scalar_to_array:

    if xp is None:
        try:
            xp = _default_xp_ctxvar.get()
        except LookupError:
            xp = array_namespace(desired)

Hence, we need this block before we convert to xp.asarray and send it to our xp assert functions.

@lucascolley lucascolley self-requested a review May 20, 2026 16:15
Comment on lines +106 to +110
if xp is None:
try:
xp = _default_xp_ctxvar.get()
except LookupError:
xp = array_namespace(desired)
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.

We can remove the default_xp context manager code from scipy and keep it here (and import from here), but we would still do this before converting the inputs to asarrays in scipy. On another thought, we can wrap this around a function and expose it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants