Skip to content

API: Update lib.index_tricks namespace #24581

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

Merged
merged 1 commit into from
Sep 1, 2023

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Aug 29, 2023

Relevant issue #24507

Hi @rgommers @ngoldbaum,

This PR moves lib.index_tricks module to a private file and ensures that it's public methods are only available through the main namespace. Looking at overall usage of c_, s_ and r_ (which is a lot as I checked github search) I decided to keep them in the main namespace, therefore there's no need for local index_tricks namespace.

@mtsokol mtsokol force-pushed the lib-index-tricks-namespace branch from c12e904 to eab45a4 Compare August 29, 2023 12:13
@rgommers
Copy link
Member

Looking at overall usage of c_, s_ and r_ (which is a lot as I checked github search) I decided to keep them in the main namespace, therefore there's no need for local index_tricks namespace.

That sounds like the right decision to me.

@ngoldbaum
Copy link
Member

The code changes look good to me and the changes to the lib namespace look correct:

In [5]: diff = set(oldall) - set(np.lib.__all__)

In [6]: diff.issubset(np.__all__)
Out[6]: True

In [7]: diff
Out[7]: 
{'c_',
 'diag_indices',
 'diag_indices_from',
 'fill_diagonal',
 'index_exp',
 'ix_',
 'mgrid',
 'ndenumerate',
 'ndindex',
 'ogrid',
 'r_',
 'ravel_multi_index',
 's_',
 'unravel_index'}

I think is good to merge as soon as the conflict is fixed.

@mtsokol mtsokol force-pushed the lib-index-tricks-namespace branch from eab45a4 to 96395e3 Compare August 30, 2023 08:24
@mtsokol
Copy link
Member Author

mtsokol commented Aug 30, 2023

Rebased and resolved conflicts!

@ngoldbaum
Copy link
Member

Looking on github search, it seems there are downstream usages of np.lib.index_tricks.IndexExpression and np.lib.index_tricks.nd_grid. Both I think need to be available for type annotations and isinstance checks. I think that indicates that index_tricks needs to remain public with only those two symbols remaining inside it, everything else should be available in the main namespace. @rgommers do you agree?

Sorry for missing that on my previous review.

@mtsokol
Copy link
Member Author

mtsokol commented Aug 30, 2023

Looking on github search, it seems there are downstream usages of np.lib.index_tricks.IndexExpression and np.lib.index_tricks.nd_grid. Both I think need to be available for type annotations and isinstance checks. I think that indicates that index_tricks needs to remain public with only those two symbols remaining inside it, everything else should be available in the main namespace. @rgommers do you agree?

In my opinion both of them should be private...
[EDIT] I think they can stay for the purpose of type annotations, as CClass and others are available.

@mtsokol
Copy link
Member Author

mtsokol commented Aug 30, 2023

Ah, you mentioned type annotations - then sure, I can keep them.

@ngoldbaum
Copy link
Member

Yeah, isinstance checks too.

@rgommers
Copy link
Member

I'm not sure about some of these. Using https://cs.github.com/, I find a single usage of index_tricks.CClass and 14 usages of index_tricks.IndexExpression. Each funky np.X_ object seems to have a separate class associated with it. nd_grid is the only one for which I get a lot of hits.

I'm also not sure I understand the type annotations I'm seeing, they seem wrong for s_. E.g:

>>> np.s_[:, :-1]
(slice(None, None, None), slice(None, -1, None))
>>> type(np.s_[:, :-1])
<class 'tuple'>

That's a regular tuple so annotating it as IndexExpression is incorrect.

@mtsokol mtsokol force-pushed the lib-index-tricks-namespace branch from 96395e3 to ef961d9 Compare September 1, 2023 08:37
@mtsokol mtsokol modified the milestone: 2.0.0 release Sep 1, 2023
@mtsokol mtsokol force-pushed the lib-index-tricks-namespace branch from ef961d9 to 86c63b3 Compare September 1, 2023 09:30
@rgommers rgommers added this to the 2.0.0 release milestone Sep 1, 2023
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

LGTM now, thanks @mtsokol! Let's give this a go.

@rgommers rgommers merged commit 7e988fd into numpy:main Sep 1, 2023
@rgommers
Copy link
Member

rgommers commented Sep 1, 2023

nd_grid is the only one for which I get a lot of hits.

For completing the record: I rechecked those hits, and a large majority are outdated forks of the numpy repo or its docstring contents under See Also, which had a usage of np.lib.index_tricks.nd_grid. There wasn't anything that looked relevant.

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

Successfully merging this pull request may close these issues.

3 participants