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

Unhelpful error message with str.format() #49497

Closed
pitrou opened this issue Feb 13, 2009 · 10 comments
Closed

Unhelpful error message with str.format() #49497

pitrou opened this issue Feb 13, 2009 · 10 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@pitrou
Copy link
Member

pitrou commented Feb 13, 2009

BPO 5247
Nosy @rhettinger, @pitrou, @ericvsmith
Files
  • issue5247.patch
  • issue5247-1.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/ericvsmith'
    closed_at = <Date 2009-02-20.14:31:09.308>
    created_at = <Date 2009-02-13.14:12:54.414>
    labels = ['interpreter-core', 'type-bug']
    title = 'Unhelpful error message with str.format()'
    updated_at = <Date 2009-02-20.14:31:09.306>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2009-02-20.14:31:09.306>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2009-02-20.14:31:09.308>
    closer = 'eric.smith'
    components = ['Interpreter Core']
    creation = <Date 2009-02-13.14:12:54.414>
    creator = 'pitrou'
    dependencies = []
    files = ['13102', '13104']
    hgrepos = []
    issue_num = 5247
    keywords = ['patch']
    message_count = 10.0
    messages = ['81931', '81969', '81973', '82167', '82169', '82170', '82192', '82527', '82528', '82531']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'pitrou', 'eric.smith', 'LambertDW']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5247'
    versions = ['Python 2.6', 'Python 3.0', 'Python 3.1', 'Python 2.7']

    @pitrou
    Copy link
    Member Author

    pitrou commented Feb 13, 2009

    >>> "{0:f}".format(2.5)
    '2.500000'
    >>> "{0:f}".format("spam")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: Unknown conversion type f

    The error message should mention the type of the argument that doesn't
    support the given conversion type (e.g. "Unknown conversion type f for
    object of type 'float'"). Otherwise it can be very confusing.

    @pitrou pitrou added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 13, 2009
    @ericvsmith
    Copy link
    Member

    I agree. I'm not sure on backporting this.

    I'll work on a patch.

    @rhettinger
    Copy link
    Contributor

    Seems like a reasonable backport.

    @ericvsmith ericvsmith self-assigned this Feb 14, 2009
    @ericvsmith
    Copy link
    Member

    I've gone back and read PEP-3101. To use its terminology, I think the
    error message should be something like:
    Unknown presentation type %c for type %s.

    I'm not sure where I got the original wording "conversion type". It's
    true that it's sometimes used for type conversion (int->float, for
    example), but that's not its real purpose.

    It's unfortunate that "type" is used in the PEP and the online docs for
    "presentation type", and we're trying to report an error on the
    argument's type.

    Any suggestions for clearer wording?

    @ericvsmith
    Copy link
    Member

    The attached patch (against trunk) changes the message.

    However, it has at least one unintended consequence. If you have an
    object with no __format__, it gets converted to a string, which is then
    formatted. So you get:

    >>> '{0:^10}'.format(0j)
    '    0j    '
    >>> '{0:^10x}'.format(0j)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: Unknown presentation type x for str
    >>> 

    I'm calling format on an complex number, but the error says "str". The
    error is correct, because the complex number has been converted to a
    string before the formatting mechanism can get the actual type. I think
    we'll have to live with this if we add the type to the error message.

    @pitrou
    Copy link
    Member Author

    pitrou commented Feb 15, 2009

    Any suggestions for clearer wording?

    Use "formatting code" rather than "presentation type"?

    @ericvsmith
    Copy link
    Member

    With this patch, I changed it to "format code", and made it more in line
    with Antoine's original suggested message.

    I'm okay with "format code" or "formatting code", but if we do use
    either of those wordings, we should change the documentation to match.
    Not sure if the PEP should be modified. It's slightly out of date anyway.

    >>> '{0:x}'.format('')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: Unknown format code 'x' for object of type 'str'
    >>>

    @ericvsmith
    Copy link
    Member

    I'm getting ready to commit this. Does it need a test? I poked around
    and didn't see any tests that validate exception text, but maybe there
    are some I missed. I tend to think it shouldn't have a test, but I'm not
    sure what the norm is.

    @pitrou
    Copy link
    Member Author

    pitrou commented Feb 20, 2009

    Looks good to me. I don't think you need to write a specific test for this.

    @ericvsmith
    Copy link
    Member

    Committed in:
    r69806 (trunk)
    r69807 (release26-maint)
    r69808 (py3k)
    r69809 (release30-maint)

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants