Skip to content

re.split has seemingly unnecessary Anys #7940

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

Closed
GrantGryczan opened this issue May 24, 2022 · 5 comments
Closed

re.split has seemingly unnecessary Anys #7940

GrantGryczan opened this issue May 24, 2022 · 5 comments

Comments

@GrantGryczan
Copy link

I would think that the | Anys in the following four return types should be removed.

def split(self: Pattern[str], string: str, maxsplit: int = ...) -> list[str | Any]: ...

def split(self: Pattern[bytes], string: ReadableBuffer, maxsplit: int = ...) -> list[bytes | Any]: ...

def split(pattern: str | Pattern[str], string: str, maxsplit: int = ..., flags: _FlagsType = ...) -> list[str | Any]: ...

) -> list[bytes | Any]: ...

@JelleZijlstra
Copy link
Member

They're there because the list entries can be None:

In [6]: re.split(r"(a)|(b)", "canada")
Out[6]: ['c', 'a', None, 'n', 'a', None, 'd', 'a', None, '']

Though this behavior doesn't appear to be documented: https://docs.python.org/3.10/library/re.html#re.split

@GrantGryczan
Copy link
Author

Huh, alright then. Strange that it isn't documented.

@GrantGryczan
Copy link
Author

GrantGryczan commented May 25, 2022

Actually, would it be a bad idea to replace the Anys with None in that case? Otherwise, in my use case, I'll have to cast the return value as list[str | None] myself.

@GrantGryczan GrantGryczan reopened this May 25, 2022
@JukkaL
Copy link
Contributor

JukkaL commented May 25, 2022

Having None in the return value would likely cause many false positives, since often the caller can predict that there will only be strings. That's why the | Any was there in the first place instead of | None.

It would be good to add a comment explaining the Any types, as otherwise this will continue to cause confusion.

@AlexWaygood
Copy link
Member

It would be good to add a comment explaining the Any types, as otherwise this will continue to cause confusion.

@srittau has proposed #7870 to tackle this problem in a more general way; we're currently bikeshedding debating the appropriate name for the feature.

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

No branches or pull requests

4 participants