coady / multimethod

Multiple argument dispatching.
https://coady.github.io/multimethod
Other
277 stars 24 forks source link

type `__args__` should be retrieved using `typing.get_args()` #107

Closed kszucs closed 7 months ago

kszucs commented 8 months ago

My types happen to define __args__ which when used with multimethod raise the following error:

    def __new__(cls, tp, *args):
        if tp is Any:
            return object
        if hasattr(tp, '__supertype__'):  # isinstance(..., NewType) only supported >=3.10
            tp = tp.__supertype__
        if isinstance(tp, TypeVar):
            if not tp.__constraints__:
                return object
            tp = Union[tp.__constraints__]
        origin = getattr(tp, '__origin__', tp)
        if hasattr(types, 'UnionType') and isinstance(tp, types.UnionType):
            origin = Union  # `|` syntax added in 3.10
>       args = tuple(map(cls, getattr(tp, '__args__', args)))
E       TypeError: 'member_descriptor' object is not iterable

I modified multimethod's source to use the more robust typing.get_args() instead which fixed this issue.

coady commented 7 months ago

Released in v1.11.

kszucs commented 7 months ago

Great, thank you!