element-hq / element-hq/synapse

Adopt `ParamSpec` for type hints

Open
#11,711 0 comments 0 reactions 0 assignees View on GitHub
T-Task
Dominant language
Python
Stars
4.6k
Forks
600
Avg merge
5d 22h
Merged PRs (30d)
51

Description

This issue has been migrated from [#11711](https://github.com/matrix-org/synapse/issues/11711).

---

[PEP-612](https://www.python.org/dev/peps/pep-0612/) adds `ParamSpec`, which will allow us to correctly annotate:
* Decorators which preserve parameters and return types. These will no longer require `cast`s.
* Decorators which change the return type can be correctly annotated and type checked.
* Basically anything that handles `*args` and `**kwargs`.

**mypy 0.931**
(blocked on #11712)

As of mypy 0.931, basic `ParamSpec` usage is supported, plus `P.args` and `P.kwargs`. These styles of function can be annotated:
```py
def decorate(f: Callable[P, R]) -> Callable[P, Optional[R]]:
def wrapped(*args: P.args, **kwargs: P.kwargs) -> Optional[R]:
...
```
```py
def call(f: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
...
```
`Concatenate` and `def call(f: Callable[P, R], *args: P.args, some_flag: bool = False, **kwargs: P.kwargs)` (extra kwarg) don't work yet.

Contributor guide

Open the contributing guide

Research direction

Start by reading PEP 612 and checking the stated mypy 0.931 support for ParamSpec, P.args, and P.kwargs. Search the Synapse codebase for decorators and functions handling *args and **kwargs, then assess whether their annotations can adopt ParamSpec without casts; account for the stated Concatenate limitations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.