python / cpython

The Python programming language
https://www.python.org
Other
62.14k stars 29.86k forks source link

Add textwrap.dedent, .indent, as str methods. #62535

Open terryjreedy opened 11 years ago

terryjreedy commented 11 years ago
BPO 18335
Nosy @warsaw, @terryjreedy, @ncoghlan, @ezio-melotti, @merwok, @Julian, @ericsnowcurrently, @vadmium, @serhiy-storchaka

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: ```python assignee = None closed_at = None created_at = labels = ['type-feature'] title = 'Add textwrap.dedent, .indent, as str methods.' updated_at = user = 'https://github.com/terryjreedy' ``` bugs.python.org fields: ```python activity = actor = 'martin.panter' assignee = 'none' closed = False closed_date = None closer = None components = [] creation = creator = 'terry.reedy' dependencies = [] files = [] hgrepos = [] issue_num = 18335 keywords = [] message_count = 7.0 messages = ['192109', '192114', '192115', '192116', '192133', '192148', '196696'] nosy_count = 12.0 nosy_names = ['barry', 'terry.reedy', 'ncoghlan', 'ezio.melotti', 'eric.araujo', 'cvrebert', 'Julian', 'tshepang', 'eric.snow', 'martin.panter', 'serhiy.storchaka', 'SpaghettiToastBook'] pr_nums = [] priority = 'normal' resolution = None stage = 'patch review' status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue18335' versions = ['Python 3.4'] ```

terryjreedy commented 11 years ago

To end proposals for new syntax to do what they do for triple-quoted strings. Nick Coghlan gave reasons as follows: run time cost small, can be optimized away, would be used more than some other string methods. http://mail.python.org/pipermail/python-ideas/2013-July/021717.html

In response, Guido said "That's a compelling argument. Let's do it." http://mail.python.org/pipermail/python-ideas/2013-July/021718.html

ncoghlan commented 11 years ago

It turns out there's one slight wrinkle in this grand plan: it won't work for docstrings without some additional tweaking to allow for method calls in the docstring detection.

>>> def f():
...     """example""".lower()
... 
>>> print(f.__doc__)
None
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_CONST               1 ('example') 
              3 LOAD_ATTR                0 (lower) 
              6 CALL_FUNCTION            0 (0 positional, 0 keyword pair) 
              9 POP_TOP              
             10 LOAD_CONST               0 (None) 
             13 RETURN_VALUE
ncoghlan commented 11 years ago

I still think the methods are worth adding regardless - I just anticipate a request to allow method calls on docstrings to follow not long after ;)

serhiy-storchaka commented 11 years ago

str already have too many methods. Who uses str.swapcase() or str.zfill() now? I'm -0.5 for adding any new str methods.

warsaw commented 11 years ago

.dedent() is a no-brainer. JFDI. +1

.indent() I guess makes sense for symmetry, although I've rarely used it. -0

As for docstrings, I can imagine other alternatives, so let's let someone interested in that go through the PEP process.

merwok commented 11 years ago

IMO it’s fine that docstrings continue to live as pure string literals, and documentation tools continue to follow PEP-257’s advice.

vadmium commented 11 years ago

If this goes ahead, would a bytes.dedent() method be also considered? I recently discovered that textwrap.dedent() does not work on bytes() in Python 3. I would have used it for the contents of an input file in a test case.

For the record there’s an older bpo-1237680 on this, rejected in 2005.

methane commented 1 year ago

81087 is a relating issue.

methane commented 1 year ago

I implemented compile time cleandoc in #106411. So this issue should be focus on str.dedent() and bytes.dedent().

textwrap.dedent() is complex function. I want to make str.dedent() much simpler.

methane commented 1 year ago

I created a poll on discuss.python.org for naming. https://discuss.python.org/t/str-dedent-vs-str-removeindent/30127

ofek commented 9 months ago

What is the status of this?

methane commented 9 months ago

I need to write a PEP for this. But I don't have time for it for now.