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

Bump mypy-zope & mypy. #16188

Merged
merged 9 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16188.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
72 changes: 36 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions synapse/_scripts/synapse_port_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ def r(
do_backward[0] = False

if forward_rows or backward_rows:
headers = [column[0] for column in txn.description]
assert txn.description is not None
headers: Optional[List[str]] = [
column[0] for column in txn.description
]
else:
headers = None

Expand Down Expand Up @@ -544,6 +547,7 @@ async def handle_search_table(
def r(txn: LoggingTransaction) -> Tuple[List[str], List[Tuple]]:
txn.execute(select, (forward_chunk, self.batch_size))
rows = txn.fetchall()
assert txn.description is not None
headers = [column[0] for column in txn.description]

return headers, rows
Expand Down Expand Up @@ -919,7 +923,8 @@ async def _setup_sent_transactions(self) -> Tuple[int, int, int]:
def r(txn: LoggingTransaction) -> Tuple[List[str], List[Tuple]]:
txn.execute(select)
rows = txn.fetchall()
headers: List[str] = [column[0] for column in txn.description]
assert txn.description is not None
headers = [column[0] for column in txn.description]

ts_ind = headers.index("ts")

Expand Down
14 changes: 6 additions & 8 deletions synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,10 @@ def _wrapping_logic(func: Callable[P, R], *args: P.args, **kwargs: P.kwargs) ->
async def _wrapper(
*args: P.args, **kwargs: P.kwargs
) -> Any: # Return type is RInner
with wrapping_logic(func, *args, **kwargs):
# type-ignore: func() returns R, but mypy doesn't know that R is
# Awaitable here.
return await func(*args, **kwargs) # type: ignore[misc]
# type-ignore: func() returns R, but mypy doesn't know that R is
# Awaitable here.
with wrapping_logic(func, *args, **kwargs): # type: ignore[arg-type]
Comment on lines +913 to +915
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this comment might not have made perfect sense, but the tl;dr is that mypy ends up with:

synapse/logging/opentracing.py:916: error: Argument 1 has incompatible type "Callable[P, CoroutineType[Any, Any, Any]]"; expected "Callable[P, R]"  [arg-type]

So it doesn't know that func(...) will return Awaitable[R].

return await func(*args, **kwargs)

else:
# The other case here handles sync functions including those decorated with
Expand Down Expand Up @@ -980,8 +980,7 @@ def trace_with_opname(
See the module's doc string for usage examples.
"""

# type-ignore: mypy bug, see https://github.com/python/mypy/issues/12909
@contextlib.contextmanager # type: ignore[arg-type]
@contextlib.contextmanager
def _wrapping_logic(
func: Callable[P, R], *args: P.args, **kwargs: P.kwargs
) -> Generator[None, None, None]:
Expand Down Expand Up @@ -1024,8 +1023,7 @@ def tag_args(func: Callable[P, R]) -> Callable[P, R]:
if not opentracing:
return func

# type-ignore: mypy bug, see https://github.com/python/mypy/issues/12909
@contextlib.contextmanager # type: ignore[arg-type]
@contextlib.contextmanager
def _wrapping_logic(
func: Callable[P, R], *args: P.args, **kwargs: P.kwargs
) -> Generator[None, None, None]:
Expand Down
17 changes: 16 additions & 1 deletion synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Iterator,
List,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Expand Down Expand Up @@ -358,7 +359,21 @@ def rowcount(self) -> int:
return self.txn.rowcount

@property
def description(self) -> Any:
def description(
self,
) -> Optional[
Sequence[
Tuple[
str,
Optional[Any],
Optional[int],
Optional[int],
Optional[int],
Optional[int],
Optional[int],
]
]
]:
return self.txn.description

def execute_batch(self, sql: str, args: Iterable[Iterable[Any]]) -> None:
Expand Down
6 changes: 3 additions & 3 deletions synapse/util/check_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def dependencies(self) -> Iterable[str]:


DEV_EXTRAS = {"lint", "mypy", "test", "dev"}
RUNTIME_EXTRAS = (
set(metadata.metadata(DISTRIBUTION_NAME).get_all("Provides-Extra")) - DEV_EXTRAS
)
ALL_EXTRAS = metadata.metadata(DISTRIBUTION_NAME).get_all("Provides-Extra")
assert ALL_EXTRAS is not None
RUNTIME_EXTRAS = set(ALL_EXTRAS) - DEV_EXTRAS
VERSION = metadata.version(DISTRIBUTION_NAME)


Expand Down
6 changes: 3 additions & 3 deletions tests/appservice/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def get_json(
)

# We assign to a method, which mypy doesn't like.
self.api.get_json = Mock(side_effect=get_json) # type: ignore[assignment]
self.api.get_json = Mock(side_effect=get_json) # type: ignore[method-assign]

result = self.get_success(
self.api.query_3pe(self.service, "user", PROTOCOL, {b"some": [b"field"]})
Expand Down Expand Up @@ -168,7 +168,7 @@ async def get_json(
)

# We assign to a method, which mypy doesn't like.
self.api.get_json = Mock(side_effect=get_json) # type: ignore[assignment]
self.api.get_json = Mock(side_effect=get_json) # type: ignore[method-assign]

result = self.get_success(
self.api.query_3pe(self.service, "user", PROTOCOL, {b"some": [b"field"]})
Expand Down Expand Up @@ -215,7 +215,7 @@ async def post_json_get_json(
return RESPONSE

# We assign to a method, which mypy doesn't like.
self.api.post_json_get_json = Mock(side_effect=post_json_get_json) # type: ignore[assignment]
self.api.post_json_get_json = Mock(side_effect=post_json_get_json) # type: ignore[method-assign]

MISSING_KEYS = [
# Known user, known device, missing algorithm.
Expand Down
24 changes: 12 additions & 12 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_complexity_simple(self) -> None:
async def get_current_state_event_counts(room_id: str) -> int:
return int(500 * 1.23)

store.get_current_state_event_counts = get_current_state_event_counts # type: ignore[assignment]
store.get_current_state_event_counts = get_current_state_event_counts # type: ignore[method-assign]

# Get the room complexity again -- make sure it's our artificial value
channel = self.make_signed_federation_request(
Expand All @@ -74,8 +74,8 @@ def test_join_too_large(self) -> None:
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[assignment]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[assignment]
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
return_value=("", 1)
)

Expand Down Expand Up @@ -105,8 +105,8 @@ def test_join_too_large_admin(self) -> None:
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[assignment]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[assignment]
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
return_value=("", 1)
)

Expand Down Expand Up @@ -142,16 +142,16 @@ def test_join_too_large_once_joined(self) -> None:
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = AsyncMock(return_value=None) # type: ignore[assignment]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[assignment]
fed_transport.client.get_json = AsyncMock(return_value=None) # type: ignore[method-assign]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
return_value=("", 1)
)

# Artificially raise the complexity
async def get_current_state_event_counts(room_id: str) -> int:
return 600

self.hs.get_datastores().main.get_current_state_event_counts = get_current_state_event_counts # type: ignore[assignment]
self.hs.get_datastores().main.get_current_state_event_counts = get_current_state_event_counts # type: ignore[method-assign]

d = handler._remote_join(
create_requester(u1),
Expand Down Expand Up @@ -199,8 +199,8 @@ def test_join_too_large_no_admin(self) -> None:
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[assignment]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[assignment]
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
return_value=("", 1)
)

Expand Down Expand Up @@ -229,8 +229,8 @@ def test_join_too_large_admin(self) -> None:
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[assignment]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[assignment]
fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
return_value=("", 1)
)

Expand Down
4 changes: 2 additions & 2 deletions tests/federation/test_federation_catch_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
# This mock is crucial for destination_rooms to be populated.
# TODO: this seems to no longer be the case---tests pass with this mock
# commented out.
state_storage_controller.get_current_hosts_in_room = AsyncMock( # type: ignore[assignment]
state_storage_controller.get_current_hosts_in_room = AsyncMock( # type: ignore[method-assign]
return_value={"test", "host2"}
)

Expand Down Expand Up @@ -436,7 +436,7 @@ def test_catch_up_on_synapse_startup(self) -> None:
def wake_destination_track(destination: str) -> None:
woken.add(destination)

self.federation_sender.wake_destination = wake_destination_track # type: ignore[assignment]
self.federation_sender.wake_destination = wake_destination_track # type: ignore[method-assign]

# We wait quite long so that all dests can be woken up, since there is a delay
# between them.
Expand Down
4 changes: 2 additions & 2 deletions tests/federation/test_federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
federation_transport_client=self.federation_transport_client,
)

hs.get_storage_controllers().state.get_current_hosts_in_room = AsyncMock( # type: ignore[assignment]
hs.get_storage_controllers().state.get_current_hosts_in_room = AsyncMock( # type: ignore[method-assign]
return_value={"test", "host2"}
)

hs.get_storage_controllers().state.get_current_hosts_in_room_or_partial_state_approximation = ( # type: ignore[assignment]
hs.get_storage_controllers().state.get_current_hosts_in_room_or_partial_state_approximation = ( # type: ignore[method-assign]
hs.get_storage_controllers().state.get_current_hosts_in_room
)

Expand Down
Loading
Loading