Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ See [0Ver](https://0ver.org/).

## 0.31.0 WIP

### Features

- Adds missing type annotations

### Bugfixes

- Fix `{Success, Failure}.swap` returning `Any`
Expand Down
2 changes: 1 addition & 1 deletion returns/contrib/hypothesis/laws.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def check_all_laws(
)


def pure_functions_factory(thing) -> st.SearchStrategy:
def pure_functions_factory(thing: Any) -> st.SearchStrategy:
"""Factory to create pure functions."""
like = (
(lambda: None)
Expand Down
10 changes: 5 additions & 5 deletions returns/contrib/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ def __init__(self, errors_handled: _ErrorsHandled) -> None:

@staticmethod # noqa: WPS602
def assert_equal( # noqa: WPS602
first,
second,
first: object,
second: object,
*,
deps=None,
deps: object | None = None,
backend: str = 'asyncio',
) -> None:
"""Can compare two containers even with extra calling and awaiting."""
from returns.primitives.asserts import assert_equal # noqa: PLC0415

assert_equal(first, second, deps=deps, backend=backend)

def is_error_handled(self, container) -> bool:
def is_error_handled(self, container: 'ResultLikeN[Any, Any, Any]') -> bool:
"""Ensures that container has its error handled in the end."""
return id(container) in self._errors_handled

Expand Down Expand Up @@ -131,7 +131,7 @@ class _DesiredFunctionFound(BaseException): # noqa: WPS418
"""Exception to raise when expected function is found."""


def pytest_configure(config) -> None:
def pytest_configure(config: pytest.Config) -> None:
"""
Hook to be executed on import.

Expand Down
4 changes: 2 additions & 2 deletions returns/methods/cond.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar, overload
from typing import Final, TypeVar, overload

from returns.context import NoDeps
from returns.interfaces.failable import DiverseFailableN, SingleFailableN
Expand Down Expand Up @@ -79,6 +79,6 @@ def internal_cond(
return container_type.empty


cond = kinded(internal_cond)
cond: Final = kinded(internal_cond)
"""Kinded version of :func:`~internal_cond`,
use it to infer real return type."""
6 changes: 3 additions & 3 deletions returns/primitives/asserts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def assert_equal(
first,
second,
first: object,
second: object,
*,
deps=None,
deps: object | None = None,
backend: str = 'asyncio',
) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion returns/primitives/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BaseContainer(Immutable, ABC):
__slots__ = ('_inner_value',)
_inner_value: Any

def __init__(self, inner_value) -> None:
def __init__(self, inner_value: object) -> None:
"""
Wraps the given value in the Container.

Expand Down
6 changes: 4 additions & 2 deletions returns/primitives/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Self

if TYPE_CHECKING:
from returns.interfaces.unwrappable import Unwrappable # noqa: WPS433
Expand All @@ -11,6 +11,8 @@ class UnwrapFailedError(Exception):

__slots__ = ('halted_container',)

halted_container: Unwrappable

def __init__(self, container: Unwrappable) -> None:
"""
Saves halted container in the inner state.
Expand All @@ -21,7 +23,7 @@ def __init__(self, container: Unwrappable) -> None:
super().__init__()
self.halted_container = container

def __reduce__(self): # noqa: WPS603
def __reduce__(self) -> tuple[type[Self], tuple[Unwrappable]]: # noqa: WPS603
"""Custom reduce method for pickle protocol.

This helps properly reconstruct the exception during unpickling.
Expand Down
4 changes: 2 additions & 2 deletions returns/primitives/hkt.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def impossible(generic: T, value: V) -> T[V]:

if TYPE_CHECKING: # noqa: WPS604 # pragma: no cover

def __getattr__(self, attrname: str):
def __getattr__(self, attrname: str) -> Any:
"""
This function is required for ``get_attribute_hook`` in mypy plugin.

Expand Down Expand Up @@ -214,7 +214,7 @@ class Kinded(Protocol[_FunctionDefType_co]): # type: ignore
def __get__(
self,
instance: _UpdatedType,
type_,
type_: type[Any] | None,
) -> Callable[..., _UpdatedType]:
"""Used to decorate and properly analyze method calls."""

Expand Down
6 changes: 3 additions & 3 deletions returns/primitives/laws.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Sequence
from typing import ClassVar, Final, Generic, TypeVar, final
from typing import Any, ClassVar, Final, Generic, TypeVar, final

from returns.primitives.types import Immutable

Expand All @@ -25,10 +25,10 @@ class Law(Immutable):

__slots__ = ('definition',)

definition: Callable
definition: Callable[..., Any]
"""Function used to define this law."""

def __init__(self, function) -> None:
def __init__(self, function: Callable[..., Any]) -> None:
"""Saves function to the inner state."""
object.__setattr__(self, 'definition', function)

Expand Down
6 changes: 5 additions & 1 deletion returns/trampolines.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Callable
from functools import wraps
from typing import Generic, TypeVar, final
from typing import Any, Generic, TypeVar, final

from typing_extensions import ParamSpec

Expand All @@ -18,6 +18,10 @@ class Trampoline(Generic[_ReturnType]):

__slots__ = ('args', 'func', 'kwargs')

func: Callable[..., _ReturnType]
args: tuple[Any, ...]
kwargs: dict[str, Any]

def __init__( # noqa: WPS451
self,
func: Callable[_FuncParams, _ReturnType],
Expand Down
7 changes: 7 additions & 0 deletions typesafety/test_contrib/test_pytest/test_returns_asserts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- case: is_error_handled_non_container
disable_cache: false
main: |
from returns.contrib.pytest import ReturnsAsserts

def test(returns: ReturnsAsserts) -> None:
returns.is_error_handled(1) # E: Argument 1 to "is_error_handled" of "ReturnsAsserts" has incompatible type "int"; expected "ResultLikeN[Any, Any, Any]" [arg-type]
8 changes: 8 additions & 0 deletions typesafety/test_methods/test_cond.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@
out: |
main:16: note: Revealed type is "main.MyOwn[str, float]"
main:16: error: Only concrete class can be given where "type[MyOwn[Any, Any]]" is expected [type-abstract]


- case: cond_final
disable_cache: false
main: |
from returns.methods import cond

cond = cond # E: Cannot assign to final name "cond" [misc]
11 changes: 11 additions & 0 deletions typesafety/test_trampolines/test_trampoline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@
return Trampoline(_accumulate, [1], 2)

reveal_type(_accumulate([1, 2])) # N: Revealed type is "int"


- case: trampoline_attributes
disable_cache: false
main: |
from returns.trampolines import Trampoline

def test(t: Trampoline[int]) -> None:
reveal_type(t.func) # N: Revealed type is "def (*Any, **Any) -> int"
reveal_type(t.args) # N: Revealed type is "tuple[Any, ...]"
reveal_type(t.kwargs) # N: Revealed type is "dict[str, Any]"
Loading