Skip to content

Commit

Permalink
Bugfix for enums in python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Nov 12, 2024
1 parent fe4c10b commit 03e6959
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/roman_datamodels/dqflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@
the formula `2**bit_number` where `bit_number` is the 0-index bit of interest.
"""

from enum import Enum, unique
import sys
from enum import unique

import numpy as np
# Something with pickling of multiclassed enums was changed in 3.11 + allowing
# us to directly us `np.uint32` as the enum object rather than a python `int`.
if sys.version_info < (3, 11):
from enum import IntEnum
else:
from enum import Enum

import numpy as np

class IntEnum(np.uint32, Enum): ...


# fmt: off
@unique
class pixel(np.uint32, Enum):
class pixel(IntEnum):
"""Pixel-specific data quality flags"""

GOOD = 0 # No bits set, all is good
Expand Down Expand Up @@ -64,7 +74,7 @@ class pixel(np.uint32, Enum):


@unique
class group(np.uint32, Enum):
class group(IntEnum):
"""Group-specific data quality flags
Once groups are combined, these flags are equivalent to the pixel-specific flags.
"""
Expand Down
11 changes: 9 additions & 2 deletions tests/test_dqflags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from math import log10

import numpy as np
Expand Down Expand Up @@ -31,7 +32,10 @@ def test_pixel_flags(flag):
assert isinstance(flag, dqflags.pixel)

# Test that the pixel flags are ints
assert isinstance(flag, np.uint32)
if sys.version_info < (3, 11):
assert isinstance(flag, int)
else:
assert isinstance(flag, np.uint32)

# Test that the pixel flags are dict accessible
assert dqflags.pixel[flag.name] is flag
Expand Down Expand Up @@ -79,7 +83,10 @@ def test_group_flags(flag):
assert isinstance(flag, dqflags.group)

# Test that the group flags are ints
assert isinstance(flag, np.uint32)
if sys.version_info < (3, 11):
assert isinstance(flag, int)
else:
assert isinstance(flag, np.uint32)

# Test that the group flags are dict accessible
assert dqflags.group[flag.name] is flag
Expand Down

0 comments on commit 03e6959

Please sign in to comment.