This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Regression in IntFlag behaviour in f-string
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, dom1310df, eli.bendersky, eric.smith, ethan.furman, miss-islington, rt121212121
Priority: normal Keywords: patch

Created on 2020-10-02 02:38 by rt121212121, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22497 merged ethan.furman, 2020-10-02 17:21
PR 23703 merged miss-islington, 2020-12-08 19:14
PR 23704 merged miss-islington, 2020-12-08 19:15
Messages (6)
msg377793 - (view) Author: Roger Taylor (rt121212121) Date: 2020-10-02 02:38
An IntFlag member before 3.8.6 was converted to an integer in an f-string. After 3.8.6, the textual IntFlag class and member name are placed in the interpolated f-string instead of the integer.

3.8.3: f"... {X.F} ..." where X.F = 1 << 4 will give "... 16 ..."
3.8.5: Same
3.8.6: f"... {X.F} ..." where X.F = 1 << 4 will give "... X.F ..."

I have reproduced this on Linux using the version compiled and installed by pyenv, and on Windows using the 32-bit versions from the downloadable standalone installer.

Now I have to locate and go through all my SQL statements and verify that they explicitly convert IntFlag values to integer to avoid the errors introduced by this regression.
msg377804 - (view) Author: Dominic Davis-Foster (dom1310df) Date: 2020-10-02 08:46
I believe the regression is due to GH-14545 which "fixed" bpo-37479. The news entry states:

"When `Enum.__str__` is overridden in a derived class, the override will be used by `Enum.__format__` regardless of whether mixin classes are present."

.

The change is in the __format__ method of Enum. It now checks if type(self).__str__ != Enum.__str__ and if True calls str(self). Because Flag defines its own __str__ method, the expression evaluates to True. I do not think this is the indented behaviour.

In 3.8.5 str(self) was only called if _member_type_ is object.
msg377810 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-10-02 13:56
Thank you for the bug report.

Another solution would be to subclass IntFlag and specify the `__format__` method:


    from enum import IntFlag as _IntFlag
    class IntFlag(_IntFlag):
        def __format__(self, spec):
            return format(self.value, spec)
msg382759 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-12-08 19:14
New changeset 37440eef7f9a0c27e13fc9ce0850574bb00688b0 by Ethan Furman in branch 'master':
bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497)
https://github.com/python/cpython/commit/37440eef7f9a0c27e13fc9ce0850574bb00688b0
msg382762 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-12-08 19:52
New changeset 14eaa7d75282d8458455c41e9e871c56db8b9a10 by Miss Islington (bot) in branch '3.9':
bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) (GH-23703)
https://github.com/python/cpython/commit/14eaa7d75282d8458455c41e9e871c56db8b9a10
msg382763 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-12-08 19:53
New changeset cbfcc67170d459bcf3e9d63d2f44eadec740bf69 by Miss Islington (bot) in branch '3.8':
bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) (GH-23704)
https://github.com/python/cpython/commit/cbfcc67170d459bcf3e9d63d2f44eadec740bf69
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86073
2020-12-08 19:54:13ethan.furmansetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-12-08 19:53:06ethan.furmansetmessages: + msg382763
2020-12-08 19:52:36ethan.furmansetmessages: + msg382762
2020-12-08 19:15:08miss-islingtonsetpull_requests: + pull_request22571
2020-12-08 19:14:57miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22570
2020-12-08 19:14:24ethan.furmansetmessages: + msg382759
2020-10-02 17:31:10ethan.furmansetcomponents: + Library (Lib), - Interpreter Core
2020-10-02 17:21:22ethan.furmansetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request21515
2020-10-02 13:56:19ethan.furmansetassignee: ethan.furman
stage: needs patch
messages: + msg377810
versions: + Python 3.9, Python 3.10
2020-10-02 08:46:48dom1310dfsetnosy: + dom1310df
messages: + msg377804
2020-10-02 07:55:44eric.smithsetnosy: + eric.smith
2020-10-02 04:10:30xtreaksetnosy: + barry, eli.bendersky, ethan.furman
2020-10-02 02:38:10rt121212121create