diff --git a/README.md b/README.md index f468bbb..4d4de36 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ dev * Python >=3.8 required * `Type[...]` dispatches on class arguments * `|` syntax for union types +* `overload` supports generics 1.9.1 diff --git a/multimethod/__init__.py b/multimethod/__init__.py index 7a50b09..eb077c7 100644 --- a/multimethod/__init__.py +++ b/multimethod/__init__.py @@ -397,7 +397,7 @@ def __new__(cls, func): def __init__(self, func: Callable): for name, value in get_type_hints(func).items(): - if not callable(value) or isinstance(value, type): + if not callable(value) or isinstance(value, type) or hasattr(value, '__origin__'): func.__annotations__[name] = isa(value) self[inspect.signature(func)] = func diff --git a/pyproject.toml b/pyproject.toml index 6587a74..ab08cb4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ classifiers = [ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Topic :: Software Development :: Libraries :: Python Modules", "Typing :: Typed", ] diff --git a/tests/test_overload.py b/tests/test_overload.py index 006dba7..0f86581 100644 --- a/tests/test_overload.py +++ b/tests/test_overload.py @@ -1,5 +1,5 @@ import pytest -from typing import List, Literal +from typing import List, Literal, Optional from multimethod import DispatchError, isa, overload @@ -45,3 +45,12 @@ def test_generic(): assert not pred([0.0]) assert pred(0.0) assert not pred(1.0) + + @overload + def func(arg: Optional[str]): + return arg + + assert func('') == '' + assert func(None) is None + with pytest.raises(DispatchError): + func(0)