diff --git a/sphinx_needs/data.py b/sphinx_needs/data.py index d2c644322..05bc0bd4d 100644 --- a/sphinx_needs/data.py +++ b/sphinx_needs/data.py @@ -277,7 +277,7 @@ class CoreFieldParameters(TypedDict): }, "section_name": { "description": "Simply the first section.", - "schema": {"type": ["string", "null"], "default": ""}, + "schema": {"type": "string", "default": ""}, }, "signature": { "description": "Derived from a docutils desc_name node.", diff --git a/tests/__snapshots__/test_basic_doc.ambr b/tests/__snapshots__/test_basic_doc.ambr index 88e3fb6ef..ed1d80910 100644 --- a/tests/__snapshots__/test_basic_doc.ambr +++ b/tests/__snapshots__/test_basic_doc.ambr @@ -446,10 +446,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_export_id.ambr b/tests/__snapshots__/test_export_id.ambr index d1940e469..4ad4c7d7b 100644 --- a/tests/__snapshots__/test_export_id.ambr +++ b/tests/__snapshots__/test_export_id.ambr @@ -1042,10 +1042,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_external.ambr b/tests/__snapshots__/test_external.ambr index db8ed5ea0..621f255b9 100644 --- a/tests/__snapshots__/test_external.ambr +++ b/tests/__snapshots__/test_external.ambr @@ -346,10 +346,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ @@ -1123,10 +1120,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_import.ambr b/tests/__snapshots__/test_import.ambr index 4b55c9416..320061336 100644 --- a/tests/__snapshots__/test_import.ambr +++ b/tests/__snapshots__/test_import.ambr @@ -4845,10 +4845,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_need_constraints.ambr b/tests/__snapshots__/test_need_constraints.ambr index a05246902..0d7cbd308 100644 --- a/tests/__snapshots__/test_need_constraints.ambr +++ b/tests/__snapshots__/test_need_constraints.ambr @@ -882,10 +882,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_needextend.ambr b/tests/__snapshots__/test_needextend.ambr index d17d5c612..ff62ef5fc 100644 --- a/tests/__snapshots__/test_needextend.ambr +++ b/tests/__snapshots__/test_needextend.ambr @@ -650,10 +650,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ @@ -1507,10 +1504,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_needs_builder.ambr b/tests/__snapshots__/test_needs_builder.ambr index 52f9a73f5..a474441ba 100644 --- a/tests/__snapshots__/test_needs_builder.ambr +++ b/tests/__snapshots__/test_needs_builder.ambr @@ -514,10 +514,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ @@ -1174,10 +1171,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ @@ -1979,10 +1973,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/__snapshots__/test_service_github.ambr b/tests/__snapshots__/test_service_github.ambr index 7e4b113bd..e169c7ff1 100644 --- a/tests/__snapshots__/test_service_github.ambr +++ b/tests/__snapshots__/test_service_github.ambr @@ -628,10 +628,7 @@ 'default': '', 'description': 'Simply the first section.', 'field_type': 'core', - 'type': list([ - 'string', - 'null', - ]), + 'type': 'string', }), 'sections': dict({ 'default': list([ diff --git a/tests/test_data.py b/tests/test_data.py index e7f38c0df..9cc6f62b4 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -1,5 +1,6 @@ import ast import inspect +from typing import ForwardRef from sphinx_needs.data import NeedsCoreFields, NeedsInfoType @@ -10,8 +11,42 @@ def test_consistent(): but I'm not sure this is possible (to encode both the static and dynamic data required). So at least here, we check that they are consistent with each other. """ + # check fields are consistent assert set(NeedsCoreFields).issubset(set(NeedsInfoType.__annotations__)) + # check field types are consistent with schema + for field, data in NeedsCoreFields.items(): + if not (schema := data.get("schema")): + continue + type_ = NeedsInfoType.__annotations__[field] + assert isinstance(type_, ForwardRef) + type_str = type_.__forward_arg__ + if type_str.startswith("Required"): + type_str = type_str[9:-1] + if type_str == "str" or type_str == "str | Text": + assert schema["type"] == "string", field + elif type_str == "int": + assert schema["type"] == "integer", field + elif type_str == "bool": + assert schema["type"] == "boolean", field + elif type_str in ("str | None", "None | str"): + assert schema["type"] == ["string", "null"], field + elif type_str in ("int | None", "None | int"): + assert schema["type"] == ["integer", "null"], field + elif type_str in ("bool | None", "None | bool"): + assert schema["type"] == ["boolean", "null"], field + elif type_str == "list[str]": + assert schema["type"] == "array", field + assert schema["items"]["type"] == "string", field + elif type_str == "dict[str, str]": + assert schema["type"] == "object", field + assert schema["additionalProperties"]["type"] == "string", field + elif type_str.startswith("dict["): + assert schema["type"] == "object", field + else: + raise ValueError(f"Unknown type: {type_str!r} for {field!r}") + + # check descriptions are consistent source = inspect.getsource(NeedsInfoType) klass = ast.parse(source).body[0] descriptions = {}