matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.82k stars 2.13k forks source link

Type information incorrect when @cached methods are passed as parameters #16606

Open clokep opened 12 months ago

clokep commented 12 months ago

If a method that is wrapped in @cached is passed into another function, e.g. run_in_background then the type information is incorrect; the current mypy plugin only updates the type information when it is called as a method, not when it is accessed as an attribute.

E.g. from #16590:

synapse/push/bulk_push_rule_evaluator.py:359: error: Missing positional argument "room_id" in call to "run_in_background"  [call-arg]
synapse/push/bulk_push_rule_evaluator.py:360: error: Argument 2 to "run_in_background" has incompatible type "str"; expected "RoomMemberWorkerStore"  [arg-type]

The method is not getting treated as bound (and there might be other issues too).

clokep commented 12 months ago

I think we might need to expand the plugin to update the information when these sorts of methods are access as attributes instead of just when they're called as methods. mypy has other hooks we can use, but we need to be careful:

  1. That we don't apply the change twice (both as an attribute & as a method).
  2. That when access as an attribute it still appears as a CachedDescriptor (so that it has the invalidate method, etc.)