From 4c9de6f6d56f1f1c295e58537993d71921930186 Mon Sep 17 00:00:00 2001 From: "Olivier Wilkinson (reivilibre)" Date: Wed, 5 Jul 2023 12:04:39 +0100 Subject: [PATCH] Revert to `Type[Model]` to keep type variable constrained; add type ignores to work around mypy deficiency --- synapse/config/_util.py | 6 +++--- synapse/config/workers.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/synapse/config/_util.py b/synapse/config/_util.py index 6c97cd2772dc..0de58c00cc26 100644 --- a/synapse/config/_util.py +++ b/synapse/config/_util.py @@ -11,7 +11,7 @@ # 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, Dict, TypeVar +from typing import Any, Dict, TypeVar, Type import jsonschema from pydantic import BaseModel, ValidationError, parse_obj_as @@ -75,7 +75,7 @@ def json_error_to_config_error( # for what I suspect is the wrong reason. def parse_and_validate_mapping( config: Any, - model_type: TypeAlias, + model_type: Type[Model], ) -> Dict[str, Model]: """Parse `config` as a mapping from strings to a given `Model` type. Args: @@ -89,7 +89,7 @@ def parse_and_validate_mapping( 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) + instances = parse_obj_as(Dict[str, model_type], config) # type: ignore except ValidationError as e: raise ConfigError(str(e)) from e return instances diff --git a/synapse/config/workers.py b/synapse/config/workers.py index bc4d3a9f619d..75181aae450d 100644 --- a/synapse/config/workers.py +++ b/synapse/config/workers.py @@ -279,9 +279,10 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: % MAIN_PROCESS_INSTANCE_MAP_NAME ) + # type-ignore: the expression `Union[A, B]` is not a Type[Union[A, B]] currently self.instance_map: Dict[ str, InstanceLocationConfig - ] = parse_and_validate_mapping(instance_map, InstanceLocationConfig) + ] = parse_and_validate_mapping(instance_map, InstanceLocationConfig) # type: ignore # Map from type of streams to source, c.f. WriterLocations. writers = config.get("stream_writers") or {}