Repository navigation
[mypyc] Use __format__ in specialized str.format() and f-strings - #22564
Open
Jahongir-Qurbonov wants to merge 1 commit into
Open
Jahongir-Qurbonov wants to merge 1 commit into
Jahongir-Qurbonov wants to merge 1 commit into
Conversation
The specialized versions of str.format() and f-strings converted values using str(), which ignored __format__ (for example, of str/int enums or of str subclasses). Use format() instead. Also use str() for values with static type str in printf-style formatting, since they may be instances of str subclasses. Native classes that don't define __format__ (and whose subclasses don't define it either) still use str(), which is faster.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The specialized versions of
str.format()and f-strings converted values usingstr()instead offormat(), so__format__was ignored:__format__was formatted using__str__in"{}".format(obj)andf"x{obj}y".class E(str, Enum)/IntEnummembers are formatted usingEnum.__format__, which on Python 3.10 uses the value (f"x{E.A}y"gives"xay", mypyc gave"xE.Ay"). On 3.11+ it's the other way around for a value with static typestr:e: str = E.A; f"x{e}y"gives"xE.Ay", mypyc gave"xay".strwas used as is, even if it was an instance of astrsubclass that overrides__format__.Printf-style formatting (
"%s" % x) had the related issue that a value with static typestrwas used as is, so__str__of astrsubclass wasn't called (e: str = E.A; "%s" % egave"a"instead of"E.A").Now
str.format()and f-strings useformat(x)(PyObject_Format, through a helper with an inline fast path for exactstrobjects), and%usesstr(x)for all values that aren'tint. Instances of native classes still usestr(x)instr.format()and f-strings if the class and all its subclasses are known and none of them define__format__, since callingobject.__format__is quite a bit slower.intvalues still use the existing fast path. Anintsubclass instance with static typeintmay not keep its type in compiled code anyway.