Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 74e4419

Browse files
author
David Robertson
authored
Fix another jsonschema typecheck error (#11830)
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 validator 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. (Strictly 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.)
1 parent b8bf600 commit 74e4419

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

changelog.d/11817.misc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Compatibility with updated type hints for jsonschema 4.4.0.
1+
Correct a type annotation in the event validation logic.

changelog.d/11830.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Correct a type annotation in the event validation logic.

synapse/events/validator.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import collections.abc
15-
from typing import Iterable, Union
15+
from typing import Iterable, Type, Union
1616

1717
import jsonschema
1818

@@ -246,9 +246,7 @@ def _ensure_state_event(self, event: Union[EventBase, EventBuilder]) -> None:
246246

247247
# This could return something newer than Draft 7, but that's the current "latest"
248248
# validator.
249-
#
250-
# See https://github.com/python/typeshed/issues/7028 for the ignored return type.
251-
def _create_power_level_validator() -> jsonschema.Draft7Validator: # type: ignore[valid-type]
249+
def _create_power_level_validator() -> Type[jsonschema.Draft7Validator]:
252250
validator = jsonschema.validators.validator_for(POWER_LEVELS_SCHEMA)
253251

254252
# by default jsonschema does not consider a frozendict to be an object so

0 commit comments

Comments
 (0)