Skip to content

Commit 793ced1

Browse files
federicobondbeeme1mrtoddbaert
authored
refactor!: simplify namespaces to make public API more pythonic (#172)
* refactor!: simplify namespaces to make public API more pythonic Signed-off-by: Federico Bond <federicobond@gmail.com> Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com> Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
1 parent 6f7cdb8 commit 793ced1

35 files changed

+286
-178
lines changed

open_feature/hooks/hook_support.py renamed to open_feature/_internal/hook_support.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
import typing
33
from functools import reduce
44

5-
from open_feature.evaluation_context.evaluation_context import EvaluationContext
6-
from open_feature.flag_evaluation.flag_evaluation_details import FlagEvaluationDetails
7-
from open_feature.flag_evaluation.flag_type import FlagType
8-
from open_feature.hooks.hook import Hook
9-
from open_feature.hooks.hook_context import HookContext
10-
from open_feature.hooks.hook_type import HookType
5+
from open_feature.evaluation_context import EvaluationContext
6+
from open_feature.flag_evaluation import FlagEvaluationDetails, FlagType
7+
from open_feature.hook import Hook, HookContext, HookType
118

129

1310
def error_hooks(

open_feature/open_feature_api.py renamed to open_feature/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import typing
22

3-
from open_feature.evaluation_context.evaluation_context import EvaluationContext
4-
from open_feature.exception.exceptions import GeneralError
5-
from open_feature.hooks.hook import Hook
6-
from open_feature.open_feature_client import OpenFeatureClient
3+
from open_feature.client import OpenFeatureClient
4+
from open_feature.evaluation_context import EvaluationContext
5+
from open_feature.exception import GeneralError
6+
from open_feature.hook import Hook
77
from open_feature.provider.metadata import Metadata
88
from open_feature.provider.no_op_provider import NoOpProvider
99
from open_feature.provider.provider import AbstractProvider

open_feature/open_feature_client.py renamed to open_feature/client.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
import typing
33
from dataclasses import dataclass
44

5-
from open_feature import open_feature_api as api
6-
from open_feature.evaluation_context.evaluation_context import EvaluationContext
7-
from open_feature.exception.error_code import ErrorCode
8-
from open_feature.exception.exceptions import (
5+
from open_feature import api
6+
from open_feature.evaluation_context import EvaluationContext
7+
from open_feature.exception import (
8+
ErrorCode,
99
GeneralError,
1010
OpenFeatureError,
1111
TypeMismatchError,
1212
)
13-
from open_feature.flag_evaluation.flag_evaluation_details import FlagEvaluationDetails
14-
from open_feature.flag_evaluation.flag_evaluation_options import FlagEvaluationOptions
15-
from open_feature.flag_evaluation.flag_type import FlagType
16-
from open_feature.flag_evaluation.reason import Reason
17-
from open_feature.flag_evaluation.resolution_details import FlagResolutionDetails
18-
from open_feature.hooks.hook import Hook
19-
from open_feature.hooks.hook_context import HookContext
20-
from open_feature.hooks.hook_support import (
13+
from open_feature.flag_evaluation import (
14+
FlagEvaluationDetails,
15+
FlagEvaluationOptions,
16+
FlagType,
17+
Reason,
18+
FlagResolutionDetails,
19+
)
20+
from open_feature.hook import Hook, HookContext
21+
from open_feature.hook.hook_support import (
2122
after_all_hooks,
2223
after_hooks,
2324
before_hooks,

open_feature/exception/exceptions.py renamed to open_feature/exception.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import typing
2+
from enum import Enum
23

3-
from open_feature.exception.error_code import ErrorCode
4+
5+
class ErrorCode(Enum):
6+
PROVIDER_NOT_READY = "PROVIDER_NOT_READY"
7+
FLAG_NOT_FOUND = "FLAG_NOT_FOUND"
8+
PARSE_ERROR = "PARSE_ERROR"
9+
TYPE_MISMATCH = "TYPE_MISMATCH"
10+
TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING"
11+
INVALID_CONTEXT = "INVALID_CONTEXT"
12+
GENERAL = "GENERAL"
413

514

615
class OpenFeatureError(Exception):

open_feature/exception/__init__.py

Whitespace-only changes.

open_feature/exception/error_code.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

open_feature/flag_evaluation.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from __future__ import annotations
2+
import typing
3+
from dataclasses import dataclass, field
4+
5+
from open_feature._backports.strenum import StrEnum
6+
from open_feature.exception import ErrorCode
7+
8+
if typing.TYPE_CHECKING: # resolves a circular dependency in type annotations
9+
from open_feature.hook import Hook
10+
11+
12+
class FlagType(StrEnum):
13+
BOOLEAN = "BOOLEAN"
14+
STRING = "STRING"
15+
OBJECT = "OBJECT"
16+
FLOAT = "FLOAT"
17+
INTEGER = "INTEGER"
18+
19+
20+
class Reason(StrEnum):
21+
CACHED = "CACHED"
22+
DEFAULT = "DEFAULT"
23+
DISABLED = "DISABLED"
24+
ERROR = "ERROR"
25+
STATIC = "STATIC"
26+
SPLIT = "SPLIT"
27+
TARGETING_MATCH = "TARGETING_MATCH"
28+
UNKNOWN = "UNKNOWN"
29+
30+
31+
T = typing.TypeVar("T", covariant=True)
32+
33+
34+
@dataclass
35+
class FlagEvaluationDetails(typing.Generic[T]):
36+
flag_key: str
37+
value: T
38+
variant: typing.Optional[str] = None
39+
reason: typing.Optional[Reason] = None
40+
error_code: typing.Optional[ErrorCode] = None
41+
error_message: typing.Optional[str] = None
42+
43+
44+
@dataclass
45+
class FlagEvaluationOptions:
46+
hooks: typing.List[Hook] = field(default_factory=list)
47+
hook_hints: dict = field(default_factory=dict)
48+
49+
50+
U = typing.TypeVar("U", covariant=True)
51+
52+
53+
@dataclass
54+
class FlagResolutionDetails(typing.Generic[U]):
55+
value: U
56+
error_code: typing.Optional[ErrorCode] = None
57+
error_message: typing.Optional[str] = None
58+
reason: typing.Optional[Reason] = None
59+
variant: typing.Optional[str] = None
60+
flag_metadata: typing.Optional[str] = None

open_feature/flag_evaluation/__init__.py

Whitespace-only changes.

open_feature/flag_evaluation/flag_evaluation_details.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)