Skip to content

Commit 7aede8b

Browse files
authored
Fix typing (#22)
* refactor(typing): 使用Union替换类型注解中的|操作符
1 parent 44e2436 commit 7aede8b

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
fail-fast: true
1212
matrix:
1313
os: [ "ubuntu-latest" ]
14-
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
14+
python-version: [ "3.8", "3.9", "3.10" ]
1515
runs-on: ${{ matrix.os }}
1616
steps:
1717
- uses: actions/checkout@v2
@@ -28,7 +28,7 @@ jobs:
2828
installer-parallel: true
2929
- name: Load cached venv
3030
id: cached-poetry-dependencies
31-
uses: actions/cache@v2
31+
uses: actions/cache@v3
3232
with:
3333
path: .venv
3434
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
@@ -40,4 +40,4 @@ jobs:
4040
- name: Run tests
4141
run: |
4242
source .venv/bin/activate
43-
pytest tests/
43+
pytest tests/

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "usepy"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
description = "usepy"
55
homepage = "https://usepy.code05.com/"
66
authors = ["miclon <jcnd@163.com>"]

src/usepy/list/first.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from typing import TypeVar, Sequence
2+
from typing_ import Union
3+
4+
25

36
T = TypeVar("T")
47

58

6-
def first(array: Sequence[T]) -> T | None:
9+
def first(array: Sequence[T]) -> Union[T, None]:
710
"""
811
Gets the first element of `array`.
912
1013
:param array: The array to query.
1114
:type array: Sequence[T]
1215
:return: Returns the first element of `array`.
13-
:rtype: T | None
16+
:rtype: Union[T, None]
1417
1518
:Example:
1619

src/usepy/list/last.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from typing import TypeVar, Sequence
2+
from typing_ import Union
23

34
T = TypeVar("T")
45

56

6-
def last(array: Sequence[T]) -> T | None:
7+
def last(array: Sequence[T]) -> Union[T, None]:
78
"""
89
Gets the last element of `array`.
910
1011
:param array: The array to query.
1112
:type array: Sequence[T]
1213
:return: Returns the last element of `array`.
13-
:rtype: T | None
14+
:rtype: Union[T, None]
1415
1516
:Example:
1617

src/usepy/list/sample.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import random
22
from typing import TypeVar, Sequence
3+
from typing_ import Union
34

45
T = TypeVar("T")
56

67

7-
def sample(arr: Sequence[T], count: int = 1) -> T | list[T]:
8+
def sample(arr: Sequence[T], count: int = 1) -> Union[T, list[T]]:
89
"""
910
Returns a random element from a sequence.
1011
@@ -13,9 +14,10 @@ def sample(arr: Sequence[T], count: int = 1) -> T | list[T]:
1314
1415
Args:
1516
arr (Sequence[T]): The sequence to sample from.
17+
count (int, optional): The number of elements to sample. Defaults to 1.
1618
1719
Returns:
18-
T: A random element from the sequence.
20+
Union[T, list[T]]: A random element from the sequence.
1921
2022
Example:
2123
>>> sample([1, 2, 3, 4, 5])

src/usepy/typing_.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
3+
4+
if sys.version_info >= (3, 10):
5+
from typing import Union
6+
else:
7+
from typing_extensions import Union

0 commit comments

Comments
 (0)