Skip to content
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

Using >1 different Union raises TypeError #79

Closed
IsaacBreen opened this issue Dec 30, 2022 · 1 comment
Closed

Using >1 different Union raises TypeError #79

IsaacBreen opened this issue Dec 30, 2022 · 1 comment

Comments

@IsaacBreen
Copy link

IsaacBreen commented Dec 30, 2022

I tried to find a minimal example (tested with Python 3.10 and 3.11):

from multimethod import multimethod

@multimethod
def f(x: int | float): ...

@multimethod
def f(x: tuple | list): ...
~/miniforge3/envs/py311/lib/python3.11/site-packages/multimethod/__init__.py:110 in                  │
│ __subclasscheck__                                                                                                    │
│                                                                                                                      │
│   107 │   │   │   return issubclass(origin, self.__origin__)                                                         │
│   108 │   │   return (  # check args first to avoid a recursion error in ABCMeta                                     │
│   109 │   │   │   len(args) == nargs                                                                                 │
│ ❱ 110 │   │   │   and issubclass(origin, self.__origin__)                                                            │
│   111 │   │   │   and all(map(issubclass, args, self.__args__))                                                      │
│   112 │   │   )                                                                                                      │
│   113                                                                                                                │
│                                                                                                                      │
│ ╭─────────────────── locals ────────────────────╮                                                                    │
│ │     args = (<class 'tuple'>, <class 'list'>)  │                                                                    │
│ │    nargs = 2                                  │                                                                    │
│ │   origin = tuple | list                       │                                                                    │
│ │     self = <class 'multimethod.int | float'>  │                                                                    │
│ │ subclass = <class 'multimethod.tuple | list'> │                                                                    │
│ ╰───────────────────────────────────────────────╯                                                                    │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: issubclass() arg 1 must be a class

Worth noting that this works fine:

@multimethod
def f(x: Union[int, float]): ...

@multimethod
def f(x: tuple | list): ...
@coady
Copy link
Owner

coady commented Dec 30, 2022

Interesting, the | syntax isn't just syntactic sugar for Union. It added a new UnionType, which will need its own support.

Thanks for the report.

coady added a commit that referenced this issue Dec 30, 2022
`UnionType` only works with `issubclass` as the right arg, not the left. So the `Union` implementation is reused instead. Refs #79.
@coady coady closed this as completed Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants