Skip to content

Commit e963e30

Browse files
committed
Fix by new variable binding
1 parent f4f356b commit e963e30

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

sphinx_needs/schema/core.py

+35-31
Original file line numberDiff line numberDiff line change
@@ -121,38 +121,38 @@ def reduce_need(
121121

122122
if keep:
123123
coerced_value = value
124-
if (
125-
schema_field["field_type"] == "extra"
126-
and field in config.extra_options
127-
and config.extra_options[field].schema is not None
128-
and "type" in config.extra_options[field].schema
129-
and config.extra_options[field].schema["type"] != "string"
130-
):
131-
type_ = config.extra_options[field].schema["type"]
132-
if not isinstance(value, str):
133-
raise ValueError(
134-
f"Field '{field}': cannot coerce '{value}' (type: {type(value)}) to {type_}"
135-
)
136-
try:
137-
if type_ == "integer":
138-
coerced_value = int(value)
139-
if type_ == "number":
140-
coerced_value = float(value)
141-
except ValueError as exc:
142-
raise ValueError(
143-
f"Field '{field}': cannot coerce '{value}' to {type_}"
144-
) from exc
145-
if type_ == "boolean":
146-
truthy = {"true", "yes", "y", "on", "1"}
147-
falsy = {"false", "no", "n", "off", "0"}
148-
if value.lower() in truthy:
149-
coerced_value = True
150-
elif value.lower() in falsy:
151-
coerced_value = False
152-
else:
124+
if schema_field["field_type"] == "extra" and field in config.extra_options:
125+
option_schema = config.extra_options[field].schema
126+
if (
127+
option_schema is not None
128+
and "type" in option_schema
129+
and option_schema["type"] != "string"
130+
):
131+
type_ = option_schema["type"]
132+
if not isinstance(value, str):
153133
raise ValueError(
154-
f"Field '{field}': cannot coerce '{value}' to boolean"
134+
f"Field '{field}': cannot coerce '{value}' (type: {type(value)}) to {type_}"
155135
)
136+
try:
137+
if type_ == "integer":
138+
coerced_value = int(value)
139+
elif type_ == "number":
140+
coerced_value = float(value)
141+
except ValueError as exc:
142+
raise ValueError(
143+
f"Field '{field}': cannot coerce '{value}' to {type_}"
144+
) from exc
145+
if type_ == "boolean":
146+
truthy = {"true", "yes", "y", "on", "1"}
147+
falsy = {"false", "no", "n", "off", "0"}
148+
if value.lower() in truthy:
149+
coerced_value = True
150+
elif value.lower() in falsy:
151+
coerced_value = False
152+
else:
153+
raise ValueError(
154+
f"Field '{field}': cannot coerce '{value}' to boolean"
155+
)
156156
reduced_need[field] = coerced_value
157157

158158
return reduced_need
@@ -444,7 +444,11 @@ def validate_need(
444444
needs=needs,
445445
all_type_schemas=all_type_schemas,
446446
type_schemas=[cast(SchemaType, linked_schema)],
447-
need_path=[*need_path, link_type, str(target_need_dict["id"])],
447+
need_path=[
448+
*need_path,
449+
link_type,
450+
str(target_need_dict["id"]),
451+
],
448452
schema_path=[
449453
*schema_path_new,
450454
"link_schema",

0 commit comments

Comments
 (0)