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

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

Open
terryjreedy opened this issue Jul 1, 2013 · 12 comments
Open

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

terryjreedy opened this issue Jul 1, 2013 · 12 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@terryjreedy
Copy link
Member

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:

assignee = None
closed_at = None
created_at = <Date 2013-07-01.05:33:49.321>
labels = ['type-feature']
title = 'Add textwrap.dedent, .indent, as str methods.'
updated_at = <Date 2013-09-01.01:58:54.523>
user = 'https://github.com/terryjreedy'

bugs.python.org fields:

activity = <Date 2013-09-01.01:58:54.523>
actor = 'martin.panter'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2013-07-01.05:33:49.321>
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
Copy link
Member Author

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

@terryjreedy terryjreedy added the type-feature A feature request or enhancement label Jul 1, 2013
@ncoghlan
Copy link
Contributor

ncoghlan commented Jul 1, 2013

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
Copy link
Contributor

ncoghlan commented Jul 1, 2013

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
Copy link
Member

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
Copy link
Member

warsaw commented Jul 1, 2013

.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
Copy link
Member

merwok commented Jul 1, 2013

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

@vadmium
Copy link
Member

vadmium commented Sep 1, 2013

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
Copy link
Member

methane commented Jul 1, 2023

#81087 is a relating issue.

@methane
Copy link
Member

methane commented Jul 14, 2023

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.

  • remove only whitespace -- user need to use textwrap.dedent() to support TAB indent
    • If we want to support TAB, we will add option later.
  • both of bytes.dedent() and str.dedent() use same char* based logic -- str.dedent() uses utf-8. This minimize our maintenance cost.

@methane
Copy link
Member

methane commented Jul 23, 2023

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

@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 27, 2023
@ofek
Copy link
Sponsor Contributor

ofek commented Dec 9, 2023

What is the status of this?

@methane
Copy link
Member

methane commented Dec 10, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

9 participants