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/archive%40mail-archive.com

Reply via email to