From 6cbec9b87469171ca5a343d98a392c662ed3deb6 Mon Sep 17 00:00:00 2001 From: Alexander Malyga Date: Sun, 21 Apr 2024 13:38:16 +0200 Subject: [PATCH] Mark _T and _E type vars as covariant --- poltergeist/result.py | 4 ++-- tests/mypy/test_result.yml | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/poltergeist/result.py b/poltergeist/result.py index 84f043b..680d3c7 100644 --- a/poltergeist/result.py +++ b/poltergeist/result.py @@ -1,7 +1,7 @@ from typing import Any, Callable, Generic, NoReturn, TypeVar, final, overload -_T = TypeVar("_T") -_E = TypeVar("_E", bound=BaseException) +_T = TypeVar("_T", covariant=True) +_E = TypeVar("_E", bound=BaseException, covariant=True) _D = TypeVar("_D") diff --git a/tests/mypy/test_result.yml b/tests/mypy/test_result.yml index 913d81a..c7e1957 100644 --- a/tests/mypy/test_result.yml +++ b/tests/mypy/test_result.yml @@ -5,6 +5,23 @@ err: Result[str, Exception] = Err(123) # E: Argument 1 to "Err" has incompatible type "int"; expected "Exception" [arg-type] other: Result[str, int] # E: Type argument "int" of "Result" must be a subtype of "BaseException" [type-var] +- case: result_generic_covariant + main: | + from abc import ABC, abstractmethod + from typing import Any, Generic, TypeVar + from poltergeist import Err, Result + + FooT = TypeVar("FooT", bound=Result[Any, Exception]) + + class Foo(Generic[FooT], ABC): + @abstractmethod + def run(self) -> FooT: + ... + + class Bar(Foo[Result[Any, ValueError]]): + def run(self) -> Result[Any, ValueError]: + return Err(ValueError("")) + - case: result_err main: | from poltergeist import Result