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

str.strip() should have a means of adding to the default behaviour #83599

Closed
senji mannequin opened this issue Jan 22, 2020 · 9 comments
Closed

str.strip() should have a means of adding to the default behaviour #83599

senji mannequin opened this issue Jan 22, 2020 · 9 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@senji
Copy link
Mannequin

senji mannequin commented Jan 22, 2020

BPO 39418
Nosy @rhettinger, @stevendaprano, @iritkatriel
Files
  • test_ws.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-03-26.01:16:46.049>
    created_at = <Date 2020-01-22.12:16:56.167>
    labels = ['type-feature', 'library', '3.9']
    title = 'str.strip() should have a means of adding to the default behaviour'
    updated_at = <Date 2021-03-26.01:16:46.049>
    user = 'https://bugs.python.org/senji'

    bugs.python.org fields:

    activity = <Date 2021-03-26.01:16:46.049>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-03-26.01:16:46.049>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-01-22.12:16:56.167>
    creator = 'senji'
    dependencies = []
    files = ['48858']
    hgrepos = []
    issue_num = 39418
    keywords = []
    message_count = 9.0
    messages = ['360459', '360460', '360461', '360464', '360466', '360467', '360726', '389531', '389533']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'steven.daprano', 'senji', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue39418'
    versions = ['Python 3.9']

    @senji
    Copy link
    Mannequin Author

    senji mannequin commented Jan 22, 2020

    If I want to remove the default set of 'whitespace' characters plus something else from a string there's currently no way to cleanly specify that. In addition there's no way to programatically acquire what characters are considered whitespace so you can't call split with an argument constructed of existing whitespace characters with the new things you need.

    As an example you could have an additionally= parameter such that:

    " ( 123 ) ".strip() gives "( 123 )" and
    " ( 123 ) ".strip(additionally="()") gives "123"

    I've not given that any thought so it's probably not the best way of solving the problem.

    @senji senji mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes type-feature A feature request or enhancement labels Jan 22, 2020
    @senji
    Copy link
    Mannequin Author

    senji mannequin commented Jan 22, 2020

    Oops, sorry, that should say 'strip' througout not 'split'. My bad.

    @stevendaprano
    Copy link
    Member

    This is a new feature, not a bug, so it will apply only to 3.9. All older versions are in feature freeze.

    there's no way to programatically acquire what characters are considered whitespace

    https://docs.python.org/3/library/stdtypes.html#str.isspace

    https://docs.python.org/3/library/string.html#string.whitespace

    @stevendaprano stevendaprano removed 3.7 (EOL) end of life 3.8 only security fixes labels Jan 22, 2020
    @stevendaprano
    Copy link
    Member

    There's a discrepancy between the string.whitespace constant and actual whitespace as tested by the str.isspace() method.

    I've found five ASCII whitespace characters (as reported by isspace) which aren't in the string.whitespace constant, and 18 non-ASCII whitespace characters which aren't in the string.

    The good news is that there are no non-whitespace characters in the constant :-)

    The non-ASCII ones probably don't matter, as string.whitespace is documented as only being ASCII. But the following are missing:

    U+001C, U+001D, U+001E, U+001F, U+0085

    @senji
    Copy link
    Mannequin Author

    senji mannequin commented Jan 22, 2020

    https://bugs.python.org/issue25433 has a summary of the issue about what actually constitutes whitespace from the perspective of improving the documentation. In terms of what strip does it turns out that string.whitespace is a red herring.

    A list of whitespace characters _could_ be produced at runtime using str.isspace() but that would require iterating over the entire space of characters; which is likely to be slow as well as poor style.

    @stevendaprano
    Copy link
    Member

    Attached is a script I used to test:

    • that str.isspace() currently matches the definition given in the docs;

    • that string.whitespace contains all whitespace characters, and nothing but whitespace;

    • that str.strip() strips off all whitespace characters, and nothing but whitespace;

    • that str.split() splits on all whitespace characters, and nothing but whitespace.

    I haven't tested the .lstrip rstrip rsplit methods because I'm lazy :-)

    The only test that fails is that the string.whitespace is missing some whitespace.

    Again, because I'm lazy, I haven't written this as proper unit tests. Besides, it's quite likely all these things are already in the official test suite. (If not, I might re-write the missing parts as unit tests and submit a PR.)

    @rhettinger
    Copy link
    Contributor

    For this problem, I would reach for regular expressions. They are specifically designed for flexibility and customization.

    @iritkatriel
    Copy link
    Member

    In python 3.9 there is also str.removeprefix:
    https://docs.python.org/3/library/stdtypes.html#str.removeprefix
    which you can apply in a loop.

    @iritkatriel iritkatriel added stdlib Python modules in the Lib dir labels Mar 26, 2021
    @rhettinger
    Copy link
    Contributor

    Marking this as closed because the issue doesn't seem to have gathered much interest. If someone wants to move this forward, I recommend discussing it on python-ideas.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants