Skip to content

Commit c3beedc

Browse files
committed
fix: Appease mypy --strict on perf branch
CI runs ``mypy --strict .`` which covers the new ``benchmarks/`` dir and is stricter than the default invocation. Fixes: - ``flagsmith/mappers.py::_variant_priority`` — annotate the dict lookup so it doesn't leak ``Any`` through the ``int`` return type. - ``benchmarks/env.py::build_environment`` — annotate the ``json.load`` result so the ``dict[str, Any]`` return type is satisfied. - ``benchmarks/bench.py::_make_client`` — drop the redundant ``_Flagsmith__evaluation_context`` pre-seed (the property setter sets the backing field itself) and cast the synthetic env dict to ``EnvironmentModel`` at the engine boundary. beep boop
1 parent d0db215 commit c3beedc

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

benchmarks/bench.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@
1919
import pstats
2020
import statistics
2121
import time
22-
from typing import Callable
22+
from typing import Callable, cast
2323

2424
from benchmarks.env import build_environment
2525
from flagsmith import Flagsmith
26+
from flagsmith.api.types import EnvironmentModel
2627
from flagsmith.mappers import map_environment_document_to_context
2728

2829

2930
def _make_client(n_features: int, with_multivariate: int = 0) -> Flagsmith:
30-
env_doc = build_environment(
31-
n_features=n_features,
32-
with_multivariate=with_multivariate,
31+
env_doc = cast(
32+
EnvironmentModel,
33+
build_environment(
34+
n_features=n_features,
35+
with_multivariate=with_multivariate,
36+
),
3337
)
3438
# Build a local-eval client without hitting the network / starting polling.
39+
# The property setter on ``_evaluation_context`` initialises all the
40+
# derived caches we need; everything else below is what ``Flagsmith.__init__``
41+
# would otherwise set during its real construction path.
3542
client = Flagsmith.__new__(Flagsmith)
3643
client.offline_mode = False
3744
client.enable_local_evaluation = True
@@ -40,10 +47,6 @@ def _make_client(n_features: int, with_multivariate: int = 0) -> Flagsmith:
4047
client.enable_realtime_updates = False
4148
client._analytics_processor = None
4249
client._pipeline_analytics_processor = None
43-
client._Flagsmith__evaluation_context = None
44-
client._environment_context_without_segments = None
45-
client._environment_flags_cache = None
46-
client._identity_flags_match_environment = False
4750
client._environment_updated_at = None
4851
client._evaluation_context = map_environment_document_to_context(env_doc)
4952
return client

benchmarks/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def build_environment(
2121
with_multivariate: int = 0,
2222
) -> dict[str, typing.Any]:
2323
with open(TEMPLATE_PATH) as f:
24-
env = json.load(f)
24+
env: dict[str, typing.Any] = json.load(f)
2525

2626
# Base feature state to clone.
2727
base_fs = copy.deepcopy(env["feature_states"][0])

flagsmith/mappers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,5 @@ def _map_environment_document_feature_states_to_feature_contexts(
278278

279279

280280
def _variant_priority(variant: typing.Mapping[str, typing.Any]) -> int:
281-
return variant["priority"]
281+
priority: int = variant["priority"]
282+
return priority

0 commit comments

Comments
 (0)