Summary
Consumers who subclass library types (e.g., Product, Package) need to control Pydantic's extra field policy per-environment:
- Development/CI:
extra="forbid" to catch bugs
- Production:
extra="ignore" for forward compatibility
Currently, every generated type inherits model_config = ConfigDict(extra='allow') from the schemas' additionalProperties: true. Consumers must manually override model_config on every subclass — @KonstantinMirin reports doing this 28 times across 4 schema files in prebid/salesagent.
Proposed Solution
Add a __init_subclass__ hook to AdCPBaseModel that respects a class-level _adcp_extra_mode attribute:
class AdCPBaseModel(BaseModel):
@classmethod
def __init_subclass__(cls, **kwargs: Any) -> None:
super().__init_subclass__(**kwargs)
extra_mode = getattr(cls, '_adcp_extra_mode', None)
if extra_mode is not None and 'extra' not in cls.model_config:
cls.model_config = ConfigDict(extra=extra_mode)
Consumers set it once on their base:
class SalesAgentBaseModel(AdCPBaseModel):
_adcp_extra_mode = "forbid" # or "ignore" in prod
class Product(LibraryProduct):
pass # inherits "forbid" automatically
Why not an env var?
An env var (ADCP_EXTRA_MODE) is simpler but:
- Global — affects all types in the process, including library internals
- Can't scope different policies to different type hierarchies
- Requires process restart to change modes
The __init_subclass__ hook is scoped to consumer subclasses and composable.
Origin
Raised by @KonstantinMirin in #150.
Summary
Consumers who subclass library types (e.g.,
Product,Package) need to control Pydantic'sextrafield policy per-environment:extra="forbid"to catch bugsextra="ignore"for forward compatibilityCurrently, every generated type inherits
model_config = ConfigDict(extra='allow')from the schemas'additionalProperties: true. Consumers must manually overridemodel_configon every subclass — @KonstantinMirin reports doing this 28 times across 4 schema files in prebid/salesagent.Proposed Solution
Add a
__init_subclass__hook toAdCPBaseModelthat respects a class-level_adcp_extra_modeattribute:Consumers set it once on their base:
Why not an env var?
An env var (
ADCP_EXTRA_MODE) is simpler but:The
__init_subclass__hook is scoped to consumer subclasses and composable.Origin
Raised by @KonstantinMirin in #150.