This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: string.Template doesn't work with the self keyword argument
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: eric.araujo, georg.brandl, python-dev, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2015-03-15 08:21 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
string_formatting_self.patch serhiy.storchaka, 2015-03-15 08:21 review
string_formatting_self_2.patch serhiy.storchaka, 2015-03-20 09:41 review
Messages (6)
msg238132 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-15 08:21
string.Template doesn't allow to specify the self substitute parameter as keyword argument.

>>> import string
>>> string.Template('the self is $self').substitute(self='bozo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: substitute() got multiple values for argument 'self'
>>> string.Template('the self is $self').safe_substitute(self='bozo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: safe_substitute() got multiple values for argument 'self'

The same issue is with string.Formatter.format:

>>> string.Formatter().format('the self is {self}', self='bozo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: format() got multiple values for argument 'self'

Proposed patch fixes these issues.
msg238631 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-20 09:41
Updated patch addresses Paul's and Demian's comments.

Converting the format_string parameter to positional parameter can break third-party code, so this needs a deprecation period.
msg239177 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-24 20:31
New changeset 1c19778123a3 by Serhiy Storchaka in branch '2.7':
Issue #23671: string.Template now allows to specify the "self" parameter as
https://hg.python.org/cpython/rev/1c19778123a3

New changeset 7a4b499c4dc0 by Serhiy Storchaka in branch '3.4':
Issue #23671: string.Template now allows to specify the "self" parameter as
https://hg.python.org/cpython/rev/7a4b499c4dc0

New changeset 0dd263949e41 by Serhiy Storchaka in branch 'default':
Issue #23671: string.Template now allows to specify the "self" parameter as
https://hg.python.org/cpython/rev/0dd263949e41
msg239183 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-03-24 21:28
This is a nice catch of a subtle bug and a nice fix.  I verified that replacing 'self' with '*args' does not simply shift the problem from 'self' to 'args'.

>>> class C:
	def test(*args, **kwargs):
		print(args, kwargs )
	
>>> c = C()
>>> c.test(1, args=2, kwargs=3, self=4)
(<__main__.C object at 0x00000000035FE128>, 1) {'args': 2, 'kwargs': 3, 'self': 4}

While the * and ** names are, like parameter names, local names given in the signature header, they are not counted as parameter names in the code object .co_argcount or .co_kwonlyargcount attributes that are used along with co_varnames in the name-argument matching process.
msg239412 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2015-03-27 15:58
"descriptor 'substitute' of 'Template' object needs an argument"

These error messages don’t seem very user-friendly.  I think the style in the rest of the module is like "substitute method wants x y z".
msg239414 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-27 16:13
It matches error messages generated by builtin unbound methods.

>>> str.format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'format' of 'str' object needs an argument

It would be incorrect to say "substitute method wants x y z", because the substitute method doesn't need any arguments.

>>> string.Template('spam').substitute()
'spam'
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67859
2015-03-27 16:13:15serhiy.storchakasetmessages: + msg239414
2015-03-27 15:58:34eric.araujosetnosy: + eric.araujo
messages: + msg239412
2015-03-24 21:28:27terry.reedysetnosy: + terry.reedy
messages: + msg239183
2015-03-24 20:33:12serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2015-03-24 20:31:58python-devsetnosy: + python-dev
messages: + msg239177
2015-03-24 20:06:37serhiy.storchakasetassignee: serhiy.storchaka
2015-03-20 09:41:57serhiy.storchakasetfiles: + string_formatting_self_2.patch

messages: + msg238631
2015-03-15 08:21:34serhiy.storchakacreate