From 3eb4f60ddf0c8236aff030850a1dd9b6805ad409 Mon Sep 17 00:00:00 2001 From: miclon Date: Thu, 17 Jul 2025 09:44:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor(typing):=20=E4=BD=BF=E7=94=A8Union?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=B1=BB=E5=9E=8B=E6=B3=A8=E8=A7=A3=E4=B8=AD?= =?UTF-8?q?=E7=9A=84|=E6=93=8D=E4=BD=9C=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 统一使用Union类型注解以提高代码兼容性,特别是在旧版本Python中的支持 --- src/usepy/list/first.py | 7 +++++-- src/usepy/list/last.py | 5 +++-- src/usepy/list/sample.py | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/usepy/list/first.py b/src/usepy/list/first.py index fcb1415..8e73396 100644 --- a/src/usepy/list/first.py +++ b/src/usepy/list/first.py @@ -1,16 +1,19 @@ from typing import TypeVar, Sequence +from typing_ import Union + + T = TypeVar("T") -def first(array: Sequence[T]) -> T | None: +def first(array: Sequence[T]) -> Union[T, None]: """ Gets the first element of `array`. :param array: The array to query. :type array: Sequence[T] :return: Returns the first element of `array`. - :rtype: T | None + :rtype: Union[T, None] :Example: diff --git a/src/usepy/list/last.py b/src/usepy/list/last.py index 05ccbc1..6e3101a 100644 --- a/src/usepy/list/last.py +++ b/src/usepy/list/last.py @@ -1,16 +1,17 @@ from typing import TypeVar, Sequence +from typing_ import Union T = TypeVar("T") -def last(array: Sequence[T]) -> T | None: +def last(array: Sequence[T]) -> Union[T, None]: """ Gets the last element of `array`. :param array: The array to query. :type array: Sequence[T] :return: Returns the last element of `array`. - :rtype: T | None + :rtype: Union[T, None] :Example: diff --git a/src/usepy/list/sample.py b/src/usepy/list/sample.py index 1a8cb53..363aa21 100644 --- a/src/usepy/list/sample.py +++ b/src/usepy/list/sample.py @@ -1,10 +1,11 @@ import random from typing import TypeVar, Sequence +from typing_ import Union T = TypeVar("T") -def sample(arr: Sequence[T], count: int = 1) -> T | list[T]: +def sample(arr: Sequence[T], count: int = 1) -> Union[T, list[T]]: """ Returns a random element from a sequence. @@ -13,9 +14,10 @@ def sample(arr: Sequence[T], count: int = 1) -> T | list[T]: Args: arr (Sequence[T]): The sequence to sample from. + count (int, optional): The number of elements to sample. Defaults to 1. Returns: - T: A random element from the sequence. + Union[T, list[T]]: A random element from the sequence. Example: >>> sample([1, 2, 3, 4, 5]) From e3066dbf83f65c98da9bf289e7cba917769a7e5e Mon Sep 17 00:00:00 2001 From: miclon Date: Thu, 17 Jul 2025 09:44:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat(typing):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=20Python=203.10=20=E4=BB=A5=E4=B8=8B=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=20Union=20=E7=B1=BB=E5=9E=8B=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/usepy/typing_.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/usepy/typing_.py diff --git a/src/usepy/typing_.py b/src/usepy/typing_.py new file mode 100644 index 0000000..9aa6ce4 --- /dev/null +++ b/src/usepy/typing_.py @@ -0,0 +1,7 @@ +import sys + + +if sys.version_info >= (3, 10): + from typing import Union +else: + from typing_extensions import Union From a43f2da9fd45d0d83c42056dd115833aca9622f9 Mon Sep 17 00:00:00 2001 From: miclon Date: Thu, 17 Jul 2025 09:46:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?ci:=20=E5=8D=87=E7=BA=A7=20actions/cache=20?= =?UTF-8?q?=E4=BB=8E=20v2=20=E5=88=B0=20v3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ce61952..80a6cab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,7 +28,7 @@ jobs: installer-parallel: true - name: Load cached venv id: cached-poetry-dependencies - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: .venv key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }} @@ -40,4 +40,4 @@ jobs: - name: Run tests run: | source .venv/bin/activate - pytest tests/ \ No newline at end of file + pytest tests/