plum: "Could not resolve the type hint of" warning, for TypeVar

Hi, thanks for the awesome library! Super useful.

I’ve been getting this warning:

image

Reproduce:

from typing import TypeVar

from plum import dispatch


class Foo:
    ...


FooClass = TypeVar("FooClass")


@dispatch
def bar(created_chat: int) -> int:
    return ...


@dispatch
def bar(
    foo_cls: FooClass = Foo,
) -> FooClass:
    return ...


bar()

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 21 (12 by maintainers)

Most upvoted comments

Note to the unwise and the unwary: @beartype and thus Plum should superficially support a TypeVar with either:

  • Explicit bounds (e.g., TypeVar('FooClass', bound=Foo)).
  • Explicit constraints (e.g., TypeVar('FooClass', Foo)).

In either case, @beartype and thus Plum should reduce that TypeVar to that bounds and those constraints. This may require @beartype 0.16.0, which I’m on the precipice of releasing this Friday. Dare I do it? I dare.

As @wesselb suggests, full-blown type variable support at runtime is non-trivial in the extreme and not something anyone wants to voluntarily tackle. You’ll either need full-time grant funding for that and/or Asperger’s. I have the latter but not the former. Therefore, I’ll eventually tackle this – but only after having exhausted every other outstanding feature request and issue in @beartype. It is the highest of the high-hanging fruit. It cannot be plucked without back pain, kidney pain, a blown ACL, and a ruptured Achilles heel. Don’t go down that road, @giladbarnea.

@ilan-gold The warnings here can be ignored. Your code will run, but may not behave as expected.

Consider the following (very contrived) example:

from plum import dispatch
from typing import TypeVar


class Str2(str):
    pass

T = TypeVar("T", bound=str)

@dispatch
def f(x: T) -> T:
    return Str2(x)


f("hey")  # Works, but shouldn't, because the input is `str` and the output a `Str2`!