Skip to content

Allow keyword argument in str.splitlines() #57221

Closed

Description

@mdickinson
BPO 13012
Nosy @terryjreedy, @mdickinson, @ezio-melotti, @alex, @meadori
Files
  • issue13012.patch
  • issue13012-repl.diff
  • 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 = 'https://github.com/mdickinson'
    closed_at = <Date 2011-09-28.14:40:53.786>
    created_at = <Date 2011-09-20.13:44:06.891>
    labels = ['interpreter-core', 'type-feature']
    title = 'Allow keyword argument in str.splitlines()'
    updated_at = <Date 2011-09-28.14:40:53.786>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2011-09-28.14:40:53.786>
    actor = 'ezio.melotti'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2011-09-28.14:40:53.786>
    closer = 'ezio.melotti'
    components = ['Interpreter Core']
    creation = <Date 2011-09-20.13:44:06.891>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['23208', '23233']
    hgrepos = []
    issue_num = 13012
    keywords = ['patch']
    message_count = 18.0
    messages = ['144326', '144327', '144328', '144329', '144343', '144358', '144361', '144470', '144474', '144482', '144483', '144488', '144489', '144497', '144498', '144506', '144565', '144566']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'mark.dickinson', 'ezio.melotti', 'alex', 'meador.inge', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13012'
    versions = ['Python 3.3']

    Activity

    1. mdickinson commented on Sep 20, 2011

      @mdickinson
      MemberAuthor

      The docstring of str.splitlines says:

      splitlines(...)
          S.splitlines([keepends]) -> list of strings
          
          Return a list of the lines in S, breaking at line boundaries.
          Line breaks are not included in the resulting list unless keepends
          is given and true.

      Currently the optional argument can only be specified positionally. What do people think about also allowing this argument to be specified by keyword? (And applying the same change to bytes.splitlines.)

      The main motivation here is readability: in

         long_string.splitlines(keepends=True)

      it's fairly clear what the boolean argument is doing. But in

         long_string.splitlines(True)

      it's much less clear. (This came up during a Python training class that I was running recently.) I'm not advocating making the argument keyword-only; just allowing those who want to specify it by keyword to do so.

      There are many other str methods that don't accept keyword arguments, but it's only this one where I think readability would really benefit.

    2. added
      interpreter-core
      type-feature
      on Sep 20, 2011
    3. mdickinson commented on Sep 20, 2011

      @mdickinson
      MemberAuthor

      (And applying the same change to bytes.splitlines.)

      Oh, and bytearray.splitlines, too.

    4. alex commented on Sep 20, 2011

      @alex
      Member

      Personally, I regard every C function which, for obscure internal details, doesn't take keyword arguments as a sad bug, which should of course be fixed :)

    5. meadori commented on Sep 20, 2011

      @meadori
      Member

      +1; the keyword arg version is much more readable.

    6. mdickinson commented on Sep 20, 2011

      @mdickinson
      MemberAuthor

      Here's a patch.

    7. ezio-melotti commented on Sep 21, 2011

      @ezio-melotti
      Member

      LGTM

    8. meadori commented on Sep 21, 2011

      @meadori
      Member

      Patch looks good. I noticed a change in the conventional type for
      'keepends' from 'int' to 'bool'. Several unit tests were updated to
      match this change. Perhaps other call sites should be updated too? A
      little greping shows:

      $ grep -Rl 'splitlines(0)' * --include='*.py'
      Doc/tools/docutils/writers/newlatex2e/__init__.py
      $ grep -Rl 'splitlines(1)' * --include='*.py'
      Doc/tools/sphinx/pycode/pgen2/tokenize.py
      Doc/tools/docutils/readers/python/moduleparser.py
      Lib/test/test_tokenize.py
      Lib/difflib.py
      Lib/lib2to3/pgen2/tokenize.py
      Lib/codecs.py
    9. terryjreedy commented on Sep 23, 2011

      @terryjreedy
      Member

      I agree with Alex. The poorly documented fact that *some* C-coded functions cannot accept arguments identified by keyword rather than position is a bit hole in the function abstraction.
      +1 to the patch (and the int to bool change)

    10. ezio-melotti commented on Sep 23, 2011

      @ezio-melotti
      Member

      The attached patch adds 'keepends=' to a few calls and replaces 0/1 with False/True. The patch can be applied after Mark's patch. Doing two separate commits is probably better.

    11. meadori commented on Sep 24, 2011

      @meadori
      Member

      Doing two separate commits is probably better.

      Out of curiosity, what is typically the convention on that? The
      devguide seems to suggest one changeset per issue:

      """
      Just please make sure that you generate a single, condensed patch rather than a series of several changesets.
      """

      I think for this case two patches is better. In general, I am OK with
      the git-style series and hg-style patchbombs, but the devguide seems
      to say otherwise. Hmmm, that makes me wonder if we can patchbomb the
      tracker :-)

    12. terryjreedy commented on Sep 24, 2011

      @terryjreedy
      Member

      One changeset per issue is a general goal. So is the ability to review patches. Sometime people forget to add a News or Acks entry and have to followup with an addendum. (Mark's patch still lack that, for instance.) Sometimes a patch is so large that a reviewer asks or requires splitting. In this case, each patch is large enough and touches enough files (up to 10 I think) that two is plausible. What would not be appreciated by either a reviewer or commit-list recipients would be one patch file per patched file. That would be a patch bomb indeed ;-)

    13. python-dev commented on Sep 24, 2011

      python-devmannequin
      Mannequin

      New changeset 0b2660384d4e by Mark Dickinson in branch 'default':
      Issue bpo-13012: Allow 'keepends' to be passed as a keyword argument in str.splitlines, bytes.splitlines and bytearray.splitlines.
      http://hg.python.org/cpython/rev/0b2660384d4e

    14. mdickinson commented on Sep 24, 2011

      @mdickinson
      MemberAuthor

      Thanks for the reviews.

      Applied the first patch, plus Misc/NEWS entry, and minus the change to Lib/collections/init.py; I think that change belongs more with the second patch.

    15. mdickinson commented on Sep 24, 2011

      @mdickinson
      MemberAuthor

      Ezio's patch looks fine to me, though as Meador points out in the Rietveld review, there are a couple of omissions that could be fixed.

      Personally, I'm not so convinced of the value of upgrading all the existing uses of splitlines to use 'keepends'; I just wanted to be able to do this in *new* code. :-) I guess I'm +0 on the int -> bool changes (replacing .splitlines(1) with .splitlines(True)), and -0 on adding the extra 'splitlines' keywords throughout existing source.

    16. ezio-melotti commented on Sep 24, 2011

      @ezio-melotti
      Member

      Out of curiosity, what is typically the convention on that?

      In theory every issue should be about a single problem and therefore have a single patch to fix it. Sometimes a secondary "problem" that is not strictly related to the first one arises, for example when a cleanup is necessary before applying the patch, or when a new feature is added and a few places get changed to use it. In these cases is not worth opening another issue, so both the patches are attached to the same issue, but committed separately because they address two different "problems".

      Sometime people forget to add a News or Acks entry and have to
      followup with an addendum. (Mark's patch still lack that, for
      instance.)

      Avoiding to include the NEWS entry in patches is common, because the file gets updated often and it will most likely cause conflicts. make patchcheck remind us to add it -- assuming you don't forget to use it.

      Ezio's patch looks fine to me, though as Meador points out in the
      Rietveld review, there are a couple of omissions that could be fixed.

      Adding keepends there would have made the line too long, required some splitting and probably made the code less readable overall, so I prefer to keep it out.

      Personally, I'm not so convinced of the value of upgrading all the
      existing uses of splitlines to use 'keepends'; I just wanted to be
      able to do this in *new* code. :-)

      If we can make old code more readable too and we already have a patch to do it, why not? (one instance also had the comment # True == keep line ends)

    17. terryjreedy commented on Sep 24, 2011

      @terryjreedy
      Member

      Given that someone is willing to write a patch and someone else to review it, I am +1 on changing 0/1 to False/True anywhere appropriate in the stdlib. Ditto for using a new feature. The "# True == keep line ends" comment illustrates why the original patch is a good idea.

    18. python-dev commented on Sep 28, 2011

      python-devmannequin
      Mannequin

      New changeset 83f43b58c988 by Ezio Melotti in branch 'default':
      bpo-13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
      http://hg.python.org/cpython/rev/83f43b58c988

    19. ezio-melotti commented on Sep 28, 2011

      @ezio-melotti
      Member

      I applied my patch, including the changes in Lib/collections/init.py, the issue can now be closed.

    20. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    interpreter-coretype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions