From b4409d51c52d6805a52e222288406d428cb045b8 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 12 Dec 2022 19:15:48 +0000 Subject: [PATCH 1/5] Enable `--warn-redundant-casts` option in mypy Doesn't do much but helps me sleep better at night. --- mypy.ini | 1 + synapse/storage/database.py | 3 ++- synapse/storage/engines/postgres.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy.ini b/mypy.ini index a4a1e4511a07..727536df50fd 100644 --- a/mypy.ini +++ b/mypy.ini @@ -12,6 +12,7 @@ local_partial_types = True no_implicit_optional = True disallow_untyped_defs = True strict_equality = True +warn_redundant_casts = True files = docker/, diff --git a/synapse/storage/database.py b/synapse/storage/database.py index 55bcb90001e9..f522e290b0e8 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -667,7 +667,8 @@ def new_transaction( ) # also check variables referenced in func's closure if inspect.isfunction(func): - f = cast(types.FunctionType, func) + # Keep the cast for now---it helps PyCharm to understand what `func` is. + f = cast(types.FunctionType, func) # type: ignore[warn-redundant-casts] if f.__closure__: for i, cell in enumerate(f.__closure__): if inspect.isgenerator(cell.cell_contents): diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py index 719a51733621..f9f562ea4544 100644 --- a/synapse/storage/engines/postgres.py +++ b/synapse/storage/engines/postgres.py @@ -77,7 +77,7 @@ def check_database( # docs: The number is formed by converting the major, minor, and # revision numbers into two-decimal-digit numbers and appending them # together. For example, version 8.1.5 will be returned as 80105 - self._version = cast(int, db_conn.server_version) + self._version = db_conn.server_version allow_unsafe_locale = self.config.get("allow_unsafe_locale", False) # Are we on a supported PostgreSQL version? From 1e40c3e61c9aad7e3cd53db9e01a9c7b15138dc5 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 12 Dec 2022 19:17:23 +0000 Subject: [PATCH 2/5] Changelog --- changelog.d/14671.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14671.misc diff --git a/changelog.d/14671.misc b/changelog.d/14671.misc new file mode 100644 index 000000000000..93ceaeafc9b9 --- /dev/null +++ b/changelog.d/14671.misc @@ -0,0 +1 @@ +Improve type hints. From be29fea6becbbdfde667b7f59498fcef9234b844 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 12 Dec 2022 20:03:45 +0000 Subject: [PATCH 3/5] Fix name of the ignore --- synapse/storage/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/database.py b/synapse/storage/database.py index f522e290b0e8..0b29e67b9447 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -668,7 +668,7 @@ def new_transaction( # also check variables referenced in func's closure if inspect.isfunction(func): # Keep the cast for now---it helps PyCharm to understand what `func` is. - f = cast(types.FunctionType, func) # type: ignore[warn-redundant-casts] + f = cast(types.FunctionType, func) # type: ignore[redundant-cast] if f.__closure__: for i, cell in enumerate(f.__closure__): if inspect.isgenerator(cell.cell_contents): From fe4e2055eb5097f530e53148ed93e49462975d7e Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 12 Dec 2022 20:03:55 +0000 Subject: [PATCH 4/5] Fix one more missed cast Not sure why I didn't see this one locally, maybe I needed a poetry update --- scripts-dev/release.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts-dev/release.py b/scripts-dev/release.py index bf47b6c713ff..cd9c1ba24666 100755 --- a/scripts-dev/release.py +++ b/scripts-dev/release.py @@ -27,7 +27,7 @@ import urllib.request from os import path from tempfile import TemporaryDirectory -from typing import Any, List, Optional, cast +from typing import Any, List, Optional import attr import click @@ -176,7 +176,7 @@ def _prepare() -> None: # Switch to the release branch. # Cast safety: parse() won't return a version.LegacyVersion from our # version string format. - parsed_new_version = cast(version.Version, version.parse(new_version)) + parsed_new_version = version.parse(new_version) # We assume for debian changelogs that we only do RCs or full releases. assert not parsed_new_version.is_devrelease From e668d3adb090deb88277ba53f896acd3733c2df7 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Mon, 12 Dec 2022 20:44:49 +0000 Subject: [PATCH 5/5] Remove old comment Co-authored-by: Patrick Cloke --- scripts-dev/release.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts-dev/release.py b/scripts-dev/release.py index cd9c1ba24666..6974fd789575 100755 --- a/scripts-dev/release.py +++ b/scripts-dev/release.py @@ -174,8 +174,6 @@ def _prepare() -> None: click.get_current_context().abort() # Switch to the release branch. - # Cast safety: parse() won't return a version.LegacyVersion from our - # version string format. parsed_new_version = version.parse(new_version) # We assume for debian changelogs that we only do RCs or full releases.