-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
string.Template doesn't work with the self keyword argument #67859
Comments
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. |
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. |
New changeset 1c19778123a3 by Serhiy Storchaka in branch '2.7': New changeset 7a4b499c4dc0 by Serhiy Storchaka in branch '3.4': New changeset 0dd263949e41 by Serhiy Storchaka in branch 'default': |
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. |
"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". |
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' |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: