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: str.format: Silent truncation of kwargs when passing keywords containing colons
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Kenneth Lim, abarry, vstinner
Priority: normal Keywords:

Created on 2016-05-30 14:52 by Kenneth Lim, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
poc.py Kenneth Lim, 2016-05-30 14:52 Demonstration of issue
Messages (6)
msg266696 - (view) Author: Kenneth Lim (Kenneth Lim) Date: 2016-05-30 14:52
Passing a dict with colon-containing keys as kwargs to a function results in a KeyError stemming from a silent truncation of the keys.

Error does not clearly describe the issue in this case.
msg266697 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-05-30 14:56
IMHO the error is very explicit:

Traceback (most recent call last):
  File "poc.py", line 6, in <module>
    print(a.format(**r))
KeyError: 'HGNC'

Your "r" directory has no "HGNC" key. I think that you misunderstood the formatting of str.format:
https://docs.python.org/dev/library/string.html#formatstrings

The format string "{HGNC:11892}" is parsed as key="HGNC", format="11892".
msg266698 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-05-30 15:02
In other words, you cannot use keys containing a colon in str.format - you'll need to replace the colons by something else (for example an underscore, that works fine).
msg266703 - (view) Author: Kenneth Lim (Kenneth Lim) Date: 2016-05-30 16:19
Hi Barry,

I was aware of that. However, I was alluding to the KeyError produced, and
the silent truncation of the text. Rather than failing at the colon step
(SyntaxError when on run), truncated arguments pass this step, causing
errors downstream.

On Mon, May 30, 2016 at 11:02 PM, Emanuel Barry <report@bugs.python.org>
wrote:

>
> Emanuel Barry added the comment:
>
> In other words, you cannot use keys containing a colon in str.format -
> you'll need to replace the colons by something else (for example an
> underscore, that works fine).
>
> ----------
> nosy: +ebarry
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue27160>
> _______________________________________
>
msg266707 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-05-30 16:37
The behaviour is correct, it's your assumptions that aren't :)

The code for str.format only checks for what's before the colon (here, "HGNC") and checks if that's part of the dict provided. It isn't, so it raises a KeyError. It doesn't even get to the format spec part (which is a perfectly valid format specifier). Your dict can contain anything or be empty, str.format only checks for the existence of the key you asked for ("HGNC"). "{HGNC:11892}" is also a perfectly valid Python string.

P.S.: While I'm fine with people calling me by my last name, there's another developer whose name is Barry Warsaw, so let's try to avoid confusion here ;-)
msg266709 - (view) Author: Kenneth Lim (Kenneth Lim) Date: 2016-05-30 16:58
Ah, so the truncation is fully intended behavior.

As for the name, a touch too many comic-books in the past might be the
reason I latched onto "Barry" as a first name.

Thanks for the clarification.

On Tue, May 31, 2016 at 12:37 AM, Emanuel Barry <report@bugs.python.org>
wrote:

>
> Emanuel Barry added the comment:
>
> The behaviour is correct, it's your assumptions that aren't :)
>
> The code for str.format only checks for what's before the colon (here,
> "HGNC") and checks if that's part of the dict provided. It isn't, so it
> raises a KeyError. It doesn't even get to the format spec part (which is a
> perfectly valid format specifier). Your dict can contain anything or be
> empty, str.format only checks for the existence of the key you asked for
> ("HGNC"). "{HGNC:11892}" is also a perfectly valid Python string.
>
> P.S.: While I'm fine with people calling me by my last name, there's
> another developer whose name is Barry Warsaw, so let's try to avoid
> confusion here ;-)
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue27160>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71347
2016-05-30 16:58:29Kenneth Limsetmessages: + msg266709
2016-05-30 16:37:25abarrysetmessages: + msg266707
2016-05-30 16:19:54Kenneth Limsetmessages: + msg266703
2016-05-30 15:02:36abarrysetnosy: + abarry
messages: + msg266698
2016-05-30 14:59:41abarrysetstatus: open -> closed
resolution: not a bug
stage: resolved
2016-05-30 14:56:47vstinnersettitle: Silent truncation of kwargs when passing keywords containing colons -> str.format: Silent truncation of kwargs when passing keywords containing colons
2016-05-30 14:56:42vstinnersetnosy: + vstinner
messages: + msg266697
2016-05-30 14:52:24Kenneth Limcreate