Skip to content

Enum works in python 3.10 breaks in 3.11 #104271

Description

@naringas

Bug report

I have this (simplified) code

from enum import Enum, auto, unique

@unique
class TokenType(Enum):
	# // Single-character tokens.
	LEFT_PAREN = '('
	
	# // Literals.
	IDENTIFIER = auto()
	STRING = auto()
	NUMBER = auto()
	
	# // Keywords.
	AND = 'and'
	
	WHILE = 'while'

	EOF = auto()

I tested this (I developed it) using python 3.10.8.

My friend ran it in 3.11.3 and it breaks like this:

  File ".../3.11.3/lib/python3.11/enum.py", line 1561, in unique
    raise ValueError('duplicate values found in %r: %s' %
ValueError: duplicate values found in <enum 'TokenType'>: NUMBER -> STRING, EOF -> STRING

We can work around the problem using a StrEnum and it's all fine. But I think this is a regression bug. I don't think my code should break like this because the standard library changed.
Also, fyi, StrEnum is new in 3.11 so I cannot just adjust my own code (which works well).

Linked PRs

Activity

  1. added a commit that references this issue on May 7, 2023
  2. itamaro commented on May 7, 2023

    @itamaro
    Contributor

    I confirmed the issue and proposed a fix in gh-104279

    I think iterating the list of last_values in gh-91457 in sorted order (instead of reversed sorted order) was an oversight

    in any case, @naringas, it seems that this use of auto() will be deprecated in 3.13.
    you should be able to fix this in your code by overriding _generate_next_value_ to get your desired behavior in a way that works with older python versions.

  3. naringas commented on May 7, 2023

    @naringas
    Author

    in any case, @naringas, it seems that this use of auto() will be deprecated in 3.13. you should be able to fix this in your code by overriding _generate_next_value_ to get your desired behavior in a way that works with older python versions.

    I think the issue came from how I use Enum with mixed values; I have values either as strings, or 'auto'.

    Now that I've had to think about this, I realize expect unique (hence the decorator to error out if this fails) numerical values. I think this is a valid expectation for a unique Enum type with mixed values.

    question: this issue gets closed if/when the PR gets merged?

  4. sunmy2019 commented on May 8, 2023

    @sunmy2019
    Member
  5. itamaro commented on May 8, 2023

    @itamaro
    Contributor

    question: this issue gets closed if/when the PR gets merged?

    I think so, although it's up to the codeowner (Ethan) to decide if and how this should be fixed

  6. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    3.12only security fixes
    on May 8, 2023
  7. itamaro commented on May 20, 2023

    @itamaro
    Contributor

    @ethanfurman what do you think about this issue and proposed fix?

  8. ethanfurman commented on May 22, 2023

    @ethanfurman
    Member

    Apologies for the delay, I'll try to get to this this week. In general I agree with the proposed solution.

  9. self-assigned this
    on May 23, 2023
  10. added a commit that references this issue on May 23, 2023
  11. added a commit that references this issue on May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions