Replies: 7 comments
-
It seems it is not a very good choice to implement to accepting empty dict as dict[K, V] if there are two kind dict: dict[K1, V1] and dict[K2, V2] There are following three condition:
currently, multimethod use condition 1 |
Beta Was this translation helpful? Give feedback.
-
Good summary, and there's another problematic case. If empty collections are supported, an empty list called on a method with But what if it's So there's a conundrum where the only sensible behavior is likely to be surprising to the average user. |
Beta Was this translation helpful? Give feedback.
-
There's a prototype in branch empty to experiment with, but I'm not sure about this. It could just be a limitation of a dynamically typed language. |
Beta Was this translation helpful? Give feedback.
-
I suppose you could just fall back on a catchall: @multimethod
def foo(xs: List[int]): ...
@multimethod
def foo(xs: List[bool]): ...
@multimethod
def foo(xs: List):
# deal with the empty case explicitly
if not len(xs):
return "unknown"
raise TypeError(...) It'd be cool if @multimethod
def foo(xs: Literal[[]]):
return "unknown"
# ~/.pyenv/versions/3.8.5/envs/hacking/lib/python3.8/site-packages/multimethod/__init__.py in __new__(cls, tp, *args)
# 45 origin = getattr(tp, '__extra__', getattr(tp, '__origin__', tp))
# 46 args = tuple(map(cls, getattr(tp, '__args__', None) or args))
# ---> 47 if set(args) <= {object} and not (origin is tuple and args):
# 48 return origin
# 49 bases = (origin,) if type(origin) is type else ()
#
# TypeError: unhashable type: 'list' |
Beta Was this translation helpful? Give feedback.
-
The problem is with |
Beta Was this translation helpful? Give feedback.
-
Converting to a discussion since there's no clear path forward. |
Beta Was this translation helpful? Give feedback.
-
Implemented in d7727e5. |
Beta Was this translation helpful? Give feedback.
-
code:
run and get output
similarly, I cannot pass empty list
[]
intolist[int]
Beta Was this translation helpful? Give feedback.
All reactions