Skip to content

Type hint for TypeConversionDict.get is too limited #2988

Closed

Description

@kasium

Given the following code

from werkzeug.datastructures import TypeConversionDict

d = TypeConversionDict()

def parse_int(value: str) -> int:
    return int(value)

d.get("foo", type=int)
d.get("foo", type=parse_int)

mypy complains with

foo.py:13:1: error: No overload variant of "get" of "TypeConversionDict" matches argument types "str", "Callable[[str], int]"  [call-overload]
    d.get("foo", type=parse_int)
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo.py:13:1: note: Possible overload variants:
foo.py:13:1: note:     def get(self, key: Any) -> Any | None
foo.py:13:1: note:     def get(self, key: Any, default: Any) -> Any
foo.py:13:1: note:     def [T] get(self, key: Any, default: T) -> Any | T
foo.py:13:1: note:     def [T] get(self, key: str, type: type[T]) -> T | None
foo.py:13:1: note:     def [T] get(self, key: str, default: T, type: type[T]) -> T

It seems like that the type hint for type is too limited and should also include callables as the docstring says

    :param type: A callable that is used to cast the value in the
                :class:`MultiDict`.  If a :exc:`ValueError` or a
                :exc:`TypeError` is raised by this callable the default
                value is returned.

Environment:

  • Python version: 3.12.0
  • Werkzeug version: 3.1.1
  • mypy version: 1.13.0

Activity

  1. added this to the 3.1.2 milestone on Nov 4, 2024
  2. locked as resolved and limited conversation to collaborators on Nov 20, 2024
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