python-stdlib/unittest/unittest: Remove f-strings.#1110
python-stdlib/unittest/unittest: Remove f-strings.#1110agatti wants to merge 1 commit intomicropython:masterfrom
Conversation
dpgeorge
left a comment
There was a problem hiding this comment.
Thanks for doing this.
While your at it, you could also remove the import os at the top, it's not needed, and eliminates a dependency.
(When I looked at this I wanted to convert all string formatting to "".format(...), because that's guaranteed to always be available, while % formatting is not due to MICROPY_PY_BUILTINS_STR_OP_MODULO. But that's a much bigger change, and anyway unittest won't work with less than MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES due to other things like io module.)
| if self.params: | ||
| detail = ", ".join(f"{k}={v}" for k, v in self.params.items()) | ||
| test_details += (f" ({detail})",) | ||
| detail = ", ".join("%s=%s" % (k, v) for k, v in self.params.items()) |
There was a problem hiding this comment.
This could be "%s=%s" % k_v for k_v in self.params.items() if you wanted to get a little tricky. That would be more efficient as well, no unpacking of the tuple or repacking (and hence allocating another 2-tuple).
| detail = " ".join((str(i) for i in c)) | ||
| print("======================================================================") | ||
| print(f"FAIL: {detail}") | ||
| print("FAIL: %s" % detail) |
There was a problem hiding this comment.
suggest just print("FAIL:", detail) (we know detail is a str so it's equivalent)
I admit I thought modulo was one of the features available on all profiles. I've simply followed the style of the rest of the code and made sure it worked on device. Still, if the minimum ROM level for this could be lowered even further that'd be nice somehow. Once I'm done with the CH32V 64KiB and 128KiB MCUs I guess I can have a go at the lower-specced ones and see if this can be adapted to work on that too. |
This commit removes usage of f-strings from the unittest module, in favour to the old printf-style raw string formatting operations. The module is a bit special since it is meant to also validate the behaviour of MicroPython interpreters, and those may come with any combination of configuration options. For example, f-strings are not available by default on some feature levels, and thus the test suite won't run cleanly on certain targets unless the support for that feature is explicitly enabled. See the discussion at micropython/micropython#19111 for more information. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
bcb03ac to
9cc8794
Compare
Summary
This PR removes usage of f-strings from the unittest module, in favour to the old printf-style raw string formatting operations.
The module is a bit special since it is meant to be also validate the behaviour of MicroPython interpreters, and those may come with any combination of configuration options. For example, f-strings are not available by default on some feature levels, and thus the test suite won't run cleanly on certain targets unless the support for that feature is explicitly enabled.
See the discussion at micropython/micropython#19111 for more information.
I've had a quick look at ruff's available rules and I didn't find a ruff rule that would prevent f-strings to reappear in the future, otherwise I'd have put the restriction in place for this file.
As a bonus, now the module is 31 bytes shorter :)
Testing
Overwriting the
__init__.pyfile on a board compiled withMICROPY_CONFIG_ROM_LEVEL_CORE_FEATURESdoesn't trigger aSyntaxErrorexception anymore. Tests in MicroPython's test suite that depend onunittestto be available on device now execute.Generative AI
I did not use generative AI tools when creating this PR.