If I had it to do over again, I’d implement it more strictly and only allow chars that are valid in identifiers.
But see https://bugs.python.org/issue31907 for a case that is currently valid and would break if we changed how it worked. I’m not sure it’s worth the churn of deprecating this and eventually making it illegal. -- Eric. > On Oct 31, 2017, at 6:37 AM, Serhiy Storchaka <storch...@gmail.com> wrote: > > According to the specification of format string syntax [1] (I meant > str.format(), not f-strings), both argument name and attribute name must be > Python identifiers. > > But the current implementation is more lenient and allow arbitrary sequences > of characters while they don't contain '.', '[', ']', '{', '}', ':', '!'. > > >>> '{#}'.format_map({'#': 42}) > '42' > >>> import types > >>> '{0.#}'.format(types.SimpleNamespace(**{'#': 42})) > '42' > > This can be confusing due to similarity with the format string syntaxes in > str.format() and f-strings. > > >> name = 'abc' > >>> f'{name.upper()}' > 'ABC' > >>> '{name.upper()}'.format(name='abc') > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > AttributeError: 'str' object has no attribute 'upper()' > > If accept only identifiers, we could produce more specific error message. > > Is there a bug in the documentation or in the implementation? > > [1] https://docs.python.org/3/library/string.html#format-string-syntax > > _______________________________________________ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com