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

Regression in IntFlag behaviour in f-string #86073

Closed
rt121212121 mannequin opened this issue Oct 2, 2020 · 6 comments
Closed

Regression in IntFlag behaviour in f-string #86073

rt121212121 mannequin opened this issue Oct 2, 2020 · 6 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rt121212121
Copy link
Mannequin

rt121212121 mannequin commented Oct 2, 2020

BPO 41907
Nosy @warsaw, @ericvsmith, @ethanfurman, @miss-islington
PRs
  • bpo-41907: fix format() behavior for IntFlag #22497
  • [3.9] bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) #23703
  • [3.8] bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) #23704
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2020-12-08.19:54:13.182>
    created_at = <Date 2020-10-02.02:38:10.890>
    labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
    title = 'Regression in IntFlag behaviour in f-string'
    updated_at = <Date 2020-12-08.19:54:13.181>
    user = 'https://bugs.python.org/rt121212121'

    bugs.python.org fields:

    activity = <Date 2020-12-08.19:54:13.181>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2020-12-08.19:54:13.182>
    closer = 'ethan.furman'
    components = ['Library (Lib)']
    creation = <Date 2020-10-02.02:38:10.890>
    creator = 'rt121212121'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41907
    keywords = ['patch']
    message_count = 6.0
    messages = ['377793', '377804', '377810', '382759', '382762', '382763']
    nosy_count = 7.0
    nosy_names = ['barry', 'eric.smith', 'eli.bendersky', 'ethan.furman', 'rt121212121', 'miss-islington', 'dom1310df']
    pr_nums = ['22497', '23703', '23704']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41907'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @rt121212121
    Copy link
    Mannequin Author

    rt121212121 mannequin commented Oct 2, 2020

    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.

    @rt121212121 rt121212121 mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Oct 2, 2020
    @dom1310df
    Copy link
    Mannequin

    dom1310df mannequin commented Oct 2, 2020

    I believe the regression is due to #58750 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.

    @ethanfurman
    Copy link
    Member

    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)

    @ethanfurman ethanfurman added 3.9 only security fixes 3.10 only security fixes labels Oct 2, 2020
    @ethanfurman ethanfurman self-assigned this Oct 2, 2020
    @ethanfurman ethanfurman added 3.9 only security fixes 3.10 only security fixes labels Oct 2, 2020
    @ethanfurman ethanfurman self-assigned this Oct 2, 2020
    @ethanfurman ethanfurman added stdlib Python modules in the Lib dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Oct 2, 2020
    @ethanfurman
    Copy link
    Member

    New changeset 37440ee by Ethan Furman in branch 'master':
    bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497)
    37440ee

    @ethanfurman
    Copy link
    Member

    New changeset 14eaa7d by Miss Islington (bot) in branch '3.9':
    bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) (GH-23703)
    14eaa7d

    @ethanfurman
    Copy link
    Member

    New changeset cbfcc67 by Miss Islington (bot) in branch '3.8':
    bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) (GH-23704)
    cbfcc67

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant