diff options
author | Jason Little <realtyem@gmail.com> | 2023-04-17 18:53:43 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-17 23:53:43 +0000 |
commit | e12d788bb7526b7d9ab09bb3921b3cd17f20a939 (patch) | |
tree | a6e9634c51c708c68c97be37a2f36647e21aee4e /synapse/config/_util.py | |
parent | Bump mypy from 1.0.0 to 1.0.1 (#15447) (diff) | |
download | synapse-e12d788bb7526b7d9ab09bb3921b3cd17f20a939.tar.xz |
Switch `InstanceLocationConfig` to a pydantic `BaseModel` (#15431)
* Switch InstanceLocationConfig to a pydantic BaseModel, apply Strict* types and add a few helper methods(that will make more sense in follow up work). Co-authored-by: David Robertson <davidr@element.io>
Diffstat (limited to 'synapse/config/_util.py')
-rw-r--r-- | synapse/config/_util.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/synapse/config/_util.py b/synapse/config/_util.py index d3a4b484ab..dfc5d12210 100644 --- a/synapse/config/_util.py +++ b/synapse/config/_util.py @@ -11,9 +11,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Iterable +from typing import Any, Dict, Iterable, Type, TypeVar import jsonschema +from pydantic import BaseModel, ValidationError, parse_obj_as from synapse.config._base import ConfigError from synapse.types import JsonDict @@ -64,3 +65,28 @@ def json_error_to_config_error( else: path.append(str(p)) return ConfigError(e.message, path) + + +Model = TypeVar("Model", bound=BaseModel) + + +def parse_and_validate_mapping( + config: Any, + model_type: Type[Model], +) -> Dict[str, Model]: + """Parse `config` as a mapping from strings to a given `Model` type. + Args: + config: The configuration data to check + model_type: The BaseModel to validate and parse against. + Returns: + Fully validated and parsed Dict[str, Model]. + Raises: + ConfigError, if given improper input. + """ + try: + # type-ignore: mypy doesn't like constructing `Dict[str, model_type]` because + # `model_type` is a runtime variable. Pydantic is fine with this. + instances = parse_obj_as(Dict[str, model_type], config) # type: ignore[valid-type] + except ValidationError as e: + raise ConfigError(str(e)) from e + return instances |