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

Support tuples for more stringlike functions #118184

Closed
nineteendo opened this issue Apr 23, 2024 · 2 comments
Closed

Support tuples for more stringlike functions #118184

nineteendo opened this issue Apr 23, 2024 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Apr 23, 2024

Feature or enhancement

Proposal:

These are all the functions of str (and bytes too) that could get tuple support like str.startswith() & str.endswith().

I think it would be most useful for str.find(), str.rfind(), str.rsplit() & str.split().
This would improve some code in the stdlib and would also benefit end users.

from collections.abc import Sequence
import sys
from typing import LiteralString, SupportsIndex


class str(Sequence[str]):
    def count(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
    def find(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
    def index(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
    @overload
    def partition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ...
    @overload
    def partition(self, sep: str, /) -> tuple[str, str, str]: ...
    if sys.version_info >= (3, 9):
        @overload
        def removeprefix(self: LiteralString, prefix: LiteralString, /) -> LiteralString: ...
        @overload
        def removeprefix(self, prefix: str, /) -> str: ...
        @overload
        def removesuffix(self: LiteralString, suffix: LiteralString, /) -> LiteralString: ...
        @overload
        def removesuffix(self, suffix: str, /) -> str: ...
    def rfind(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
    def rindex(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
    @overload
    def rpartition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ...
    @overload
    def rpartition(self, sep: str, /) -> tuple[str, str, str]: ...
    @overload
    def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
    @overload
    def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ...
    @overload
    def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
    @overload
    def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ...

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

Linked PRs

@nineteendo nineteendo added the type-feature A feature request or enhancement label Apr 23, 2024
@zware
Copy link
Member

zware commented Apr 23, 2024

Note: I'm no longer getting a reponse on Discourse (and the followup thread was merged).

There was no support for the idea on Discourse. The purpose of the discussion is to find support before adding clutter to the issue tracker. If sentiment changes on Discourse, this issue can be reopened.

@zware zware closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2024
@nineteendo
Copy link
Contributor Author

I have permission from @pfmoore to submit a PR and see if it gets accepted or rejected:

One other option is for someone just to submit one or more PRs implementing the proposed feature(s). The PRs will either get accepted or rejected, and then you have your answer. The lack of response might just be because there’s not a lot that’s interesting to say.

I don’t personally think this is worth the effort to implement, and I’m not convinced I’d find it very useful in practice. But I also don’t think it’s such a big deal that it needs a big debate, or communty consensus, or a PEP. So if you want to put in the effort, just go for it.

@nineteendo nineteendo changed the title Support tuples for find & rfind Support tuples for find, index, rfind & rindex May 26, 2024
@nineteendo nineteendo changed the title Support tuples for find, index, rfind & rindex Support tuples for more stringlike functions May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants