Skip to content

Commit a9f091d

Browse files
Fix compatibility with pydantic<2.12
1 parent 761bdda commit a9f091d

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

tools/python/mbed_tools/build/_internal/config/assemble_build_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _assemble_config_from_sources(
9898
# if it does not pass the schema. This provides compatibility with older projects, as mbed_app.json has
9999
# historically been a total wild west where any internal Mbed state could potentially be overridden.
100100
try:
101-
MbedAppJSON.model_validate(mbed_app_json_dict, extra="forbid", strict=True)
101+
MbedAppJSON.model_validate(mbed_app_json_dict, strict=True)
102102
except pydantic.ValidationError as ex:
103103
logger.warning(
104104
"mbed_app.json5 failed to validate against the schema. This likely means it contains deprecated attributes, misspelled attributes, or overrides for things that should not be set in mbed_app.json5. This version of mbed-os still allows this, but this will change in the future."

tools/python/mbed_tools/build/_internal/config/schemas.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import Union, Literal
1313
from typing_extensions import Self
1414

15-
from pydantic import BaseModel, Field, model_validator
15+
from pydantic import BaseModel, Field, model_validator, ConfigDict
1616

1717
ConfigSettingValue = Union[int, float, bool, str, None]
1818

@@ -23,6 +23,9 @@ class ConfigEntryDetails(BaseModel):
2323
for a library, target, or application.
2424
"""
2525

26+
# Throw an error during validation if there are extra fields
27+
model_config = ConfigDict(extra="forbid")
28+
2629
macro_name: str | None = None
2730
"""
2831
Name of the macro that this setting shall generate. If unset, defaults to
@@ -71,6 +74,9 @@ class MemoryBankConfiguration(BaseModel):
7174
This is a "mini" version of MemoryBankDefinition that just allows setting the size and start address.
7275
"""
7376

77+
# Throw an error during validation if there are extra fields
78+
model_config = ConfigDict(extra="forbid")
79+
7480
start: int | None = None
7581
"""
7682
Start address of the memory region.
@@ -88,6 +94,9 @@ class BaseJSONConfig(BaseModel):
8894
as well as each entry in targets.json5.
8995
"""
9096

97+
# Throw an error during validation if there are extra fields
98+
model_config = ConfigDict(extra="forbid")
99+
91100
config: dict[str, ConfigSettingValue | ConfigEntryDetails] = Field(default_factory=dict)
92101
"""
93102
Configuration items for this library or app. These can be defined directly as "name": default value, or as
@@ -158,6 +167,9 @@ class MbedLibJSON(BaseJSONConfig):
158167
to the Mbed configuration system.
159168
"""
160169

170+
# Throw an error during validation if there are extra fields
171+
model_config = ConfigDict(extra="forbid")
172+
161173
name: str
162174
"""
163175
The name of this library. This is also considered the 'namespace' and will be prepended to all
@@ -196,6 +208,9 @@ class MemoryBankDefinition(BaseModel):
196208
(though this data gets converted from XML to JSON by the cmsis-pack-manager tool)
197209
"""
198210

211+
# Throw an error during validation if there are extra fields
212+
model_config = ConfigDict(extra="forbid")
213+
199214
default: bool = False
200215
"""
201216
Hint from the CMSIS data that this memory region should be used for storing general data/code.
@@ -240,6 +255,9 @@ class TargetJSON(BaseJSONConfig):
240255
Schema for one entry in targets.json5.
241256
"""
242257

258+
# Throw an error during validation if there are extra fields
259+
model_config = ConfigDict(extra="forbid")
260+
243261
inherits: list[str] = Field(default_factory=list)
244262
"""
245263
Parent target(s) to inherit this target's attributes from.
@@ -563,6 +581,9 @@ class MbedAppJSON(BaseJSONConfig):
563581
Schema for mbed_app.json5 files.
564582
"""
565583

584+
# Throw an error during validation if there are extra fields
585+
model_config = ConfigDict(extra="forbid")
586+
566587
target_overrides: dict[str, dict[str, ConfigSettingValue | MemoryBankConfiguration]] = Field(default_factory=dict)
567588
"""
568589
List of overrides applied based on target labels. This is similar to the "overrides" section,

tools/python/mbed_tools/build/_internal/config/source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def from_mbed_lib_json_file(
120120

121121
# Load JSON file using schema
122122
try:
123-
mbed_lib = schemas.MbedLibJSON.model_validate(decode_json_file(mbed_lib_json_path), extra="forbid", strict=True)
123+
mbed_lib = schemas.MbedLibJSON.model_validate(decode_json_file(mbed_lib_json_path), strict=True)
124124
except pydantic.ValidationError as ex:
125125
logger.error(f"{context} did not validate against the schema for mbed_lib.json5!")
126126
raise ex

tools/python/mbed_tools/build/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _load_raw_targets_data(program: MbedProgram) -> dict[str, TargetJSON]:
8484
results = {}
8585
for target, target_json_dict in targets_data.items():
8686
try:
87-
target_json = TargetJSON.model_validate(target_json_dict, extra="forbid", strict=True)
87+
target_json = TargetJSON.model_validate(target_json_dict, strict=True)
8888

8989
# Issue warnings if any config entries have invalid names.
9090
# We need to do this here, or otherwise warnings will only get printed

0 commit comments

Comments
 (0)