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

Error in str.format for non number formatting #5828

Open
TG-Techie opened this issue Jan 10, 2022 · 3 comments
Open

Error in str.format for non number formatting #5828

TG-Techie opened this issue Jan 10, 2022 · 3 comments

Comments

@TG-Techie
Copy link

TG-Techie commented Jan 10, 2022

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.

@TG-Techie TG-Techie added the bug label Jan 10, 2022
@Neradoc
Copy link

Neradoc commented Jan 10, 2022

That looks like a new thing in 3.10:

Python 3.9.6 (default, Aug 11 2021, 01:29:00) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> '{:09}'.format('Jan')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '=' alignment not allowed in string format specifier
>>> 

@jepler
Copy link
Member

jepler commented Jan 10, 2022 •

Since this is about the core, I've prepared a fix and offered it to micropython. We can make a decision whether to take it ourselves, independently of whether they accept the change: micropython#8156

@jepler
Copy link
Member

jepler commented Jan 10, 2022

Python folks considered it a bug fix, as far as I can tell:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants