diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cc424d1..cc3363c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ See [0Ver](https://0ver.org/). - Add `python3.15` support - Add `mypy>=1.19,<2.5` support +- Fix overridden method of `Some` returning `Any` ## 0.29.1 WIP diff --git a/returns/maybe.py b/returns/maybe.py index 7c9bc90f..2e1abc4d 100644 --- a/returns/maybe.py +++ b/returns/maybe.py @@ -346,41 +346,43 @@ def __repr__(self): """ return '' - def map(self, function): - """Does nothing for ``Nothing``.""" - return self + if not TYPE_CHECKING: # noqa: WPS604 # pragma: no branch - def apply(self, container): - """Does nothing for ``Nothing``.""" - return self + def map(self, function): + """Does nothing for ``Nothing``.""" + return self - def bind(self, function): - """Does nothing for ``Nothing``.""" - return self + def apply(self, container): + """Does nothing for ``Nothing``.""" + return self - def bind_optional(self, function): - """Does nothing.""" - return self + def bind(self, function): + """Does nothing for ``Nothing``.""" + return self - def lash(self, function): - """Composes this container with a function returning container.""" - return function(None) + def bind_optional(self, function): + """Does nothing.""" + return self - def value_or(self, default_value): - """Returns default value.""" - return default_value + def lash(self, function): + """Composes this container with a function returning container.""" + return function(None) - def or_else_call(self, function): - """Returns the result of a passed function.""" - return function() + def value_or(self, default_value): + """Returns default value.""" + return default_value - def unwrap(self): - """Raises an exception, since it does not have a value inside.""" - raise UnwrapFailedError(self) + def or_else_call(self, function): + """Returns the result of a passed function.""" + return function() - def failure(self) -> None: - """Returns failed value.""" - return self._inner_value + def unwrap(self): + """Raises an exception, since it does not have a value inside.""" + raise UnwrapFailedError(self) + + def failure(self) -> None: + """Returns failed value.""" + return self._inner_value def __bool__(self): """Returns ``False``.""" @@ -417,29 +419,29 @@ def unwrap(self): """Returns inner value for successful container.""" return self._inner_value - def map(self, function): - """Composes current container with a pure function.""" - return Some(function(self._inner_value)) + def map(self, function): + """Composes current container with a pure function.""" + return Some(function(self._inner_value)) - def apply(self, container): - """Calls a wrapped function in a container on this container.""" - if isinstance(container, Some): - return self.map(container.unwrap()) # type: ignore - return container + def apply(self, container): + """Calls a wrapped function in a container on this container.""" + if isinstance(container, Some): + return self.map(container.unwrap()) # type: ignore + return container - def lash(self, function): - """Does nothing for ``Some``.""" - return self + def lash(self, function): + """Does nothing for ``Some``.""" + return self - def value_or(self, default_value): - """Returns inner value for successful container.""" - return self._inner_value + def value_or(self, default_value): + """Returns inner value for successful container.""" + return self._inner_value - def or_else_call(self, function): - """Returns inner value for successful container.""" - return self._inner_value + def or_else_call(self, function): + """Returns inner value for successful container.""" + return self._inner_value - def failure(self): + def failure(self) -> Never: """Raises exception for successful container.""" raise UnwrapFailedError(self) diff --git a/typesafety/test_maybe/test_maybe_type.yml b/typesafety/test_maybe/test_maybe_type.yml index 4b021b2d..fea7dac9 100644 --- a/typesafety/test_maybe/test_maybe_type.yml +++ b/typesafety/test_maybe/test_maybe_type.yml @@ -157,3 +157,19 @@ from returns.maybe import Some reveal_type(Some(1).unwrap()) # N: Revealed type is "int" + + +- case: some_methods + disable_cache: false + main: | + from returns.maybe import Some + + def fallback() -> str: + ... + + reveal_type(Some(1).map(str)) # N: Revealed type is "returns.maybe.Maybe[str]" + reveal_type(Some(1).apply(Some(float))) # N: Revealed type is "returns.maybe.Maybe[float]" + reveal_type(Some(1).lash(lambda _: Some(2))) # N: Revealed type is "returns.maybe.Maybe[int]" + reveal_type(Some(1).value_or(None)) # N: Revealed type is "int | None" + reveal_type(Some(1).or_else_call(fallback)) # N: Revealed type is "int | str" + reveal_type(Some(1).failure()) # N: Revealed type is "Never"