Skip to content

Commit

Permalink
Merge pull request #2 from alexandermalyga/simplify-generics
Browse files Browse the repository at this point in the history
Simplify generics
  • Loading branch information
alexandermalyga authored Jan 14, 2023
2 parents f148059 + 9881bb0 commit 1ebd311
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
8 changes: 4 additions & 4 deletions poltergeist/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@final
@dataclass(repr=False, frozen=True, slots=True)
class Ok(Generic[T, E]):
class Ok(Generic[T]):
_value: T

def __repr__(self) -> str:
Expand All @@ -31,13 +31,13 @@ def unwrap_or(self, default: DefaultT) -> T:
def unwrap_or(self, default: Any = None) -> Any:
return self.unwrap()

def unwrap_or_else(self, op: Callable[[E], DefaultT]) -> T:
def unwrap_or_else(self, op: Any) -> T:
return self.unwrap()


@final
@dataclass(repr=False, eq=False, frozen=True, slots=True)
class Err(Generic[T, E]):
class Err(Generic[E]):
_err: E

def __repr__(self) -> str:
Expand Down Expand Up @@ -71,4 +71,4 @@ def __eq__(self, __o: Any) -> bool:
)


Result = Ok[T, E] | Err[T, E]
Result = Ok[T] | Err[E]
12 changes: 6 additions & 6 deletions tests/mypy/test_decorator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@poltergeist
def test(a: int, b: str) -> float | None: ...
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None], builtins.Exception], poltergeist.result.Err[Union[builtins.float, None], builtins.Exception]]"
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None]], poltergeist.result.Err[builtins.Exception]]"
- case: decorator_default
main: |
Expand All @@ -14,7 +14,7 @@
@poltergeist()
def test(a: int, b: str) -> float | None: ...
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None], builtins.Exception], poltergeist.result.Err[Union[builtins.float, None], builtins.Exception]]"
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None]], poltergeist.result.Err[builtins.Exception]]"
- case: decorator_with_args
main: |
Expand All @@ -23,7 +23,7 @@
@poltergeist(error=ValueError)
def test(a: int, b: str) -> float | None: ...
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None], builtins.ValueError], poltergeist.result.Err[Union[builtins.float, None], builtins.ValueError]]"
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None]], poltergeist.result.Err[builtins.ValueError]]"
- case: decorator_invalid_error_type
main: |
Expand All @@ -34,6 +34,6 @@
out: |
main:3: error: No overload variant of "poltergeist" matches argument type "int" [call-overload]
main:3: note: Possible overload variants:
main:3: note: def [P`-1, T] poltergeist(Callable[P, T], /) -> Callable[P, Union[Ok[T, Exception], Err[T, Exception]]]
main:3: note: def poltergeist() -> Callable[[Callable[P, T]], Callable[P, Union[Ok[T, Exception], Err[T, Exception]]]]
main:3: note: def [E <: BaseException] poltergeist(*, error: Type[E]) -> Callable[[Callable[P, T]], Callable[P, Union[Ok[T, E], Err[T, E]]]]
main:3: note: def [P`-1, T] poltergeist(Callable[P, T], /) -> Callable[P, Union[Ok[T], Err[Exception]]]
main:3: note: def poltergeist() -> Callable[[Callable[P, T]], Callable[P, Union[Ok[T], Err[Exception]]]]
main:3: note: def [E <: BaseException] poltergeist(*, error: Type[E]) -> Callable[[Callable[P, T]], Callable[P, Union[Ok[T], Err[E]]]]
12 changes: 6 additions & 6 deletions tests/mypy/test_err.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
- case: err_generic
main: |
from poltergeist import Err
instance: Err[str, Exception] = Err(123) # E: Argument 1 to "Err" has incompatible type "int"; expected "Exception" [arg-type]
other: Err[str, int] # E: Type argument "int" of "Err" must be a subtype of "BaseException" [type-var]
instance: Err[Exception] = Err(123) # E: Argument 1 to "Err" has incompatible type "int"; expected "Exception" [arg-type]
other: Err[int] # E: Type argument "int" of "Err" must be a subtype of "BaseException" [type-var]
- case: err_err
main: |
from poltergeist import Err
instance: Err[str, Exception]
instance: Err[Exception]
reveal_type(instance.err()) # N: Revealed type is "builtins.Exception"
- case: err_unwrap
main: |
from poltergeist import Err
instance: Err[str, Exception]
instance: Err[Exception]
reveal_type(instance.unwrap()) # N: Revealed type is "<nothing>"
- case: err_unwrap_or
main: |
from poltergeist import Err
instance: Err[str, Exception]
instance: Err[Exception]
reveal_type(instance.unwrap_or()) # N: Revealed type is "None"
reveal_type(instance.unwrap_or(123)) # N: Revealed type is "builtins.int"
reveal_type(instance.unwrap_or("abc")) # N: Revealed type is "builtins.str"
- case: err_unwrap_or_else
main: |
from poltergeist import Err
instance: Err[str, Exception]
instance: Err[Exception]
reveal_type(instance.unwrap_or_else(lambda e: e)) # N: Revealed type is "builtins.Exception"
instance.unwrap_or_else(123) # E: Argument 1 to "unwrap_or_else" of "Err" has incompatible type "int"; expected "Callable[[Exception], <nothing>]" [arg-type]
default: str = instance.unwrap_or_else(lambda e: e) # E: Incompatible types in assignment (expression has type "Exception", variable has type "str") [assignment]
12 changes: 5 additions & 7 deletions tests/mypy/test_ok.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
- case: ok_generic
main: |
from poltergeist import Ok
instance: Ok[str, Exception] = Ok(123) # E: Argument 1 to "Ok" has incompatible type "int"; expected "str" [arg-type]
other: Ok[str, int] # E: Type argument "int" of "Ok" must be a subtype of "BaseException" [type-var]
instance: Ok[str] = Ok(123) # E: Argument 1 to "Ok" has incompatible type "int"; expected "str" [arg-type]
- case: ok_err
main: |
from poltergeist import Ok
instance: Ok[str, Exception]
instance: Ok[str]
reveal_type(instance.err()) # N: Revealed type is "None"
- case: ok_unwrap
main: |
from poltergeist import Ok
instance: Ok[str, Exception]
instance: Ok[str]
reveal_type(instance.unwrap()) # N: Revealed type is "builtins.str"
- case: ok_unwrap_or
main: |
from poltergeist import Ok
instance: Ok[str, Exception]
instance: Ok[str]
reveal_type(instance.unwrap_or()) # N: Revealed type is "builtins.str"
reveal_type(instance.unwrap_or(123)) # N: Revealed type is "builtins.str"
reveal_type(instance.unwrap_or("abc")) # N: Revealed type is "builtins.str"
- case: ok_unwrap_or_else
main: |
from poltergeist import Ok
instance: Ok[str, Exception]
instance: Ok[str]
reveal_type(instance.unwrap_or_else(lambda e: e)) # N: Revealed type is "builtins.str"
instance.unwrap_or_else(123) # E: Argument 1 to "unwrap_or_else" of "Ok" has incompatible type "int"; expected "Callable[[Exception], <nothing>]" [arg-type]
10 changes: 2 additions & 8 deletions tests/mypy/test_result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from poltergeist import Result, Ok, Err
ok: Result[str, Exception] = Ok(123) # E: Argument 1 to "Ok" has incompatible type "int"; expected "str" [arg-type]
err: Result[str, Exception] = Err(123) # E: Argument 1 to "Err" has incompatible type "int"; expected "Exception" [arg-type]
other: Result[str, int]
out: |
main:4: error: Type argument "int" of "Ok" must be a subtype of "BaseException" [type-var]
main:4: error: Type argument "int" of "Err" must be a subtype of "BaseException" [type-var]
other: Result[str, int] # E: Type argument "int" of "Err" must be a subtype of "BaseException" [type-var]
- case: result_err
main: |
Expand Down Expand Up @@ -34,7 +31,4 @@
instance: Result[str, Exception]
reveal_type(instance.unwrap_or_else(lambda e: e)) # N: Revealed type is "Union[builtins.str, builtins.Exception]"
default: str = instance.unwrap_or_else(lambda e: e) # E: Incompatible types in assignment (expression has type "Union[str, Exception]", variable has type "str") [assignment]
instance.unwrap_or_else(123)
out: |
main:5: error: Argument 1 to "unwrap_or_else" of "Ok" has incompatible type "int"; expected "Callable[[Exception], <nothing>]" [arg-type]
main:5: error: Argument 1 to "unwrap_or_else" of "Err" has incompatible type "int"; expected "Callable[[Exception], <nothing>]" [arg-type]
instance.unwrap_or_else(123) # E: Argument 1 to "unwrap_or_else" of "Err" has incompatible type "int"; expected "Callable[[Exception], <nothing>]" [arg-type]
8 changes: 4 additions & 4 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def test_ok() -> None:
result: Result[str, Exception] = Ok("abc")
result = Ok("abc")

match result:
case Ok(v):
Expand All @@ -24,15 +24,15 @@ def test_ok() -> None:


def test_ok_eq() -> None:
result: Result[str, Exception] = Ok("abc")
result = Ok("abc")
assert result == Ok("abc")
assert result != Ok("aaa")
assert result != Err(Exception("abc"))
assert result != "abc"


def test_error() -> None:
result: Result[str, ValueError] = Err(ValueError("abc"))
result = Err(ValueError("abc"))

match result:
case Err(e):
Expand All @@ -58,7 +58,7 @@ def test_error() -> None:


def test_err_eq() -> None:
result: Result[str, ValueError] = Err(ValueError("abc"))
result = Err(ValueError("abc"))
assert result == Err(ValueError("abc"))
assert result != Err(ValueError("aaa"))
assert result != Err(ValueError("abc", 1))
Expand Down

0 comments on commit 1ebd311

Please sign in to comment.