From be10a0e3765fac3638ea0a52952396afce002398 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 25 Jan 2022 19:33:41 +0000 Subject: [PATCH 1/3] Fix another jsonschema typecheck error Similar to #11817. In `_create_power_level_validator` we - retrieve `validator`. This is a class implementing the `jsonschema.protocols.Validator` interface. In other words, `validator: Type[jsonschema.protocols.Validator]`. - we then create an second validtor class by modifying the original `validator`. We return that class, which is also of type `Type[jsonschema.protocols.Validator]`. So the original annotation was incorrect: it claimed we were returning an instance of jsonSchema.Draft7Validator, not the class (or a subclass) itself. (Stricly speaking this is incorrect, because `POWER_LEVELS_SCHEMA` isn't pinned to a particular version of JSON Schema. But there are other complications with the type stubs if you try to fix this; I felt like the change herein was a decent compromise that better expresses intent). (I suspect/hope the typeshed project would welcome an effort to improve the jsonschema stubs. Let's see if I get some spare time.) --- synapse/events/validator.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/synapse/events/validator.py b/synapse/events/validator.py index 424557301708..360d24274a52 100644 --- a/synapse/events/validator.py +++ b/synapse/events/validator.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import collections.abc -from typing import Iterable, Union +from typing import Iterable, Type, Union import jsonschema @@ -246,9 +246,7 @@ def _ensure_state_event(self, event: Union[EventBase, EventBuilder]) -> None: # This could return something newer than Draft 7, but that's the current "latest" # validator. -# -# See https://github.com/python/typeshed/issues/7028 for the ignored return type. -def _create_power_level_validator() -> jsonschema.Draft7Validator: # type: ignore[valid-type] +def _create_power_level_validator() -> Type[jsonschema.Draft7Validator]: validator = jsonschema.validators.validator_for(POWER_LEVELS_SCHEMA) # by default jsonschema does not consider a frozendict to be an object so From b41893c466c11d52afe8a6b06d04a540e723239a Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 25 Jan 2022 19:47:58 +0000 Subject: [PATCH 2/3] Changelog --- changelog.d/11830.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11830.misc diff --git a/changelog.d/11830.misc b/changelog.d/11830.misc new file mode 100644 index 000000000000..fe248d00ab94 --- /dev/null +++ b/changelog.d/11830.misc @@ -0,0 +1 @@ +Correct a type annotation in the event validation logic. \ No newline at end of file From acf1e4a1e5a26c6ed77de618005cd6231668b214 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 25 Jan 2022 19:55:22 +0000 Subject: [PATCH 3/3] Replace previous changelog with this one --- changelog.d/11817.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/11817.misc b/changelog.d/11817.misc index bd29d8d6ebf3..3d6b2ea4d485 100644 --- a/changelog.d/11817.misc +++ b/changelog.d/11817.misc @@ -1 +1 @@ -Compatibility with updated type hints for jsonschema 4.4.0. +Correct a type annotation in the event validation logic.