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

Commit

Permalink
Update mypy and mypy-zope (#13925)
Browse files Browse the repository at this point in the history
* Update mypy and mypy-zope

* Unignore assigning to LogRecord attributes

Presumably python/typeshed#8064 makes this ok

Cherry-picked from #13521

* Remove unused ignores due to mypy ParamSpec fixes

python/mypy#12668

Cherry-picked from #13521

* Remove additional unused ignores

* Fix new mypy complaints related to `assertGreater`

Presumably due to python/typeshed#8077

* Changelog

* Reword changelog

Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>

Co-authored-by: Patrick Cloke <clokep@users.noreply.github.com>
  • Loading branch information
David Robertson and clokep authored Sep 30, 2022
1 parent b2aadd8 commit 6d543d6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 67 deletions.
1 change: 1 addition & 0 deletions changelog.d/13925.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update mypy (0.950 -> 0.981) and mypy-zope (0.3.7 -> 0.3.11).
59 changes: 30 additions & 29 deletions poetry.lock

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

5 changes: 2 additions & 3 deletions scripts-dev/check_pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ def make_wrapper(factory: Callable[P, R]) -> Callable[P, R]:

@functools.wraps(factory)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
# type-ignore: should be redundant once we can use https://github.com/python/mypy/pull/12668
if "strict" not in kwargs: # type: ignore[attr-defined]
if "strict" not in kwargs:
raise MissingStrictInConstrainedTypeException(factory.__name__)
if not kwargs["strict"]: # type: ignore[index]
if not kwargs["strict"]:
raise MissingStrictInConstrainedTypeException(factory.__name__)
return factory(*args, **kwargs)

Expand Down
4 changes: 1 addition & 3 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def register_sighup(func: Callable[P, None], *args: P.args, **kwargs: P.kwargs)
func: Function to be called when sent a SIGHUP signal.
*args, **kwargs: args and kwargs to be passed to the target function.
"""
# This type-ignore should be redundant once we use a mypy release with
# https://github.com/python/mypy/pull/12668.
_sighup_callbacks.append((func, args, kwargs)) # type: ignore[arg-type]
_sighup_callbacks.append((func, args, kwargs))


def start_worker_reactor(
Expand Down
20 changes: 10 additions & 10 deletions synapse/logging/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,29 +586,29 @@ def filter(self, record: logging.LogRecord) -> Literal[True]:
True to include the record in the log output.
"""
context = current_context()
record.request = self._default_request # type: ignore
record.request = self._default_request

# context should never be None, but if it somehow ends up being, then
# we end up in a death spiral of infinite loops, so let's check, for
# robustness' sake.
if context is not None:
# Logging is interested in the request ID. Note that for backwards
# compatibility this is stored as the "request" on the record.
record.request = str(context) # type: ignore
record.request = str(context)

# Add some data from the HTTP request.
request = context.request
if request is None:
return True

record.ip_address = request.ip_address # type: ignore
record.site_tag = request.site_tag # type: ignore
record.requester = request.requester # type: ignore
record.authenticated_entity = request.authenticated_entity # type: ignore
record.method = request.method # type: ignore
record.url = request.url # type: ignore
record.protocol = request.protocol # type: ignore
record.user_agent = request.user_agent # type: ignore
record.ip_address = request.ip_address
record.site_tag = request.site_tag
record.requester = request.requester
record.authenticated_entity = request.authenticated_entity
record.method = request.method
record.url = request.url
record.protocol = request.protocol
record.user_agent = request.user_agent

return True

Expand Down
4 changes: 2 additions & 2 deletions synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,9 @@ def _wrapping_logic(
# FIXME: We could update this to handle any type of function by ignoring the
# first argument only if it's named `self` or `cls`. This isn't fool-proof
# but handles the idiomatic cases.
for i, arg in enumerate(args[1:], start=1): # type: ignore[index]
for i, arg in enumerate(args[1:], start=1):
set_tag(SynapseTags.FUNC_ARG_PREFIX + argspec.args[i], str(arg))
set_tag(SynapseTags.FUNC_ARGS, str(args[len(argspec.args) :])) # type: ignore[index]
set_tag(SynapseTags.FUNC_ARGS, str(args[len(argspec.args) :]))
set_tag(SynapseTags.FUNC_KWARGS, str(kwargs))
yield

Expand Down
22 changes: 6 additions & 16 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ def call_after(
# LoggingTransaction isn't expecting there to be any callbacks; assert that
# is not the case.
assert self.after_callbacks is not None
# type-ignore: need mypy containing https://github.com/python/mypy/pull/12668
self.after_callbacks.append((callback, args, kwargs)) # type: ignore[arg-type]
self.after_callbacks.append((callback, args, kwargs))

def async_call_after(
self, callback: Callable[P, Awaitable], *args: P.args, **kwargs: P.kwargs
Expand All @@ -312,8 +311,7 @@ def async_call_after(
# LoggingTransaction isn't expecting there to be any callbacks; assert that
# is not the case.
assert self.async_after_callbacks is not None
# type-ignore: need mypy containing https://github.com/python/mypy/pull/12668
self.async_after_callbacks.append((callback, args, kwargs)) # type: ignore[arg-type]
self.async_after_callbacks.append((callback, args, kwargs))

def call_on_exception(
self, callback: Callable[P, object], *args: P.args, **kwargs: P.kwargs
Expand All @@ -331,8 +329,7 @@ def call_on_exception(
# LoggingTransaction isn't expecting there to be any callbacks; assert that
# is not the case.
assert self.exception_callbacks is not None
# type-ignore: need mypy containing https://github.com/python/mypy/pull/12668
self.exception_callbacks.append((callback, args, kwargs)) # type: ignore[arg-type]
self.exception_callbacks.append((callback, args, kwargs))

def fetchone(self) -> Optional[Tuple]:
return self.txn.fetchone()
Expand Down Expand Up @@ -421,10 +418,7 @@ def _do_execute(
sql = self.database_engine.convert_param_style(sql)
if args:
try:
# The type-ignore should be redundant once mypy releases a version with
# https://github.com/python/mypy/pull/12668. (`args` might be empty,
# (but we'll catch the index error if so.)
sql_logger.debug("[SQL values] {%s} %r", self.name, args[0]) # type: ignore[index]
sql_logger.debug("[SQL values] {%s} %r", self.name, args[0])
except Exception:
# Don't let logging failures stop SQL from working
pass
Expand Down Expand Up @@ -655,19 +649,15 @@ def new_transaction(
# For now, we just log an error, and hope that it works on the first attempt.
# TODO: raise an exception.

# Type-ignore Mypy doesn't yet consider ParamSpec.args to be iterable; see
# https://github.com/python/mypy/pull/12668
for i, arg in enumerate(args): # type: ignore[arg-type, var-annotated]
for i, arg in enumerate(args):
if inspect.isgenerator(arg):
logger.error(
"Programming error: generator passed to new_transaction as "
"argument %i to function %s",
i,
func,
)
# Type-ignore Mypy doesn't yet consider ParamSpec.args to be a mapping; see
# https://github.com/python/mypy/pull/12668
for name, val in kwargs.items(): # type: ignore[attr-defined]
for name, val in kwargs.items():
if inspect.isgenerator(val):
logger.error(
"Programming error: generator passed to new_transaction as "
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ async def search_rooms(
raise Exception("Unrecognized database engine")

# mypy expects to append only a `str`, not an `int`
args.append(limit) # type: ignore[arg-type]
args.append(limit)

results = await self.db_pool.execute(
"search_rooms", self.db_pool.cursor_to_dict, sql, *args
Expand Down
6 changes: 6 additions & 0 deletions tests/storage/test_monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ def test_initialise_reserved_users(self):

# Test each of the registered users is marked as active
timestamp = self.get_success(self.store.user_last_seen_monthly_active(user1))
# Mypy notes that one shouldn't compare Optional[int] to 0 with assertGreater.
# Check that timestamp really is an int.
assert timestamp is not None
self.assertGreater(timestamp, 0)
timestamp = self.get_success(self.store.user_last_seen_monthly_active(user2))
assert timestamp is not None
self.assertGreater(timestamp, 0)

# Test that users with reserved 3pids are not removed from the MAU table
Expand Down Expand Up @@ -166,9 +170,11 @@ def test_user_last_seen_monthly_active(self):
self.get_success(self.store.upsert_monthly_active_user(user_id2))

result = self.get_success(self.store.user_last_seen_monthly_active(user_id1))
assert result is not None
self.assertGreater(result, 0)

result = self.get_success(self.store.user_last_seen_monthly_active(user_id3))
assert result is not None
self.assertNotEqual(result, 0)

@override_config({"max_mau_value": 5})
Expand Down
4 changes: 1 addition & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ def looping_call(
*args: P.args,
**kwargs: P.kwargs,
) -> None:
# This type-ignore should be redundant once we use a mypy release with
# https://github.com/python/mypy/pull/12668.
self.loopers.append(Looper(function, interval / 1000.0, self.now, args, kwargs)) # type: ignore[arg-type]
self.loopers.append(Looper(function, interval / 1000.0, self.now, args, kwargs))

def cancel_call_later(self, timer: Timer, ignore_errs: bool = False) -> None:
if timer.expired:
Expand Down

0 comments on commit 6d543d6

Please sign in to comment.