-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dict ambiguity #88
Comments
Hey @hodgespodge! Thanks for opening an issue. :) I believe the ambiguity arises from the following: In [1]: from beartype.door import is_bearable
In [2]: is_bearable({"1": "2"}, dict[int, int])
Out[2]: True This is not right. I will open an issue on Beartype. EDIT: Done. |
@hodgespodge Deep type checking of Unfortunately, this means that there is no current solution. :( You might have to go for something hacky like this: from typing import List, Tuple
from plum import dispatch
@dispatch
def _f(x: List[Tuple[str, str]]):
x = dict(x)
print("dict[str, str]")
@dispatch
def _f(x: List[Tuple[int, int]]):
x = dict(x)
print("dict[int, int]")
@dispatch
def f(x: dict):
return _f(list(x.items()))
f({1: 1})
f({"1": "1"}) You could use some decorators and other syntactic sugar to make this look nicer. If you're really into it, you could even use some magic to automatically convert I will keep this in mind and update this issue as soon as a better solution is available. |
@wesselb Thanks for investigating and getting back so quickly. I'll patiently look forward to the fix. Like I said, I'm really enjoying the library otherwise! |
I have been summoned. On behalf of all @beartype versions everywhere, lead @beartype guy @leycec is here to apologize for our present lack of deep type-checking of dictionaries. To summarize the current state of @beartype:
Alternately, anyone with a pressing need to deeply type-check dictionaries now can do so via our existing from beartype import beartype
from beartype.typing import Annotated
from beartype.vale import Is
# Type hint matching a dictionary mapping from strings to... other strings.
# Use "dict_of_strs_to_strs" instead of "dict[str, str]" for profit and fun.
dict_of_strs_to_strs = Annotated[
dict, Is[lambda mapping:
isinstance(next(iter(mapping.keys())), str) and
isinstance(next(iter(mapping.values())), str)
]]
@beartype
def muh_func(muh_dict: dict_of_strs_to_strs) -> str:
return (muh_dict['good key?'] if 'good key?' in muh_dict else 'bad key!')
# Prints: "...is good". Feel the good Summer vibrations, everyone -- except
# for those poor bastards in Australia and New Zealand. Pity them as they
# celebrate Summer Christmas with Aussie Santa Claus and his Kiwi reindeer.
print(muh_func({'good key?': '...is good'})) Tested and working. Punch a duck or just use |
Yesss, it is the one and only @leycec! Your tentative schedule of releases sound really promising. I will be looking forward to them very much. :) And thanks for pointing out the option to use |
I have not worked with covariant typing before so forgive if the following is ignorance on my part:
I assume the following code should be unambiguous. It fails due to ambiguity (tested with Python 3.11.3, plum-dispatch 2.1.1, and beartype 0.14.1 on Windows 10) :
Thanks for any clarification. I'm enjoying the library otherwise!
The text was updated successfully, but these errors were encountered: