Skip to content

Commit 9915345

Browse files
author
David Robertson
authored
Fix typechecking (#53)
Fixes #52. See PR's commit messages for more details. Drive-by fixes to remove twisted-related config, missed in #39.
1 parent 5a2d4af commit 9915345

File tree

9 files changed

+19
-28
lines changed

9 files changed

+19
-28
lines changed

mypy.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
[mypy]
2-
strict = true
3-
plugins = mypy_zope:plugin
4-
# Some calls to Twisted APIs are untyped
5-
disallow_untyped_calls = false
2+
strict = true

setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ dev =
3636
# for tests
3737
tox
3838
# for type checking
39-
mypy == 0.931
40-
mypy-zope
39+
mypy == 1.7.1
4140
types-jsonschema
4241
types-PyYAML
4342
types-cachetools

src/matrix_content_scanner/crypto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, mcs: "MatrixContentScanner") -> None:
6565
)
6666

6767
# Generate a new key pair and turns it into a pickle.
68-
self._decryptor = PkDecryption()
68+
self._decryptor = PkDecryption() # type: ignore[no-untyped-call]
6969
pickle_bytes = self._decryptor.pickle(passphrase=key)
7070

7171
# Try to write the pickle's content into a file.

src/matrix_content_scanner/httpserver.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,5 @@ def start(self) -> None:
130130
app=self._app,
131131
host=self._bind_address,
132132
port=self._bind_port,
133-
# We need to ignore mypy's error here because what we do here is correct
134-
# according to aiohttp's documentation.
135-
# See https://github.com/aio-libs/aiohttp/issues/7077
136-
print=None, # type: ignore[arg-type]
133+
print=None,
137134
)

src/matrix_content_scanner/logutils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ def setup_custom_factory() -> None:
3030

3131
def _factory(*args: Any, **kwargs: Any) -> logging.LogRecord:
3232
record = old_factory(*args, **kwargs)
33-
# Define custom attributes on the records. We need to ignore the types here
34-
# because otherwise mypy complains the attributes aren't defined on LogRecord.
35-
record.request_id = request_id.get(None) # type: ignore[attr-defined]
33+
record.request_id = request_id.get(None)
3634
return record
3735

3836
logging.setLogRecordFactory(_factory)

src/matrix_content_scanner/scanner/file_downloader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import TYPE_CHECKING, Dict, Optional, Tuple
1919

2020
import aiohttp
21-
from multidict import CIMultiDictProxy, MultiDictProxy
21+
from multidict import CIMultiDictProxy, MultiMapping
2222

2323
from matrix_content_scanner.utils.constants import ErrCode
2424
from matrix_content_scanner.utils.errors import (
@@ -54,7 +54,7 @@ def __init__(self, mcs: "MatrixContentScanner"):
5454
async def download_file(
5555
self,
5656
media_path: str,
57-
thumbnail_params: Optional[MultiDictProxy[str]] = None,
57+
thumbnail_params: Optional[MultiMapping[str]] = None,
5858
) -> MediaDescription:
5959
"""Retrieve the file with the given `server_name/media_id` path, and stores it on
6060
disk.
@@ -156,7 +156,7 @@ async def _build_https_url(
156156
async def _get_file_content(
157157
self,
158158
url: str,
159-
thumbnail_params: Optional[MultiDictProxy[str]],
159+
thumbnail_params: Optional[MultiMapping[str]],
160160
) -> MediaDescription:
161161
"""Retrieve the content of the file at a given URL.
162162
@@ -304,7 +304,7 @@ async def _discover_via_well_known(self, domain: str) -> Optional[str]:
304304
async def _get(
305305
self,
306306
url: str,
307-
query: Optional[MultiDictProxy[str]] = None,
307+
query: Optional[MultiMapping[str]] = None,
308308
) -> Tuple[int, bytes, CIMultiDictProxy[str]]:
309309
"""Sends a GET request to the provided URL.
310310

src/matrix_content_scanner/scanner/scanner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from mautrix.crypto.attachments import decrypt_attachment
2828
from mautrix.errors import DecryptionError
2929
from mautrix.util import magic
30-
from multidict import MultiDictProxy
30+
from multidict import MultiMapping
3131

3232
from matrix_content_scanner.utils.constants import ErrCode
3333
from matrix_content_scanner.utils.errors import ContentScannerRestError, FileDirtyError
@@ -95,7 +95,7 @@ async def scan_file(
9595
self,
9696
media_path: str,
9797
metadata: Optional[JsonDict] = None,
98-
thumbnail_params: Optional[MultiDictProxy[str]] = None,
98+
thumbnail_params: Optional["MultiMapping[str]"] = None,
9999
) -> MediaDescription:
100100
"""Download and scan the given media.
101101
@@ -163,7 +163,7 @@ async def _scan_file(
163163
cache_key: str,
164164
media_path: str,
165165
metadata: Optional[JsonDict] = None,
166-
thumbnail_params: Optional[MultiDictProxy[str]] = None,
166+
thumbnail_params: Optional[MultiMapping[str]] = None,
167167
) -> MediaDescription:
168168
"""Download and scan the given media.
169169
@@ -368,7 +368,7 @@ def _get_cache_key_for_file(
368368
self,
369369
media_path: str,
370370
metadata: Optional[JsonDict],
371-
thumbnail_params: Optional[MultiDictProxy[str]],
371+
thumbnail_params: Optional[MultiMapping[str]],
372372
) -> str:
373373
"""Generates the key to use to store the result for the given media in the result
374374
cache.

tests/scanner/test_file_downloader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def _get(
6464

6565
# Mock _get so we don't actually try to download files.
6666
self.get_mock = Mock(side_effect=_get)
67-
self.downloader._get = self.get_mock # type: ignore[assignment]
67+
self.downloader._get = self.get_mock # type: ignore[method-assign]
6868

6969
async def test_download(self) -> None:
7070
"""Tests that downloading a file works."""
@@ -230,7 +230,7 @@ async def _get(
230230

231231
# Mock _get so we don't actually try to download files.
232232
self.get_mock = Mock(side_effect=_get)
233-
self.downloader._get = self.get_mock # type: ignore[assignment]
233+
self.downloader._get = self.get_mock # type: ignore[method-assign]
234234

235235
async def test_discover(self) -> None:
236236
"""Checks that the base URL to use to download files can be discovered via

tests/scanner/test_scanner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def download_file(
5252

5353
# Mock download_file so we don't actually try to download files.
5454
mcs = get_content_scanner()
55-
mcs.file_downloader.download_file = self.downloader_mock # type: ignore[assignment]
55+
mcs.file_downloader.download_file = self.downloader_mock # type: ignore[method-assign]
5656
self.scanner = mcs.scanner
5757

5858
async def test_scan(self) -> None:
@@ -161,7 +161,7 @@ async def test_cache_max_size_mismatching_hash(self) -> None:
161161
"""
162162
# Mock the _run_scan command so we can keep track of its call count.
163163
mock_runner = Mock(return_value=0)
164-
self.scanner._run_scan = mock_runner # type: ignore[assignment]
164+
self.scanner._run_scan = mock_runner # type: ignore[method-assign]
165165

166166
# Calculate the cache key for this file so we can look it up later.
167167
cache_key = self.scanner._get_cache_key_for_file(MEDIA_PATH, None, None)
@@ -269,7 +269,7 @@ async def test_dont_cache_exit_codes(self) -> None:
269269
# It's tricky to give a value to `scanner._script` that makes `_run_scan` return 5
270270
# directly, so we just mock it here.
271271
run_scan_mock = Mock(return_value=5)
272-
self.scanner._run_scan = run_scan_mock # type: ignore[assignment]
272+
self.scanner._run_scan = run_scan_mock # type: ignore[method-assign]
273273

274274
# Scan the file, we'll check later that it wasn't cached.
275275
with self.assertRaises(FileDirtyError):
@@ -318,7 +318,7 @@ async def _scan_file(*args: Any) -> MediaDescription:
318318
return self.downloader_res
319319

320320
scan_mock = Mock(side_effect=_scan_file)
321-
self.scanner._scan_file = scan_mock # type: ignore[assignment]
321+
self.scanner._scan_file = scan_mock # type: ignore[method-assign]
322322

323323
# Request two scans of the same file at the same time.
324324
results = await asyncio.gather(

0 commit comments

Comments
 (0)