|
24 | 24 | _SEQUENCE_BASE_CLASS_TEMPLATE_DATA_KEY = "sequence_base_class" |
25 | 25 | _SEQUENCE_ITEM_TYPE_TEMPLATE_DATA_KEY = "sequence_item_type" |
26 | 26 | _SEQUENCE_SLICE_TYPE_TEMPLATE_DATA_KEY = "sequence_slice_type" |
| 27 | +_ROOT_MODEL_CONFIG_KEYS: frozenset[str] = frozenset({"regex_engine", "frozen"}) |
| 28 | + |
| 29 | + |
| 30 | +def _root_model_config_items(config: Any) -> list[tuple[str, Any]]: |
| 31 | + return [ |
| 32 | + (field_name, value) |
| 33 | + for field_name, value in _config_dict_items(config) |
| 34 | + if field_name in _ROOT_MODEL_CONFIG_KEYS and value is not None |
| 35 | + ] |
27 | 36 |
|
28 | 37 |
|
29 | 38 | class RootModel(BaseModel): |
@@ -56,28 +65,25 @@ def __init__( |
56 | 65 |
|
57 | 66 | @staticmethod |
58 | 67 | def _has_meaningful_config(config: Any) -> bool: |
| 68 | + has_config = False |
59 | 69 | match config: |
60 | 70 | case None: |
61 | | - return False |
62 | | - case dict(): |
63 | | - return config.get("regex_engine") is not None or config.get("frozen") is not None |
| 71 | + pass |
64 | 72 | case _: |
65 | | - return getattr(config, "regex_engine", None) is not None or getattr(config, "frozen", None) is not None |
| 73 | + has_config = bool(_root_model_config_items(config)) |
| 74 | + return has_config |
66 | 75 |
|
67 | 76 | def _sync_config_items(self) -> None: |
68 | | - if _CONFIG_ITEMS_TEMPLATE_DATA_KEY in self.extra_template_data: |
69 | | - return |
70 | 77 | config = self.extra_template_data.get("config") |
71 | | - if not self._has_meaningful_config(config): |
72 | | - self.extra_template_data.pop("config", None) |
73 | | - return |
74 | | - if config_items := _config_dict_items(config): |
| 78 | + if config_items := _root_model_config_items(config): |
| 79 | + self.extra_template_data["config"] = dict(config_items) |
75 | 80 | self.extra_template_data[_CONFIG_ITEMS_TEMPLATE_DATA_KEY] = config_items |
76 | 81 | if IMPORT_CONFIG_DICT not in self._additional_imports: |
77 | 82 | self._additional_imports.append(IMPORT_CONFIG_DICT) |
78 | 83 | self.clear_imports_cache() |
79 | 84 | return |
80 | 85 | self.extra_template_data.pop("config", None) |
| 86 | + self.extra_template_data.pop(_CONFIG_ITEMS_TEMPLATE_DATA_KEY, None) |
81 | 87 | self._additional_imports = [imp for imp in self._additional_imports if imp != IMPORT_CONFIG_DICT] |
82 | 88 | self.clear_imports_cache() |
83 | 89 |
|
|
0 commit comments