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

Commit 6d3ad4c

Browse files
committed
Fix isinstance(...) checks, most are false positives.
1 parent 68c95b2 commit 6d3ad4c

File tree

17 files changed

+42
-33
lines changed

17 files changed

+42
-33
lines changed

synapse/config/_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ def parse_size(value: Union[str, int]) -> int:
186186
TypeError, if given something other than an integer or a string
187187
ValueError: if given a string not of the form described above.
188188
"""
189-
if type(value) is int:
189+
if type(value) is int: # noqa: E721
190190
return value
191-
elif type(value) is str:
191+
elif isinstance(value, str):
192192
sizes = {"K": 1024, "M": 1024 * 1024}
193193
size = 1
194194
suffix = value[-1]
@@ -218,9 +218,9 @@ def parse_duration(value: Union[str, int]) -> int:
218218
TypeError, if given something other than an integer or a string
219219
ValueError: if given a string not of the form described above.
220220
"""
221-
if type(value) is int:
221+
if type(value) is int: # noqa: E721
222222
return value
223-
elif type(value) is str:
223+
elif isinstance(value, str):
224224
second = 1000
225225
minute = 60 * second
226226
hour = 60 * minute

synapse/config/appservice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AppServiceConfig(Config):
3434
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
3535
self.app_service_config_files = config.get("app_service_config_files", [])
3636
if not isinstance(self.app_service_config_files, list) or not all(
37-
type(x) is str for x in self.app_service_config_files
37+
isinstance(x, str) for x in self.app_service_config_files
3838
):
3939
raise ConfigError(
4040
"Expected '%s' to be a list of AS config files:"

synapse/event_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,11 @@ def _check_power_levels(
846846
"kick",
847847
"invite",
848848
}:
849-
if type(v) is not int:
849+
if type(v) is not int: # noqa: E721
850850
raise SynapseError(400, f"{v!r} must be an integer.")
851851
if k in {"events", "notifications", "users"}:
852852
if not isinstance(v, collections.abc.Mapping) or not all(
853-
type(v) is int for v in v.values()
853+
type(v) is int for v in v.values() # noqa: E721
854854
):
855855
raise SynapseError(
856856
400,

synapse/events/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def _copy_power_level_value_as_integer(
702702
:raises TypeError: if `old_value` is neither an integer nor a base-10 string
703703
representation of an integer.
704704
"""
705-
if type(old_value) is int:
705+
if type(old_value) is int: # noqa: E721
706706
power_levels[key] = old_value
707707
return
708708

@@ -730,7 +730,7 @@ def validate_canonicaljson(value: Any) -> None:
730730
* Floats
731731
* NaN, Infinity, -Infinity
732732
"""
733-
if type(value) is int:
733+
if type(value) is int: # noqa: E721
734734
if value < CANONICALJSON_MIN_INT or CANONICALJSON_MAX_INT < value:
735735
raise SynapseError(400, "JSON integer out of range", Codes.BAD_JSON)
736736

synapse/events/validator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ def _validate_retention(self, event: EventBase) -> None:
151151
max_lifetime = event.content.get("max_lifetime")
152152

153153
if min_lifetime is not None:
154-
if type(min_lifetime) is not int:
154+
if type(min_lifetime) is not int: # noqa: E721
155155
raise SynapseError(
156156
code=400,
157157
msg="'min_lifetime' must be an integer",
158158
errcode=Codes.BAD_JSON,
159159
)
160160

161161
if max_lifetime is not None:
162-
if type(max_lifetime) is not int:
162+
if type(max_lifetime) is not int: # noqa: E721
163163
raise SynapseError(
164164
code=400,
165165
msg="'max_lifetime' must be an integer",

synapse/federation/federation_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def event_from_pdu_json(pdu_json: JsonDict, room_version: RoomVersion) -> EventB
280280
_strip_unsigned_values(pdu_json)
281281

282282
depth = pdu_json["depth"]
283-
if type(depth) is not int:
283+
if type(depth) is not int: # noqa: E721
284284
raise SynapseError(400, "Depth %r not an intger" % (depth,), Codes.BAD_JSON)
285285

286286
if depth < 0:

synapse/federation/federation_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ def from_json_dict(cls, d: JsonDict) -> "TimestampToEventResponse":
18911891
)
18921892

18931893
origin_server_ts = d.get("origin_server_ts")
1894-
if type(origin_server_ts) is not int:
1894+
if type(origin_server_ts) is not int: # noqa: E721
18951895
raise ValueError(
18961896
"Invalid response: 'origin_server_ts' must be a int but received %r"
18971897
% origin_server_ts

synapse/handlers/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def maybe_schedule_expiry(self, event: EventBase) -> None:
379379
"""
380380

381381
expiry_ts = event.content.get(EventContentFields.SELF_DESTRUCT_AFTER)
382-
if type(expiry_ts) is not int or event.is_state():
382+
if type(expiry_ts) is not int or event.is_state(): # noqa: E721
383383
return
384384

385385
# _schedule_expiry_for_event won't actually schedule anything if there's already

synapse/http/matrixfederationclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def _validate(v: Any) -> bool:
243243
return (
244244
isinstance(v, list)
245245
and len(v) == 2
246-
and type(v[0]) == int
246+
and type(v[0]) == int # noqa: E721
247247
and isinstance(v[1], dict)
248248
)
249249

synapse/media/oembed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult:
204204
calc_description_and_urls(open_graph_response, oembed["html"])
205205
for size in ("width", "height"):
206206
val = oembed.get(size)
207-
if type(val) is int:
207+
if type(val) is int: # noqa: E721
208208
open_graph_response[f"og:video:{size}"] = val
209209

210210
elif oembed_type == "link":

synapse/media/thumbnailer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self, input_path: str):
7878
image_exif = self.image._getexif() # type: ignore
7979
if image_exif is not None:
8080
image_orientation = image_exif.get(EXIF_ORIENTATION_TAG)
81-
assert type(image_orientation) is int
81+
assert type(image_orientation) is int # noqa: E721
8282
self.transpose_method = EXIF_TRANSPOSE_MAPPINGS.get(image_orientation)
8383
except Exception as e:
8484
# A lot of parsing errors can happen when parsing EXIF

synapse/push/bulk_push_rule_evaluator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ async def _action_for_event_by_user(
379379
keys = list(notification_levels.keys())
380380
for key in keys:
381381
level = notification_levels.get(key, SENTINEL)
382-
if level is not SENTINEL and type(level) is not int:
382+
if level is not SENTINEL and type(level) is not int: # noqa: E721
383383
try:
384384
notification_levels[key] = int(level)
385385
except (TypeError, ValueError):
@@ -472,7 +472,9 @@ async def _action_for_event_by_user(
472472

473473

474474
def _is_simple_value(value: Any) -> bool:
475-
return isinstance(value, (bool, str)) or type(value) is int or value is None
475+
return (
476+
isinstance(value, (bool, str)) or type(value) is int or value is None
477+
) # noqa: E721
476478

477479

478480
def _flatten_dict(

synapse/rest/admin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ async def on_POST(
157157
logger.info("[purge] purging up to token %s (event_id %s)", token, event_id)
158158
elif "purge_up_to_ts" in body:
159159
ts = body["purge_up_to_ts"]
160-
if type(ts) is not int:
160+
if type(ts) is not int: # noqa: E721
161161
raise SynapseError(
162162
HTTPStatus.BAD_REQUEST,
163163
"purge_up_to_ts must be an int",

synapse/rest/admin/registration_tokens.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
143143
else:
144144
# Get length of token to generate (default is 16)
145145
length = body.get("length", 16)
146-
if type(length) is not int:
146+
if type(length) is not int: # noqa: E721
147147
raise SynapseError(
148148
HTTPStatus.BAD_REQUEST,
149149
"length must be an integer",
@@ -163,7 +163,8 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
163163

164164
uses_allowed = body.get("uses_allowed", None)
165165
if not (
166-
uses_allowed is None or (type(uses_allowed) is int and uses_allowed >= 0)
166+
uses_allowed is None
167+
or (type(uses_allowed) is int and uses_allowed >= 0) # noqa: E721
167168
):
168169
raise SynapseError(
169170
HTTPStatus.BAD_REQUEST,
@@ -172,13 +173,15 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
172173
)
173174

174175
expiry_time = body.get("expiry_time", None)
175-
if type(expiry_time) not in (int, type(None)):
176+
if expiry_time is not None and type(expiry_time) is not int: # noqa: E721
176177
raise SynapseError(
177178
HTTPStatus.BAD_REQUEST,
178179
"expiry_time must be an integer or null",
179180
Codes.INVALID_PARAM,
180181
)
181-
if type(expiry_time) is int and expiry_time < self.clock.time_msec():
182+
if (
183+
type(expiry_time) is int and expiry_time < self.clock.time_msec()
184+
): # noqa: E721
182185
raise SynapseError(
183186
HTTPStatus.BAD_REQUEST,
184187
"expiry_time must not be in the past",
@@ -283,7 +286,7 @@ async def on_PUT(self, request: SynapseRequest, token: str) -> Tuple[int, JsonDi
283286
uses_allowed = body["uses_allowed"]
284287
if not (
285288
uses_allowed is None
286-
or (type(uses_allowed) is int and uses_allowed >= 0)
289+
or (type(uses_allowed) is int and uses_allowed >= 0) # noqa: E721
287290
):
288291
raise SynapseError(
289292
HTTPStatus.BAD_REQUEST,
@@ -294,13 +297,15 @@ async def on_PUT(self, request: SynapseRequest, token: str) -> Tuple[int, JsonDi
294297

295298
if "expiry_time" in body:
296299
expiry_time = body["expiry_time"]
297-
if type(expiry_time) not in (int, type(None)):
300+
if expiry_time is not None and type(expiry_time) is not int: # noqa: E721
298301
raise SynapseError(
299302
HTTPStatus.BAD_REQUEST,
300303
"expiry_time must be an integer or null",
301304
Codes.INVALID_PARAM,
302305
)
303-
if type(expiry_time) is int and expiry_time < self.clock.time_msec():
306+
if (
307+
type(expiry_time) is int and expiry_time < self.clock.time_msec()
308+
): # noqa: E721
304309
raise SynapseError(
305310
HTTPStatus.BAD_REQUEST,
306311
"expiry_time must not be in the past",

synapse/rest/admin/users.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,14 +1172,16 @@ async def on_POST(
11721172
messages_per_second = body.get("messages_per_second", 0)
11731173
burst_count = body.get("burst_count", 0)
11741174

1175-
if type(messages_per_second) is not int or messages_per_second < 0:
1175+
if (
1176+
type(messages_per_second) is not int or messages_per_second < 0
1177+
): # noqa: E721
11761178
raise SynapseError(
11771179
HTTPStatus.BAD_REQUEST,
11781180
"%r parameter must be a positive int" % (messages_per_second,),
11791181
errcode=Codes.INVALID_PARAM,
11801182
)
11811183

1182-
if type(burst_count) is not int or burst_count < 0:
1184+
if type(burst_count) is not int or burst_count < 0: # noqa: E721
11831185
raise SynapseError(
11841186
HTTPStatus.BAD_REQUEST,
11851187
"%r parameter must be a positive int" % (burst_count,),

synapse/rest/client/report_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def on_POST(
5555
"Param 'reason' must be a string",
5656
Codes.BAD_JSON,
5757
)
58-
if type(body.get("score", 0)) is not int:
58+
if type(body.get("score", 0)) is not int: # noqa: E721
5959
raise SynapseError(
6060
HTTPStatus.BAD_REQUEST,
6161
"Param 'score' must be an integer",

synapse/storage/databases/main/events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ def _update_metadata_tables_txn(
16711671
if self._ephemeral_messages_enabled:
16721672
# If there's an expiry timestamp on the event, store it.
16731673
expiry_ts = event.content.get(EventContentFields.SELF_DESTRUCT_AFTER)
1674-
if type(expiry_ts) is int and not event.is_state():
1674+
if type(expiry_ts) is int and not event.is_state(): # noqa: E721
16751675
self._insert_event_expiry_txn(txn, event.event_id, expiry_ts)
16761676

16771677
# Insert into the room_memberships table.
@@ -2039,10 +2039,10 @@ def _store_retention_policy_for_room_txn(
20392039
):
20402040
if (
20412041
"min_lifetime" in event.content
2042-
and type(event.content["min_lifetime"]) is not int
2042+
and type(event.content["min_lifetime"]) is not int # noqa: E721
20432043
) or (
20442044
"max_lifetime" in event.content
2045-
and type(event.content["max_lifetime"]) is not int
2045+
and type(event.content["max_lifetime"]) is not int # noqa: E721
20462046
):
20472047
# Ignore the event if one of the value isn't an integer.
20482048
return

0 commit comments

Comments
 (0)