Skip to content

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

Description

@alexdotgov2

def __init__(
self,
app: WSGIApplication,
exports: (
dict[str, str | tuple[str, str]]
| t.Iterable[tuple[str, str | tuple[str, str]]]
),
disallow: None = None,
cache: bool = True,
cache_timeout: int = 60 * 60 * 12,
fallback_mimetype: str = "application/octet-stream",
) -> None:

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions