Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ specific `IntEnum` type are being assigned.

## License

Copyright (C) 2024-2025 credativ GmbH https://www.credativ.de/en/
Copyright (C) 2024-2026 credativ GmbH https://www.credativ.de/en/

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ classifiers = [
"Typing :: Typed"
]
dynamic = ["version"]
requires-python = ">=3.8"
requires-python = ">=3.12"
dependencies = []

[project.urls]
Expand Down Expand Up @@ -65,7 +65,7 @@ explicit_package_bases = true
[tool.ruff]
extend-exclude = ["src/multiprocessing_intenum/_version.py"]
line-length = 119
target-version = "py311"
target-version = "py312"

#[tool.ruff.format]

Expand Down
47 changes: 40 additions & 7 deletions src/multiprocessing_intenum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,57 @@
import multiprocessing
from enum import IntEnum
from multiprocessing.synchronize import Lock, RLock
from typing import Any, Generic, TypeVar, get_args
from types import get_original_bases
from typing import Any, get_args, get_origin

from ._version import version as __version__ # noqa: F401

T = TypeVar("T", bound=IntEnum)


class IntEnumValue(Generic[T]):
class IntEnumValue[T: IntEnum]:
"""A multiprocessing safe shared object for `IntEnum` enum values."""

# Pre-initialize type here to avoid numerous type ignores elsewhere
EnumType: type[T | IntEnum] = IntEnum

@classmethod
def __init_subclass__(cls) -> None: # noqa: D105
# set EnumType to the specific type specified by subclass
orig_base = cls.__orig_bases__[0] # type: ignore[attr-defined]
cls.EnumType = get_args(orig_base)[0]
# To provide extended type checking, we need to determine the specigic
# type of IntEnum we have been subclassed with and set the EnumType
# attribute accordingly.
#
# The classes that we have been subclassed from can fall into the
# following cases:
# 1. a generic class with a specific type given
# 2. a subclass of a generic class without a new specific type given
# 3. our base class or a subclass of it without a specific type given
# 4. a class that is not a subclass of us, e.g. a mixin class
#
# In case 1, our original bases do contain the generic class with its
# type argument included.
# In cases 2 and 3, our original bases do contain the class itself only.
# ie. with the type argument *not* included.
# In case 2, the class or a parent of it must itself have been case 1,
# thus our EnumType attribute is resolved to the correct value as per
# MRO already.
# In case 3, the EnumType attribute remains the default and no extended
# type checking will happen.
# In case 4, the class is to be ignored.
#
# Multiple inheritance (as in case 4) needs to be taken into account,
# we thus iterate over original bases in MRO and terminate if one of
# the conclusive cases 1-3 is identified.
#
for base in get_original_bases(cls):
# check if parent is a generic class that is a subclass of us (case 1)
origin = get_origin(base)
if origin is not None and issubclass(origin, IntEnumValue):
# set EnumType to the specific type of the generic class
cls.EnumType = get_args(base)[0]
break

# check if parent is a subclass of us (cases 2 and 3)
if isinstance(base, type) and issubclass(base, IntEnumValue):
break

def __init__(self, value: T | str, lock: None | Lock | RLock = None) -> None:
"""Initialize IntEnumValue object.
Expand Down
42 changes: 42 additions & 0 deletions tests/test_intenum.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,53 @@ class Bar(IntEnumValue[BarEnum]):
pass


class TestMixin:
pass


class MixedFoo(TestMixin, IntEnumValue[FooEnum]):
pass


class IntEnumValueTestCase(TestCase):
def test_init(self) -> None:
foo = Foo(FooEnum.FOO)
self.assertEqual(foo, FooEnum.FOO)

def test_init_no_specific_type(self) -> None:
class Baz(IntEnumValue):
pass

self.assertEqual(Baz.EnumType, IntEnum) # type: ignore[misc]

def test_init_subsubclass(self) -> None:
class Baz(Bar[BarEnum]):
pass

self.assertEqual(Baz.EnumType, BarEnum) # type: ignore[misc]

def test_init_subsubclass_no_specific_type(self) -> None:
class Baz(Bar):
pass

self.assertEqual(Baz.EnumType, BarEnum) # type: ignore[misc]

def test_init_mixin(self) -> None:
foo = MixedFoo(FooEnum.FOO)
self.assertEqual(foo, FooEnum.FOO)

def test_init_multi_inheritance_mro(self) -> None:
class FooOrBar(Foo, Bar):
pass

self.assertEqual(FooOrBar.EnumType, FooEnum) # type: ignore[misc]

def test_init_multi_inheritance_mro_generic(self) -> None:
class FooOrBar(Foo, IntEnumValue[BarEnum]):
pass

self.assertEqual(FooOrBar.EnumType, FooEnum) # type: ignore[misc]

def test_init_name(self) -> None:
foo = Foo(FooEnum.FOO.name)
self.assertEqual(foo, FooEnum.FOO)
Expand Down
Loading
Loading