mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
70a460c46a
Give up on pydantic.
29 lines
1.0 KiB
Python
29 lines
1.0 KiB
Python
from enum import Enum
|
|
|
|
|
|
def enum_value_to_index(enum_value: Enum):
|
|
"""Converts an enum value to its index"""
|
|
return list(enum_value.__class__.__members__).index(enum_value.value)
|
|
|
|
|
|
# TODO: once tuple_tree_generator becomes kind-aware, this will no longer be necessary
|
|
def force_constructor_kwarg(base_class: type, kwarg_name, kwarg_value):
|
|
"""Monkeypatches the __init__ method so that the given kwarg is forced to the given value.
|
|
If the argument was not provided it is set, if it was provided and it was different from the forced value a
|
|
ValueError is raised.
|
|
"""
|
|
assert isinstance(base_class, type)
|
|
|
|
original_init = base_class.__init__
|
|
|
|
def init_forcing_value(self, *args, **kwargs):
|
|
if kwarg_name not in kwargs:
|
|
kwargs[kwarg_name] = kwarg_value
|
|
else:
|
|
if kwargs[kwarg_name] != kwarg_value:
|
|
raise ValueError(f"Invalid value provided for {kwarg_name}")
|
|
|
|
original_init(self, *args, **kwargs)
|
|
|
|
base_class.__init__ = init_forcing_value
|