deps: replace custom `Option` class with `returns.maybe.Maybe` (#489)

This commit is contained in:
Nyakku Shigure 2025-04-05 04:49:11 +08:00 committed by GitHub
parent e8a72e1a23
commit 1490c4a7dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 76 deletions

View File

@ -29,6 +29,7 @@ dependencies = [
"httpx[http2,socks]>=0.28.1",
"tomli>=2.0.2; python_version < '3.11'",
"pydantic>=2.11.2",
"returns>=0.25.0",
]
[project.urls]

View File

@ -11,7 +11,7 @@ from yutto.bilibili_typing.quality import (
from yutto.cli.settings import YuttoSettings, load_settings_file, search_for_settings_file
from yutto.processor.parser import alias_parser, path_from_cli
from yutto.utils.console.logger import Logger
from yutto.utils.funcutils.option import map_some
from yutto.utils.funcutils.functional import map_optional
if TYPE_CHECKING:
from collections.abc import Sequence
@ -135,7 +135,7 @@ def cli() -> argparse.ArgumentParser:
)
group_basic.add_argument(
"--tmp-dir",
default=map_some(path_from_cli, settings.basic.tmp_dir),
default=map_optional(path_from_cli, settings.basic.tmp_dir),
type=path_from_cli,
help="用来存放下载过程中临时文件的目录,默认为下载目录",
)

View File

@ -0,0 +1,16 @@
from __future__ import annotations
from typing import TYPE_CHECKING, TypeVar
from returns.maybe import Maybe
if TYPE_CHECKING:
from collections.abc import Callable
T = TypeVar("T")
U = TypeVar("U")
def map_optional(fn: Callable[[T], U], value: T | None) -> U | None:
return Maybe.from_optional(value).map(fn).value_or(None)

View File

@ -1,74 +0,0 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Generic, NoReturn, Protocol, TypeVar
if TYPE_CHECKING:
from collections.abc import Callable
T = TypeVar("T")
U = TypeVar("U")
class Option(Protocol, Generic[T]):
def __init__(self): ...
def is_some(self) -> bool: ...
def is_none(self) -> bool: ...
def map(self, fn: Callable[[T], Any]) -> Option[Any]: ...
def unwrap(self) -> T: ...
def unwrap_or(self, default: T) -> T:
return self.unwrap() if self.is_some() else default
@staticmethod
def from_optional(value: U | None) -> Option[U]:
return Some(value) if value is not None else None_()
def to_optional(self) -> T | None:
return self.unwrap() if self.is_some() else None
def __bool__(self) -> bool:
return self.is_some()
class Some(Option[T]):
value: T
def __init__(self, value: T):
self.value = value
def is_some(self) -> bool:
return True
def is_none(self) -> bool:
return False
def map(self, fn: Callable[[T], U]) -> Option[U]:
return Some(fn(self.value))
def unwrap(self) -> T:
return self.value
class None_(Option[Any]):
def __init__(self): ...
def is_some(self) -> bool:
return False
def is_none(self) -> bool:
return True
def map(self, fn: Callable[[Any], Any]) -> Option[Any]:
return None_()
def unwrap(self) -> NoReturn:
raise ValueError("Cannot unwrap None_ object")
def map_some(fn: Callable[[T], U], value: T | None) -> U | None:
return Option.from_optional(value).map(fn).to_optional()

14
uv.lock
View File

@ -465,6 +465,18 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/89/37/54e5ffc7c0cebee7cf30a3ac5915faa7e7abf8bdfdf3228c277f7c192489/pytest_rerunfailures-15.0-py3-none-any.whl", hash = "sha256:dd150c4795c229ef44320adc9a0c0532c51b78bb7a6843a8c53556b9a611df1a", size = 13017 },
]
[[package]]
name = "returns"
version = "0.25.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/69/2c/90479667b4e46759c11d7c0e04f2ffa47e3cdabb1984d06a004e6c523ef2/returns-0.25.0.tar.gz", hash = "sha256:1bf547311c0ade25435ce3bbe81642c325ea6b86beaf5d624cd410f0dee3ff50", size = 105128 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/18/95/3fd02e08fa0d9ec9127b8f1379eda36b0b070096eee1a7038042508ae381/returns-0.25.0-py3-none-any.whl", hash = "sha256:bdc6ec52d28e74d6965f6de5a3af5e39427e67266014b605865fe2e194a75ed0", size = 160145 },
]
[[package]]
name = "rich"
version = "14.0.0"
@ -622,6 +634,7 @@ dependencies = [
{ name = "dict2xml" },
{ name = "httpx", extra = ["http2", "socks"] },
{ name = "pydantic" },
{ name = "returns" },
{ name = "tomli", marker = "python_full_version < '3.11'" },
{ name = "typing-extensions" },
]
@ -645,6 +658,7 @@ requires-dist = [
{ name = "dict2xml", specifier = ">=1.7.6" },
{ name = "httpx", extras = ["http2", "socks"], specifier = ">=0.28.1" },
{ name = "pydantic", specifier = ">=2.11.2" },
{ name = "returns", specifier = ">=0.25.0" },
{ name = "tomli", marker = "python_full_version < '3.11'", specifier = ">=2.0.2" },
{ name = "typing-extensions", specifier = ">=4.13.1" },
]