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

string.Template doesn't work with the self keyword argument #67859

Closed
serhiy-storchaka opened this issue Mar 15, 2015 · 6 comments
Closed

string.Template doesn't work with the self keyword argument #67859

serhiy-storchaka opened this issue Mar 15, 2015 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 23671
Nosy @birkenfeld, @terryjreedy, @merwok, @serhiy-storchaka
Files
  • string_formatting_self.patch
  • string_formatting_self_2.patch
  • 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/serhiy-storchaka'
    closed_at = <Date 2015-03-24.20:33:12.816>
    created_at = <Date 2015-03-15.08:21:34.296>
    labels = ['type-bug', 'library']
    title = "string.Template doesn't work with the self keyword argument"
    updated_at = <Date 2015-03-27.16:13:15.216>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-03-27.16:13:15.216>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-03-24.20:33:12.816>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2015-03-15.08:21:34.296>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['38497', '38590']
    hgrepos = []
    issue_num = 23671
    keywords = ['patch']
    message_count = 6.0
    messages = ['238132', '238631', '239177', '239183', '239412', '239414']
    nosy_count = 5.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'eric.araujo', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23671'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 15, 2015
    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka self-assigned this Mar 24, 2015
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 24, 2015

    New changeset 1c19778123a3 by Serhiy Storchaka in branch '2.7':
    Issue bpo-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 bpo-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 bpo-23671: string.Template now allows to specify the "self" parameter as
    https://hg.python.org/cpython/rev/0dd263949e41

    @terryjreedy
    Copy link
    Member

    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.

    @merwok
    Copy link
    Member

    merwok commented Mar 27, 2015

    "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".

    @serhiy-storchaka
    Copy link
    Member Author

    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'

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants