I think this is a bug with plum, but perhaps its some other weird interaction, or just too new of beartype.
The gist is that I've been using @beartype for a while, and was trying to use plum to simplify some function overloading. So I have my beartype'd dataclass, which as of 0.14 supports Self, but adding @dispatch decorator to a member function produces these weird errors.
Python 3.8.16 (default, Apr 22 2023, 20:36:01)
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from beartype import beartype
>>> from plum import dispatch
>>> from dataclasses import dataclass
>>> from typing_extensions import Self
>>> @beartype
... @dataclass
... class Tst:
... data: str
... @dispatch
... def print(self: Self):
... print(self.data)
...
>>> t = Tst('hi')
>>> t.print()
.../python3.8/site-packages/plum/signature.py:203: UserWarning: Could not resolve the type hint of `typing_extensions.Self`. I have ended the resolution here to not make your code break, but some types might not be working correctly. Please open an issue at https://github.com/wesselb/plum.
annotation = resolve_type_hint(p.annotation)
.../python3.8/site-packages/plum/type.py:262: UserWarning: Could not resolve the type hint of `typing_extensions.Self`. I have ended the resolution here to not make your code break, but some types might not be working correctly. Please open an issue at https://github.com/wesselb/plum.
return _is_faithful(resolve_type_hint(x))
.../python3.8/site-packages/plum/type.py:262: UserWarning: Could not determine whether `typing_extensions.Self` is faithful or not. I have concluded that the type is not faithful, so your code might run with subpar performance. Please open an issue at https://github.com/wesselb/plum.
return _is_faithful(resolve_type_hint(x))
Traceback (most recent call last):
File ".../python3.8/site-packages/plum/function.py", line 347, in __call__
method, return_type = self._cache[types]
KeyError: (<class '__main__.Tst'>,)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3.8/site-packages/plum/function.py", line 419, in __call__
return self._f(self._instance, *args, **kw_args)
File ".../python3.8/site-packages/plum/function.py", line 350, in __call__
method, return_type = self.resolve_method(args, types)
File ".../python3.8/site-packages/plum/function.py", line 280, in resolve_method
signature = self._resolver.resolve(target)
File ".../python3.8/site-packages/plum/resolver.py", line 99, in resolve
for signature in [s for s in self.signatures if check(s)]:
File ".../python3.8/site-packages/plum/resolver.py", line 99, in <listcomp>
for signature in [s for s in self.signatures if check(s)]:
File ".../python3.8/site-packages/plum/resolver.py", line 90, in check
return s.match(target)
File ".../python3.8/site-packages/plum/signature.py", line 153, in match
return all(_is_bearable(v, t) for v, t in zip(values, types))
File ".../python3.8/site-packages/plum/signature.py", line 153, in <genexpr>
return all(_is_bearable(v, t) for v, t in zip(values, types))
File ".../python3.8/site-packages/beartype/door/_doorcheck.py", line 322, in is_bearable
reraise_exception_placeholder(
File ".../python3.8/site-packages/beartype/_util/error/utilerror.py", line 212, in reraise_exception_placeholder
raise exception.with_traceback(exception.__traceback__)
File ".../python3.8/site-packages/beartype/door/_doorcheck.py", line 317, in is_bearable
func_tester = make_func_tester(hint, conf)
File ".../python3.8/site-packages/beartype/_util/cache/utilcachecall.py", line 278, in _callable_cached
raise exception
File ".../python3.8/site-packages/beartype/_util/cache/utilcachecall.py", line 270, in _callable_cached
return_value = args_flat_to_return_value[args_flat] = func(
File ".../python3.8/site-packages/beartype/_check/checkmake.py", line 183, in make_func_tester
hint = sanify_hint_root_statement(
File ".../python3.8/site-packages/beartype/_check/conv/convsanify.py", line 240, in sanify_hint_root_statement
hint = reduce_hint(hint=hint, conf=conf, exception_prefix=exception_prefix)
File ".../python3.8/site-packages/beartype/_check/conv/convreduce.py", line 145, in reduce_hint
hint = _reduce_hint_uncached(
File ".../python3.8/site-packages/beartype/_check/conv/convreduce.py", line 279, in _reduce_hint_uncached
hint = hint_reducer( # type: ignore[call-arg]
File ".../python3.8/site-packages/beartype/_util/hint/pep/proposal/utilpep673.py", line 82, in reduce_hint_pep673
raise BeartypeDecorHintPep673Exception(
beartype.roar.BeartypeDecorHintPep673Exception: is_bearable() PEP 673 type hint "typing_extensions.Self" invalid outside @beartype-decorated class. PEP 673 type hints are valid only inside classes decorated by @beartype. If this hint annotates a method decorated by @beartype, instead decorate the class declaring this method by @beartype: e.g.,
# Instead of decorating methods by @beartype like this...
class BadClassIsBad(object):
@beartype
def awful_method_is_awful(self: Self) -> Self:
return self
# ...decorate classes by @beartype instead - like this!
@beartype
class GoodClassIsGood(object):
def wonderful_method_is_wonderful(self: Self) -> Self:
return self
This has been a message of the Bearhugger Broadcasting Service.
I think this is a bug with plum, but perhaps its some other weird interaction, or just too new of beartype. The gist is that I've been using @beartype for a while, and was trying to use plum to simplify some function overloading. So I have my beartype'd dataclass, which as of 0.14 supports Self, but adding @dispatch decorator to a member function produces these weird errors.
Python 3.8 Beartype 0.14 Plum 2.01 Typing Extensions 4.6.2
MWE: