Consistently use collections.abc.Mapping to check frozendict. (#12564)

This commit is contained in:
Patrick Cloke 2022-04-27 09:00:07 -04:00 committed by GitHub
parent e8d1ec0e92
commit 8a23bde823
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 9 deletions

1
changelog.d/12564.misc Normal file
View file

@ -0,0 +1 @@
Consistently check if an object is a `frozendict`.

View file

@ -27,7 +27,6 @@ from typing import (
) )
import attr import attr
from frozendict import frozendict
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
from synapse.api.errors import Codes, SynapseError from synapse.api.errors import Codes, SynapseError
@ -204,7 +203,9 @@ def _copy_field(src: JsonDict, dst: JsonDict, field: List[str]) -> None:
key_to_move = field.pop(-1) key_to_move = field.pop(-1)
sub_dict = src sub_dict = src
for sub_field in field: # e.g. sub_field => "content" for sub_field in field: # e.g. sub_field => "content"
if sub_field in sub_dict and type(sub_dict[sub_field]) in [dict, frozendict]: if sub_field in sub_dict and isinstance(
sub_dict[sub_field], collections.abc.Mapping
):
sub_dict = sub_dict[sub_field] sub_dict = sub_dict[sub_field]
else: else:
return return
@ -622,7 +623,7 @@ def validate_canonicaljson(value: Any) -> None:
# Note that Infinity, -Infinity, and NaN are also considered floats. # Note that Infinity, -Infinity, and NaN are also considered floats.
raise SynapseError(400, "Bad JSON value: float", Codes.BAD_JSON) raise SynapseError(400, "Bad JSON value: float", Codes.BAD_JSON)
elif isinstance(value, (dict, frozendict)): elif isinstance(value, collections.abc.Mapping):
for v in value.values(): for v in value.values():
validate_canonicaljson(v) validate_canonicaljson(v)

View file

@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections.abc
import logging import logging
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
@ -24,7 +25,6 @@ from typing import (
) )
import attr import attr
from frozendict import frozendict
from synapse.api.constants import RelationTypes from synapse.api.constants import RelationTypes
from synapse.api.errors import SynapseError from synapse.api.errors import SynapseError
@ -380,7 +380,7 @@ class RelationsHandler:
# Do not bundle aggregations for an event which represents an edit or an # Do not bundle aggregations for an event which represents an edit or an
# annotation. It does not make sense for them to have related events. # annotation. It does not make sense for them to have related events.
relates_to = event.content.get("m.relates_to") relates_to = event.content.get("m.relates_to")
if isinstance(relates_to, (dict, frozendict)): if isinstance(relates_to, collections.abc.Mapping):
relation_type = relates_to.get("rel_type") relation_type = relates_to.get("rel_type")
if relation_type in (RelationTypes.ANNOTATION, RelationTypes.REPLACE): if relation_type in (RelationTypes.ANNOTATION, RelationTypes.REPLACE):
continue continue

View file

@ -12,11 +12,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections.abc
import logging import logging
from typing import TYPE_CHECKING, Collection, Dict, Iterable, Optional, Set, Tuple from typing import TYPE_CHECKING, Collection, Dict, Iterable, Optional, Set, Tuple
from frozendict import frozendict
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.api.errors import NotFoundError, UnsupportedRoomVersionError from synapse.api.errors import NotFoundError, UnsupportedRoomVersionError
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
@ -160,7 +159,7 @@ class StateGroupWorkerStore(EventsWorkerStore, SQLBaseStore):
predecessor = create_event.content.get("predecessor", None) predecessor = create_event.content.get("predecessor", None)
# Ensure the key is a dictionary # Ensure the key is a dictionary
if not isinstance(predecessor, (dict, frozendict)): if not isinstance(predecessor, collections.abc.Mapping):
return None return None
# The keys must be strings since the data is JSON. # The keys must be strings since the data is JSON.

View file

@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections.abc
from typing import Any from typing import Any
from frozendict import frozendict from frozendict import frozendict
@ -35,7 +36,7 @@ def freeze(o: Any) -> Any:
def unfreeze(o: Any) -> Any: def unfreeze(o: Any) -> Any:
if isinstance(o, (dict, frozendict)): if isinstance(o, collections.abc.Mapping):
return {k: unfreeze(v) for k, v in o.items()} return {k: unfreeze(v) for k, v in o.items()}
if isinstance(o, (bytes, str)): if isinstance(o, (bytes, str)):