Skip to content

Breaks under __future__.annotations #104

Open

Description

@jtrakk

Annotations are becoming string-valued by default in a future version. This feature is accessible in Python 3.7 with from __future__ import annotations.

This breaks the current implementation of multipledispatch.dispatch():

from __future__ import annotations

import multipledispatch


@multipledispatch.dispatch()
def func(x):
    raise NotImplementedError(x)


@multipledispatch.dispatch()
def func(x: int):
    return f"x {x} int"


print(func(1))

Traceback (most recent call last):
  File "/home/user/tmp/single.py", line 11, in <module>
    @multipledispatch.dispatch()
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/multipledispatch/core.py", line 68, in _
    dispatcher.add(types, func)
  File "/home/user/.local/pipx/venvs/ipython/lib/python3.7/site-packages/multipledispatch/dispatcher.py", line 225, in add
    (typ, str_sig, self.name))
TypeError: Tried to dispatch on non-type: int
In signature: <int>
In function: func

The solution that functools.singledispatch uses is to call typing.get_type_hints() to evaluate the hint strings.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions