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

Instantiation of an empty Enum with any value no longer throws ValueError #109022

Closed
Fatal1ty opened this issue Sep 6, 2023 · 2 comments
Closed
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Fatal1ty
Copy link

Fatal1ty commented Sep 6, 2023

Bug report

Bug description:

On Python < 3.12 instantiation of an empty Enum with any value resulted in ValueError, but on 3.12.0rc2 it returns <enum 'bar'> without errors. On 3.12.0rc1 it resulted in TypeError: 'NoneType' object is not iterable that was addressed in this issue:

I'm not sure if it's intended or not but just in case, I'll write about it, so that if it's unintended behaviour it could be fixed before the final release. For me ValueError was more consistent but I don't see a practical reason to use empty enums anyway and I'm fine if this issue is going to be closed.

class Foo(Enum):
    pass

print(Foo('bar'))
# 3.11: ValueError: 'bar' is not a valid Foo
# 3.12.0rc1: TypeError: 'NoneType' object is not iterable
# 3.12.0rc2: <enum 'bar'>

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Linked PRs

@Fatal1ty Fatal1ty added the type-bug An unexpected behavior, bug, or error label Sep 6, 2023
Fatal1ty added a commit to Fatal1ty/mashumaro that referenced this issue Sep 6, 2023
@hugovk hugovk added the stdlib Python modules in the Lib dir label Sep 6, 2023
@hugovk hugovk added 3.12 bugs and security fixes 3.13 bugs and security fixes labels Sep 6, 2023
@ethanfurman
Copy link
Member

ethanfurman commented Sep 6, 2023

In Python 3.10- the code would check the number of arguments to determine if a member lookup or a functional enum creation was intended.

In Python 3.11+ it decides which path to take based on the presence of members:

  • no members defined, must be a functional creation
  • members defined, must be a member lookup

We then ended up with the None is not iterable error in those cases where the functional method was being used to make a new enum class with behavioral changes (i.e. methods), but no new members.

The last bug-fix resolves that issue, but now we have the current issue of creating a new enum class with what looks like a member lookup.

That didn't work before 3.12, so we could certainly go back to making that an error (and perhaps getting better error messages).

ethanfurman added a commit that referenced this issue Sep 8, 2023
…9048)

add guard so that ``Enum('bar')`` raises a TypeError instead of
creating a new enum class called `bar`.  To create the new but
empty class, use:

    huh = Enum('bar', names=())
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 8, 2023
…ythonGH-109048)

add guard so that ``Enum('bar')`` raises a TypeError instead of
creating a new enum class called `bar`.  To create the new but
empty class, use:

    huh = Enum('bar', names=())
(cherry picked from commit c74e440)

Co-authored-by: Ethan Furman <[email protected]>
Yhg1s pushed a commit that referenced this issue Sep 12, 2023
…GH-109048) (#109122)

gh-109022: [Enum] require `names=()` to create empty enum type (GH-109048)

add guard so that ``Enum('bar')`` raises a TypeError instead of
creating a new enum class called `bar`.  To create the new but
empty class, use:

    huh = Enum('bar', names=())
(cherry picked from commit c74e440)

Co-authored-by: Ethan Furman <[email protected]>
@hugovk
Copy link
Member

hugovk commented Nov 9, 2023

Thanks!

@hugovk hugovk closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and 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

4 participants
@Fatal1ty @hugovk @ethanfurman and others