pallets / werkzeug

The comprehensive WSGI web application library.
https://werkzeug.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
6.66k stars 1.73k forks source link

Type annotations for SharedDataMiddleware produce mypy errors in common circumstances #2958

Closed alexdotgov2 closed 1 month ago

alexdotgov2 commented 1 month ago

https://github.com/pallets/werkzeug/blob/5add63c955131fd73531d7369f16b2f1b4e342d4/src/werkzeug/middleware/shared_data.py#L102-L113

exports is annotated as dict[str, str | tuple[str, str]] (plus the iterable case). Because dict is invariant, passing a simple dict[str, str] actually produces a mypy error! Users can work around this by manually annotating the data as dict[str, str | tuple[str, str]], but this is very unergonomic.

https://mypy-play.net/?mypy=latest&python=3.12&gist=7194ebea2b603890033453b8d735e232 is a minimal demonstration of this issue, and the recommended workaround: annotating the value as collections.Mapping instead.