Skip to content

fix: reject boolean values for integer flags - #621

Open
Hexecu wants to merge 2 commits into
open-feature:mainfrom
Hexecu:fix/reject-boolean-integer-flags
Open

fix: reject boolean values for integer flags#621
Hexecu wants to merge 2 commits into
open-feature:mainfrom
Hexecu:fix/reject-boolean-integer-flags

Conversation

@Hexecu

@Hexecu Hexecu commented Aug 30, 2026

Copy link
Copy Markdown

This PR

I reproduced the behavior reported by @aepfli in #619: requesting a boolean flag through get_integer_details returns the boolean as a successful evaluation, instead of returning the integer default with TYPE_MISMATCH.

The caller gets neither the expected type nor an indication that the flag is misconfigured for that request. The fallback is silently bypassed because Python considers bool a subclass of int, so isinstance(True, int) passes the client's type check.

Reproducing the problem

This example uses the built-in provider; no flag server or credentials are needed. Run it with uv run --frozen python repro.py after saving it as repro.py in the checkout:

import asyncio

from openfeature import api
from openfeature.provider.in_memory_provider import InMemoryFlag, InMemoryProvider


async def main():
    api.set_provider_and_wait(
        InMemoryProvider({"flag": InMemoryFlag("on", {"on": True})})
    )
    try:
        client = api.get_client()
        for mode, details in (
            ("sync", client.get_integer_details("flag", 1)),
            ("async", await client.get_integer_details_async("flag", 1)),
        ):
            error = details.error_code.value if details.error_code else None
            print(
                f"{mode}: {details.value!r} ({type(details.value).__name__}), "
                f"{details.reason.value}, {error}"
            )
    finally:
        api.shutdown()


asyncio.run(main())

On the base commit, 371aca1:

sync: True (bool), STATIC, None
async: True (bool), STATIC, None

With this patch, the same example returns the supplied default and reports the mismatch:

sync: 1 (int), ERROR, TYPE_MISMATCH
async: 1 (int), ERROR, TYPE_MISMATCH

I also checked False with a default of 0. The type matters here: a test that only checks equality would pass even before the fix, since True == 1 and False == 0.

Why there are two changes

The shared type check now excludes booleans when the requested type is INTEGER. I kept isinstance for everything else so this doesn't also reject valid integer subclasses or change how other flag types are handled.

That change alone wasn't enough for the async API. While checking it, I found that the async type-mismatch branch still constructed its result with resolution.value. It reported an error, but returned the rejected value anyway. The second change uses default_value in that branch, matching the sync behavior.

This also corrects the async fallback for other client-detected type mismatches, which is why the tests cover more than bool-to-int. It doesn't change provider implementations or introduce type coercion. Boolean flags and objects containing booleans continue to work as before.

Related Issues

Fixes #619. Thanks @aepfli for the reproduction and the conformance test that caught this.

How to test

The added tests exercise both value and details getters through the real InMemoryProvider. They check the exact return type as well as the value, error details and hook behavior: a mismatch must run the error hook, skip the after hook, and give the finally hook the fallback value. There are also passing cases for valid flag values and an integer subclass, to check that the stricter bool handling doesn't affect them.

To run just these tests:

uv run --frozen pytest tests/test_client.py -q \
  -k 'client_returns_default_on_type_mismatch or client_preserves_matching_flag_types or typecheck_flag_value_accepts_integer_subclasses'

I ran the same 39 cases against the unmodified base and this patch. Before the fix, 11 failed and 28 passed: the failures were the two sync bool-to-int cases and the nine async mismatch cases. After the fix, all 39 passed.

The full suite passed on Python 3.10 through 3.14, with 211 tests on each version. The repository's Gherkin suite also passed on each version: 21 scenarios / 84 steps, using its pinned spec and in-memory provider. Ruff, mypy, the remaining pre-commit hooks, and the wheel/sdist build passed locally as well.

UV_FROZEN=1 uv run --frozen poe test-all
UV_FROZEN=1 uv run --frozen poe e2e
UV_FROZEN=1 uv run --frozen prek run --all-files --show-diff-on-failure
uv build --no-sources

Notes

For a check independent of the tests added here, I ran the in-memory self-test from python-sdk-contrib#409, at d6de5dc, against both SDK versions. The case marked as a known failure for #619 becomes XPASS(strict) with this patch. Running that unchanged test file with --runxfail, so the assertions run normally, gives 24 passed and 5 skipped. The skips are unsupported provider capabilities; the strict marker will need updating when that suite adopts the fixed SDK.

All results above are local macOS runs. I haven't tested against live flagd/OFREP servers, and these results don't replace hosted CI.

@Hexecu
Hexecu requested review from a team as code owners August 30, 2026 05:50
@coderabbitai

coderabbitai Bot commented Aug 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c7be5192-560e-4f15-b83b-f8d2423f4ab6

📥 Commits

Reviewing files that changed from the base of the PR and between e530620 and 10f26c1.

📒 Files selected for processing (2)
  • openfeature/client.py
  • tests/test_client.py

Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The client now rejects booleans for integer flags and returns the caller’s default value for asynchronous type mismatches. Tests cover matching and mismatched values across synchronous and asynchronous getters.

Changes

Flag type validation

Layer / File(s) Summary
Type mismatch validation
openfeature/client.py, tests/test_client.py
Integer checks reject boolean values while accepting integer subclasses. Tests cover matching and mismatched flag types.
Evaluation default handling
openfeature/client.py, tests/test_client.py
Asynchronous mismatches return the caller’s default value. Tests verify error details, hooks, and sync/async getters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 10f26

The change makes integer evaluations reject boolean values and consistently return the configured default with a type-mismatch error, including through the async API. No actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: gruebel

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: rejecting boolean values for integer flags.
Description check ✅ Passed The description directly explains issue #619, the boolean-to-integer mismatch, the async fallback correction, and the regression tests.
Linked Issues check ✅ Passed The changes satisfy issue #619. Integer requests reject boolean values, return the caller's default with TYPE_MISMATCH, and continue to accept valid integer subclasses.
Out of Scope Changes check ✅ Passed The async fallback correction and expanded tests are directly related to the stated type-mismatch behavior. No unrelated code changes are identified.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files.

Comment @coderabbitai help to get the list of available commands.

Exclude bool from integer flag type checks while preserving other integer subclasses. Return the caller default when the async client detects a type mismatch, matching the sync path.

Cover sync and async value/details getters, exact fallback types, hook behavior, and valid flag values with regression tests for open-feature#619.

Signed-off-by: Hexecu <vaingloryhex@gmail.com>
@Hexecu
Hexecu force-pushed the fix/reject-boolean-integer-flags branch from e530620 to 71fe148 Compare August 30, 2026 06:08
Document the boolean/integer distinction, async type-mismatch fallback, and the regression cases. Keep the evaluation logic and test assertions unchanged.

Signed-off-by: Hexecu <vaingloryhex@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A boolean flag satisfies an Integer request: bool is a subclass of int

1 participant