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

Slice attributes can be of any type. #3024

Merged
merged 2 commits into from
Jul 11, 2019
Merged

Slice attributes can be of any type. #3024

merged 2 commits into from
Jul 11, 2019

Conversation

brandtbucher
Copy link
Member

>>> slice([], object(), type)
slice([], <object object at 0x7f2e0e522120>, <class 'type'>)

@JelleZijlstra
Copy link
Member

They theoretically can be, but is it actually useful to construct slices like the one in your comment? I'm worried that this will sacrifice some type safety for code that uses standard integer slices for little real benefit.

See python/typing#159 for prior discussion on slices.

@brandtbucher
Copy link
Member Author

brandtbucher commented May 31, 2019

I agree that it's uncommon (even impossible) to use when slicing the built-in types. In data processing libraries, however, it is not unusual to slice on any key type. I encountered this false-positive when working on StaticFrame, which allows slicing by index values:

https://static-frame.readthedocs.io/en/latest/api_selection.html?highlight=slice#frame

The widely used Pandas library allows this as well:

https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#selection-by-label

Slices are rarely created using the slice constructor in typical code (attribute access is rare as well), but it's very common to explicitly build and manipulate them internally in libraries such as these.

(Thanks for the link. I did a quick search for this, but it didn't occur to me to check the typing repo!)

@brandtbucher
Copy link
Member Author

So is this something that can be considered?

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

I guess we should do this then to avoid false positive type errors.

@brandtbucher
Copy link
Member Author

Bump. Is there anything keeping this from being merged?

@rushabh-v
Copy link

It gives error: Slice index must be an Integer of None for below kind of code.
pd.IndexSlice[:"b":-1]
pd.IndexSlice["m":"m":-1]
see pandas-dev/pandas#29188

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.

3 participants