Skip to content

Commit ab93a36

Browse files
authored
Fix overridden method of Some returning Any (#2559)
1 parent ae39025 commit ab93a36

3 files changed

Lines changed: 64 additions & 45 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ See [0Ver](https://0ver.org/).
1111

1212
- Add `python3.15` support
1313
- Add `mypy>=1.19,<2.5` support
14+
- Fix overridden method of `Some` returning `Any`
1415

1516

1617
## 0.29.1 WIP

‎returns/maybe.py‎

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -346,41 +346,43 @@ def __repr__(self):
346346
"""
347347
return '<Nothing>'
348348

349-
def map(self, function):
350-
"""Does nothing for ``Nothing``."""
351-
return self
349+
if not TYPE_CHECKING: # noqa: WPS604 # pragma: no branch
352350

353-
def apply(self, container):
354-
"""Does nothing for ``Nothing``."""
355-
return self
351+
def map(self, function):
352+
"""Does nothing for ``Nothing``."""
353+
return self
356354

357-
def bind(self, function):
358-
"""Does nothing for ``Nothing``."""
359-
return self
355+
def apply(self, container):
356+
"""Does nothing for ``Nothing``."""
357+
return self
360358

361-
def bind_optional(self, function):
362-
"""Does nothing."""
363-
return self
359+
def bind(self, function):
360+
"""Does nothing for ``Nothing``."""
361+
return self
364362

365-
def lash(self, function):
366-
"""Composes this container with a function returning container."""
367-
return function(None)
363+
def bind_optional(self, function):
364+
"""Does nothing."""
365+
return self
368366

369-
def value_or(self, default_value):
370-
"""Returns default value."""
371-
return default_value
367+
def lash(self, function):
368+
"""Composes this container with a function returning container."""
369+
return function(None)
372370

373-
def or_else_call(self, function):
374-
"""Returns the result of a passed function."""
375-
return function()
371+
def value_or(self, default_value):
372+
"""Returns default value."""
373+
return default_value
376374

377-
def unwrap(self):
378-
"""Raises an exception, since it does not have a value inside."""
379-
raise UnwrapFailedError(self)
375+
def or_else_call(self, function):
376+
"""Returns the result of a passed function."""
377+
return function()
380378

381-
def failure(self) -> None:
382-
"""Returns failed value."""
383-
return self._inner_value
379+
def unwrap(self):
380+
"""Raises an exception, since it does not have a value inside."""
381+
raise UnwrapFailedError(self)
382+
383+
def failure(self) -> None:
384+
"""Returns failed value."""
385+
return self._inner_value
384386

385387
def __bool__(self):
386388
"""Returns ``False``."""
@@ -417,29 +419,29 @@ def unwrap(self):
417419
"""Returns inner value for successful container."""
418420
return self._inner_value
419421

420-
def map(self, function):
421-
"""Composes current container with a pure function."""
422-
return Some(function(self._inner_value))
422+
def map(self, function):
423+
"""Composes current container with a pure function."""
424+
return Some(function(self._inner_value))
423425

424-
def apply(self, container):
425-
"""Calls a wrapped function in a container on this container."""
426-
if isinstance(container, Some):
427-
return self.map(container.unwrap()) # type: ignore
428-
return container
426+
def apply(self, container):
427+
"""Calls a wrapped function in a container on this container."""
428+
if isinstance(container, Some):
429+
return self.map(container.unwrap()) # type: ignore
430+
return container
429431

430-
def lash(self, function):
431-
"""Does nothing for ``Some``."""
432-
return self
432+
def lash(self, function):
433+
"""Does nothing for ``Some``."""
434+
return self
433435

434-
def value_or(self, default_value):
435-
"""Returns inner value for successful container."""
436-
return self._inner_value
436+
def value_or(self, default_value):
437+
"""Returns inner value for successful container."""
438+
return self._inner_value
437439

438-
def or_else_call(self, function):
439-
"""Returns inner value for successful container."""
440-
return self._inner_value
440+
def or_else_call(self, function):
441+
"""Returns inner value for successful container."""
442+
return self._inner_value
441443

442-
def failure(self):
444+
def failure(self) -> Never:
443445
"""Raises exception for successful container."""
444446
raise UnwrapFailedError(self)
445447

‎typesafety/test_maybe/test_maybe_type.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,19 @@
157157
from returns.maybe import Some
158158
159159
reveal_type(Some(1).unwrap()) # N: Revealed type is "int"
160+
161+
162+
- case: some_methods
163+
disable_cache: false
164+
main: |
165+
from returns.maybe import Some
166+
167+
def fallback() -> str:
168+
...
169+
170+
reveal_type(Some(1).map(str)) # N: Revealed type is "returns.maybe.Maybe[str]"
171+
reveal_type(Some(1).apply(Some(float))) # N: Revealed type is "returns.maybe.Maybe[float]"
172+
reveal_type(Some(1).lash(lambda _: Some(2))) # N: Revealed type is "returns.maybe.Maybe[int]"
173+
reveal_type(Some(1).value_or(None)) # N: Revealed type is "int | None"
174+
reveal_type(Some(1).or_else_call(fallback)) # N: Revealed type is "int | str"
175+
reveal_type(Some(1).failure()) # N: Revealed type is "Never"

0 commit comments

Comments
 (0)