Skip to content

Error in str.format for non number formatting #5828

@TG-Techie

Description

@TG-Techie

CircuitPython version

Adafruit CircuitPython 7.2.0-alpha.1-58-g211dc53d4-dirty on 2022-01-10; TG-Watch with nRF52840

Code/REPL

'{:09}'.format('Jan')

Behavior

The behavior circuitpython is:

>>> '{:09}'.format('Jan')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '=' alignment not allowed in string format specifier

Description

the behavior on python 3.10 is:

>>> '{:09}'.format('Jan')
'Jan000000'

It took a good bit to figure out what the error was, I’m not sure if the behavior with this error is intentional.
The error message was decently misleading while debugging my code. (because I didn’t have any =s in the text I was using)

Additional information

Taking a quick look at the source, it looks like that error message is commented as for not numbers but is also the default case... which doesn't necessarily have an = in it

circuitpython/py/objstr.c

Lines 1378 to 1389 in 476b17b

} else {
// arg doesn't look like a number
if (align == '=') {
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
terse_str_format_value_error();
#else
mp_raise_ValueError(
MP_ERROR_TEXT("'=' alignment not allowed in string format specifier"));
#endif
}

I'm more than glad to PR a different error message! I’m not sure if this is a large enough edge case to merit a PR?

Or PR a check for an equal and display this message and display a more generic "we don’t know what to do with the given formatter" otherwise.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions