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

Commit

Permalink
Remove unused # type: ignores (#12531)
Browse files Browse the repository at this point in the history
Over time we've begun to use newer versions of mypy, typeshed, stub
packages---and of course we've improved our own annotations. This makes
some type ignore comments no longer necessary. I have removed them.

There was one exception: a module that imports `select.epoll`. The
ignore is redundant on Linux, but I've kept it ignored for those of us
who work on the source tree using not-Linux. (#11771)

I'm more interested in the config line which enforces this. I want
unused ignores to be reported, because I think it's useful feedback when
annotating to know when you've fixed a problem you had to previously
ignore.

* Installing extras before typechecking

Lacking an easy way to install all extras generically, let's bite the bullet and
make install the hand-maintained `all` extra before typechecking.

Now that matrix-org/backend-meta#6 is merged to
the release/v1 branch.
  • Loading branch information
David Robertson committed Apr 27, 2022
1 parent 8a23bde commit 6463244
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 57 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ jobs:
- run: scripts-dev/config-lint.sh

lint:
# This does a vanilla `poetry install` - no extras. I'm slightly anxious
# that we might skip some typechecks on code that uses extras. However,
# I think the right way to fix this is to mark any extras needed for
# typechecking as development dependencies. To detect this, we ought to
# turn up mypy's strictness: disallow unknown imports and be accept fewer
# uses of `Any`.
uses: "matrix-org/backend-meta/.github/workflows/python-poetry-ci.yml@v1"
with:
typechecking-extras: "all"

lint-crlf:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions changelog.d/12531.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove unused `# type: ignore`s.
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ show_error_codes = True
show_traceback = True
mypy_path = stubs
warn_unreachable = True
warn_unused_ignores = True
local_partial_types = True
no_implicit_optional = True

Expand Down Expand Up @@ -134,6 +135,11 @@ disallow_untyped_defs = True
[mypy-synapse.metrics.*]
disallow_untyped_defs = True

[mypy-synapse.metrics._reactor_metrics]
# This module imports select.epoll. That exists on Linux, but doesn't on macOS.
# See https://github.com/matrix-org/synapse/pull/11771.
warn_unused_ignores = False

[mypy-synapse.module_api.*]
disallow_untyped_defs = True

Expand Down
4 changes: 1 addition & 3 deletions stubs/sortedcontainers/sorteddict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]):
def __getitem__(self, index: slice) -> List[_KT_co]: ...
def __delitem__(self, index: Union[int, slice]) -> None: ...

class SortedItemsView( # type: ignore
ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]]
):
class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]]):
def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
@overload
def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ...
Expand Down
4 changes: 2 additions & 2 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
from twisted.protocols.tls import TLSMemoryBIOFactory
from twisted.python.threadpool import ThreadPool

import synapse
from synapse.api.constants import MAX_PDU_SIZE
from synapse.app import check_bind_error
from synapse.app.phone_stats_home import start_phone_stats_home
Expand All @@ -60,6 +59,7 @@
from synapse.events.third_party_rules import load_legacy_third_party_event_rules
from synapse.handlers.auth import load_legacy_password_auth_providers
from synapse.logging.context import PreserveLoggingContext
from synapse.logging.opentracing import init_tracer
from synapse.metrics import install_gc_manager, register_threadpool
from synapse.metrics.background_process_metrics import wrap_as_background_process
from synapse.metrics.jemalloc import setup_jemalloc_stats
Expand Down Expand Up @@ -431,7 +431,7 @@ def run_sighup(*args: Any, **kwargs: Any) -> None:
refresh_certificate(hs)

# Start the tracer
synapse.logging.opentracing.init_tracer(hs) # type: ignore[attr-defined] # noqa
init_tracer(hs) # noqa

