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

3.12.0b4 enum.IntFlag NoneType object is not iterable #106928

Closed
cdce8p opened this issue Jul 20, 2023 · 4 comments
Closed

3.12.0b4 enum.IntFlag NoneType object is not iterable #106928

cdce8p opened this issue Jul 20, 2023 · 4 comments
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

@cdce8p
Copy link
Contributor

cdce8p commented Jul 20, 2023

Bug report

import enum

class bitmap(enum.IntFlag):
    pass

print(bitmap.from_bytes(b'\x00', 'little'))

3.11

bitmap(0)

3.12.0b4

Traceback (most recent call last):
  File "/.../cpython/test.py", line 6, in <module>
    print(bitmap.from_bytes(b'\x00', 'little'))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../cpython/Lib/enum.py", line 742, in __call__
    return cls._create_(
           ^^^^^^^^^^^^^
  File "/.../cpython/Lib/enum.py", line 870, in _create_
    for item in names:
TypeError: 'NoneType' object is not iterable

I bisected the issue to #99500.
/CC: @ethanfurman

Your environment

  • CPython versions tested on: 3.12.0b4
  • Operating system and architecture: macOS ARM64
@cdce8p cdce8p added the type-bug An unexpected behavior, bug, or error label Jul 20, 2023
@ethanfurman ethanfurman self-assigned this Jul 20, 2023
@ethanfurman
Copy link
Member

Thank you for the report!

What is your use-case for this behavior? Typically, an enum with no members is meant to be subclassed.

@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir 3.12 bugs and security fixes 3.13 bugs and security fixes labels Jul 20, 2023
@cdce8p
Copy link
Contributor Author

cdce8p commented Jul 20, 2023

What is your use-case for this behavior? Typically, an enum with no members is meant to be subclassed.

Tbh I stumbled upon this while testing a third party library: https://github.com/zigpy/zigpy
As best as I can tell, they do also subclass it, but bitmap.from_bytes in that case is used in a deserialization routine from a binary struct.

I could post a longer example, just not sure it's all that useful, that's why I condensed it initially.

@ethanfurman
Copy link
Member

Please open an issue with zigpy and reference this one. They should be able to confirm if this is a problem in practice.

@puddly
Copy link

puddly commented Jul 26, 2023

While this new 3.12 behavior is backwards-incompatible with Python 3.11, we've resolved this problem by avoiding the functional API by replacing the enum metaclass (zigpy/zigpy#1236):

import enum


class AlwaysCreateEnumType(enum.EnumMeta):
    """Enum metaclass that skips the functional creation API."""

    def __call__(cls, value, names=None, *values) -> type[enum.Enum]:  # type: ignore
        return cls.__new__(cls, value)  # type: ignore


class bitmap8(
    int,
    enum.ReprEnum,
    enum.Flag,
    boundary=enum.KEEP,
    metaclass=AlwaysCreateEnumType,
):
    pass

bitmap8(0)  # works with 3.11 and 3.12

You can consider this issue resolved if no other projects rely on using the enum classes this way.

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