Skip to content

feat: migrate callers off FEATURES-as-dict access#38772

Draft
feanil wants to merge 4 commits into
masterfrom
feanil/features-dict-migration
Draft

feat: migrate callers off FEATURES-as-dict access#38772
feanil wants to merge 4 commits into
masterfrom
feanil/features-dict-migration

Conversation

@feanil

@feanil feanil commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Stacked on top of #38771 (the easy deprecation fixes). Goal: migrate every caller off of `settings.FEATURES['X'] = …` / `patch.dict(settings.FEATURES, …)` / `settings.FEATURES.update(…)` and onto plain settings access. Per Feanil: the underlying settings already exist (FEATURES was previously flattened into the common.py settings modules), so callers can move directly to `settings.X` / `@override_settings(X=…)`.

Currently a single `temp:` commit that fixes `FeaturesProxy`'s stacklevel so the warnings actually point at the call sites instead of always reporting line 89 of the proxy itself. Once CI runs and surfaces the full list, real migration commits will follow.

@feanil feanil force-pushed the feanil/fix-deprecation-warnings branch from 1b8c4e8 to 7f5dbc2 Compare June 16, 2026 18:52
@feanil feanil force-pushed the feanil/features-dict-migration branch 3 times, most recently from 9ffdb02 to f9359f9 Compare June 17, 2026 14:27
Base automatically changed from feanil/fix-deprecation-warnings to master June 17, 2026 15:33
Pushed standalone first so CI surfaces the full list of caller
sites before the migration commits land.

FeaturesProxy.__setitem__ used stacklevel=2 and update() delegated
to __setitem__, so every warning was reported at update()'s own
'self[key] = other[key]' line in this file (89) instead of the
test or app code that actually mutated FEATURES.

Why the helper instead of just bumping stacklevel: stacklevel is
a frame count from warnings.warn upward. Bumping __setitem__'s
stacklevel from 2 to 3 would fix the update()->__setitem__ path
but would overshoot by one frame on direct __setitem__ calls
(e.g. fp['X'] = True), reporting from the caller's caller. The
two paths need different stacklevel arithmetic.

Factors the emit into _warn_dict_access(stacklevel=3) so both
__setitem__ and update() can call it with the same constant; each
contributes exactly one intermediate frame. update() now writes to
self.ns directly to avoid double-warning. No behavior change
beyond the reported source location.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@feanil feanil force-pushed the feanil/features-dict-migration branch from f9359f9 to 06d3986 Compare June 17, 2026 15:41
feanil and others added 3 commits June 17, 2026 12:10
Lets @override_settings(KEY=val) be seen through both settings.KEY
and settings.FEATURES['KEY']/.get('KEY'), so tests can migrate to
the flat setting without first updating every production reader
that still does settings.FEATURES['KEY'].

We walk only the UserSettingsHolder chain that @override_settings
pushes onto django.conf.settings._wrapped, NOT the bottom Settings
layer. Django's bottom Settings is a *snapshot* of the settings
module's globals taken at init time, so it doesn't reflect runtime
mutations of the module's globals — which is exactly what legacy
patch.dict(settings.FEATURES, ...) does via proxy.ns. Reading the
snapshot would mask those mutations and break legacy tests; walking
only the explicit override layers lets both paths coexist during
the migration.

ns kept as-is. A stateless redesign (drop ns, route everything
through django.conf.settings, set FEATURES = FeaturesProxy() in
each env file) was considered but rejected for this step because
FEATURES.copy() and __iter__ are widely used to snapshot the full
set of feature flags; without ns there is no clean source of truth
for "which keys are features". Once all callers have moved off the
dict pattern the proxy can be deleted outright (every
settings.FEATURES['X'] becomes settings.X), making the stateless
refactor unnecessary.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Production reads use getattr(settings, ...); tests use
@override_settings(ENABLE_CREATOR_GROUP=...). Mixed-flag patches keep the
other flag in patch.dict until its own migration.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@feanil feanil force-pushed the feanil/features-dict-migration branch from 06d3986 to 3fe8774 Compare June 17, 2026 16:11
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.

1 participant