# Instantiate the modules so they can register their web resources to the module API
# before we start the listeners.
Expand Down
6 changes: 2 additions & 4 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def generate_ip_set(
class HttpResourceConfig:
names: List[str] = attr.ib(
factory=list,
validator=attr.validators.deep_iterable(attr.validators.in_(KNOWN_RESOURCES)), # type: ignore
validator=attr.validators.deep_iterable(attr.validators.in_(KNOWN_RESOURCES)),
)
compress: bool = attr.ib(
default=False,
Expand Down Expand Up @@ -231,9 +231,7 @@ class ManholeConfig:
class LimitRemoteRoomsConfig:
enabled: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False)
complexity: Union[float, int] = attr.ib(
validator=attr.validators.instance_of(
(float, int) # type: ignore[arg-type] # noqa
),
validator=attr.validators.instance_of((float, int)), # noqa
default=1.0,
)
complexity_error: str = attr.ib(
Expand Down
4 changes: 2 additions & 2 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ async def on_incoming_transaction(
transaction_id=transaction_id,
destination=destination,
origin=origin,
origin_server_ts=transaction_data.get("origin_server_ts"), # type: ignore
pdus=transaction_data.get("pdus"), # type: ignore
origin_server_ts=transaction_data.get("origin_server_ts"), # type: ignore[arg-type]
pdus=transaction_data.get("pdus"),
edus=transaction_data.get("edus"),
)

Expand Down
10 changes: 5 additions & 5 deletions synapse/federation/transport/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,21 @@ async def send_transaction(
"""
logger.debug(
"send_data dest=%s, txid=%s",
transaction.destination, # type: ignore
transaction.transaction_id, # type: ignore
transaction.destination,
transaction.transaction_id,
)

if transaction.destination == self.server_name: # type: ignore
if transaction.destination == self.server_name:
raise RuntimeError("Transport layer cannot send to itself!")

# FIXME: This is only used by the tests. The actual json sent is
# generated by the json_data_callback.
json_data = transaction.get_dict()

path = _create_v1_path("/send/%s", transaction.transaction_id) # type: ignore
path = _create_v1_path("/send/%s", transaction.transaction_id)

return await self.client.put_json(
transaction.destination, # type: ignore
transaction.destination,
path=path,
data=json_data,
json_data_callback=json_data_callback,
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ async def check_ui_auth(
sid = authdict["session"]

# Convert the URI and method to strings.
uri = request.uri.decode("utf-8") # type: ignore
uri = request.uri.decode("utf-8")
method = request.method.decode("utf-8")

# If there's no session ID, create a new session.
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ async def oidc_response_to_user_attributes(failures: int) -> UserAttributes:
"Mapping provider does not support de-duplicating Matrix IDs"
)

attributes = await self._user_mapping_provider.map_user_attributes( # type: ignore
attributes = await self._user_mapping_provider.map_user_attributes(
userinfo, token
)

Expand Down
6 changes: 3 additions & 3 deletions synapse/handlers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ async def _search(
itertools.chain(
# The events_before and events_after for each context.
itertools.chain.from_iterable(
itertools.chain(context["events_before"], context["events_after"]) # type: ignore[arg-type]
itertools.chain(context["events_before"], context["events_after"])
for context in contexts.values()
),
# The returned events.
Expand All @@ -373,10 +373,10 @@ async def _search(

for context in contexts.values():
context["events_before"] = self._event_serializer.serialize_events(
context["events_before"], time_now, bundle_aggregations=aggregations # type: ignore[arg-type]
context["events_before"], time_now, bundle_aggregations=aggregations
)
context["events_after"] = self._event_serializer.serialize_events(
context["events_after"], time_now, bundle_aggregations=aggregations # type: ignore[arg-type]
context["events_after"], time_now, bundle_aggregations=aggregations
)

results = [
Expand Down
4 changes: 2 additions & 2 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ async def _async_render(self, request: SynapseRequest) -> Optional[Tuple[int, An
if isawaitable(raw_callback_return):
callback_return = await raw_callback_return
else:
callback_return = raw_callback_return # type: ignore
callback_return = raw_callback_return

return callback_return

Expand Down Expand Up @@ -469,7 +469,7 @@ async def _async_render(self, request: SynapseRequest) -> Tuple[int, Any]:
if isinstance(raw_callback_return, (defer.Deferred, types.CoroutineType)):
callback_return = await raw_callback_return
else:
callback_return = raw_callback_return # type: ignore
callback_return = raw_callback_return

return callback_return

Expand Down
4 changes: 3 additions & 1 deletion synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
from synapse.types import (
DomainSpecificString,
JsonDict,
JsonMapping,
Requester,
StateMap,
UserID,
Expand Down Expand Up @@ -151,6 +152,7 @@
"PRESENCE_ALL_USERS",
"LoginResponse",
"JsonDict",
"JsonMapping",
"EventBase",
"StateMap",
"ProfileInfo",
Expand Down Expand Up @@ -1419,7 +1421,7 @@ def _validate_user_id(self, user_id: str) -> None:
f"{user_id} is not local to this homeserver; can't access account data for remote users."
)

async def get_global(self, user_id: str, data_type: str) -> Optional[JsonDict]:
async def get_global(self, user_id: str, data_type: str) -> Optional[JsonMapping]:
"""
Gets some global account data, of a specified type, for the specified user.
Expand Down
6 changes: 3 additions & 3 deletions synapse/storage/databases/main/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ def _reap_users(txn: LoggingTransaction, reserved_users: List[str]) -> None:
# is racy.
# Have resolved to invalidate the whole cache for now and do
# something about it if and when the perf becomes significant
self._invalidate_all_cache_and_stream( # type: ignore[attr-defined]
self._invalidate_all_cache_and_stream(
txn, self.user_last_seen_monthly_active
)
self._invalidate_cache_and_stream(txn, self.get_monthly_active_count, ()) # type: ignore[attr-defined]
self._invalidate_cache_and_stream(txn, self.get_monthly_active_count, ())

reserved_users = await self.get_registered_reserved_users()
await self.db_pool.runInteraction(
Expand Down Expand Up @@ -363,7 +363,7 @@ async def populate_monthly_active_users(self, user_id: str) -> None:

if self._limit_usage_by_mau or self._mau_stats_only:
# Trial users and guests should not be included as part of MAU group
is_guest = await self.is_guest(user_id) # type: ignore[attr-defined]
is_guest = await self.is_guest(user_id)
if is_guest:
return
is_trial = await self.is_trial_user(user_id)
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/prepare_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,11 @@ def _upgrade_existing_database(

if hasattr(module, "run_create"):
logger.info("Running %s:run_create", relative_path)
module.run_create(cur, database_engine) # type: ignore
module.run_create(cur, database_engine)

if not is_empty and hasattr(module, "run_upgrade"):
logger.info("Running %s:run_upgrade", relative_path)
module.run_upgrade(cur, database_engine, config=config) # type: ignore
module.run_upgrade(cur, database_engine, config=config)
elif ext == ".pyc" or file_name == "__pycache__":
# Sometimes .pyc files turn up anyway even though we've
# disabled their generation; e.g. from distribution package
Expand Down
2 changes: 1 addition & 1 deletion synapse/util/caches/ttlcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_with_expiry(self, key: KT) -> Tuple[VT, float, float]:
self._metrics.inc_hits()
return e.value, e.expiry_time, e.ttl

def pop(self, key: KT, default: T = SENTINEL) -> Union[VT, T]: # type: ignore
def pop(self, key: KT, default: T = SENTINEL) -> Union[VT, T]:
"""Remove a value from the cache
If key is in the cache, remove it and return its value, else return default.
Expand Down
9 changes: 3 additions & 6 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,23 @@ def test_mau_limits_when_disabled(self):

@override_config({"limit_usage_by_mau": True})
def test_get_or_create_user_mau_not_blocked(self):
# Type ignore: mypy doesn't like us assigning to methods.
self.store.count_monthly_users = Mock( # type: ignore[assignment]
self.store.count_monthly_users = Mock(
return_value=make_awaitable(self.hs.config.server.max_mau_value - 1)
)
# Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "c", "User"))

@override_config({"limit_usage_by_mau": True})
def test_get_or_create_user_mau_blocked(self):
# Type ignore: mypy doesn't like us assigning to methods.
self.store.get_monthly_active_count = Mock( # type: ignore[assignment]
self.store.get_monthly_active_count = Mock(
return_value=make_awaitable(self.lots_of_users)
)
self.get_failure(
self.get_or_create_user(self.requester, "b", "display_name"),
ResourceLimitError,
)

# Type ignore: mypy doesn't like us assigning to methods.
self.store.get_monthly_active_count = Mock( # type: ignore[assignment]
self.store.get_monthly_active_count = Mock(
return_value=make_awaitable(self.hs.config.server.max_mau_value)
)
self.get_failure(
Expand Down
17 changes: 11 additions & 6 deletions tests/module_api/test_account_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from twisted.test.proto_helpers import MemoryReactor

from synapse.api.errors import SynapseError
from synapse.rest import admin
from synapse.server import HomeServer
from synapse.util import Clock

from tests.unittest import HomeserverTestCase

Expand All @@ -22,7 +26,9 @@ class ModuleApiTestCase(HomeserverTestCase):
admin.register_servlets,
]

def prepare(self, reactor, clock, homeserver) -> None:
def prepare(
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
) -> None:
self._store = homeserver.get_datastores().main
self._module_api = homeserver.get_module_api()
self._account_data_mgr = self._module_api.account_data_manager
Expand Down Expand Up @@ -91,7 +97,7 @@ def test_get_global_no_mutability(self) -> None:
)
with self.assertRaises(TypeError):
# This throws an exception because it's a frozen dict.
the_data["wombat"] = False
the_data["wombat"] = False # type: ignore[index]

def test_put_global(self) -> None:
"""
Expand Down Expand Up @@ -143,15 +149,14 @@ def test_put_global_validation(self) -> None:
with self.assertRaises(TypeError):
# The account data type must be a string.
self.get_success_or_raise(
self._module_api.account_data_manager.put_global(
self.user_id, 42, {} # type: ignore
)
self._module_api.account_data_manager.put_global(self.user_id, 42, {}) # type: ignore[arg-type]
)

with self.assertRaises(TypeError):
# The account data dict must be a dict.
# noinspection PyTypeChecker
self.get_success_or_raise(
self._module_api.account_data_manager.put_global(
self.user_id, "test.data", 42 # type: ignore
self.user_id, "test.data", 42 # type: ignore[arg-type]
)
)
8 changes: 4 additions & 4 deletions tests/replication/test_federation_sender_shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def test_send_event_sharded(self):
for i in range(20):
server_name = "other_server_%d" % (i,)
room = self.create_room_with_remote_server(user, token, server_name)
mock_client1.reset_mock() # type: ignore[attr-defined]
mock_client2.reset_mock() # type: ignore[attr-defined]
mock_client1.reset_mock()
mock_client2.reset_mock()

self.create_and_send_event(room, UserID.from_string(user))
self.replicate()
Expand Down Expand Up @@ -167,8 +167,8 @@ def test_send_typing_sharded(self):
for i in range(20):
server_name = "other_server_%d" % (i,)
room = self.create_room_with_remote_server(user, token, server_name)
mock_client1.reset_mock() # type: ignore[attr-defined]
mock_client2.reset_mock() # type: ignore[attr-defined]
mock_client1.reset_mock()
mock_client2.reset_mock()

self.get_success(
typing_handler.started_typing(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def make_awaitable(result: TV) -> Awaitable[TV]:
This uses Futures as they can be awaited multiple times so can be returned
to multiple callers.
"""
future = Future() # type: ignore
future: Future[TV] = Future()
future.set_result(result)
return future

Expand All @@ -69,7 +69,7 @@ def setup_awaitable_errors() -> Callable[[], None]:

# State shared between unraisablehook and check_for_unraisable_exceptions.
unraisable_exceptions = []
orig_unraisablehook = sys.unraisablehook # type: ignore
orig_unraisablehook = sys.unraisablehook

def unraisablehook(unraisable):
unraisable_exceptions.append(unraisable.exc_value)
Expand All @@ -78,11 +78,11 @@ def cleanup():
"""
A method to be used as a clean-up that fails a test-case if there are any new unraisable exceptions.
"""
sys.unraisablehook = orig_unraisablehook # type: ignore
sys.unraisablehook = orig_unraisablehook
if unraisable_exceptions:
raise unraisable_exceptions.pop()

sys.unraisablehook = unraisablehook # type: ignore
sys.unraisablehook = unraisablehook

return cleanup

Expand Down
Loading

0 comments on commit 6463244

Please sign in to comment.