Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typing][PEP585 Upgrade][BUAA][11-20] Use standard collections for type hints for 8 files in python/paddle/ #67096

Merged
merged 10 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/paddle/audio/datasets/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List
from __future__ import annotations

import paddle

Expand All @@ -33,8 +33,8 @@ class AudioClassificationDataset(paddle.io.Dataset):

def __init__(
self,
files: List[str],
labels: List[int],
files: list[str],
labels: list[int],
feat_type: str = 'raw',
sample_rate: int = None,
**kwargs,
Expand Down
7 changes: 4 additions & 3 deletions python/paddle/audio/datasets/esc50.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

import collections
import os
from typing import List, Tuple

from paddle.dataset.common import DATA_HOME
from paddle.utils import download
Expand Down Expand Up @@ -166,14 +167,14 @@ def __init__(
files=files, labels=labels, feat_type=feat_type, **kwargs
)

def _get_meta_info(self) -> List[collections.namedtuple]:
def _get_meta_info(self) -> list[collections.namedtuple]:
ret = []
with open(os.path.join(DATA_HOME, self.meta), 'r') as rf:
for line in rf.readlines()[1:]:
ret.append(self.meta_info(*line.strip().split(',')))
return ret

def _get_data(self, mode: str, split: int) -> Tuple[List[str], List[int]]:
def _get_data(self, mode: str, split: int) -> tuple[list[str], list[int]]:
if not os.path.isdir(
os.path.join(DATA_HOME, self.audio_path)
) or not os.path.isfile(os.path.join(DATA_HOME, self.meta)):
Expand Down
7 changes: 4 additions & 3 deletions python/paddle/audio/datasets/tess.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

import collections
import os
from typing import List, Tuple

from paddle.dataset.common import DATA_HOME
from paddle.utils import download
Expand Down Expand Up @@ -111,7 +112,7 @@ def __init__(
files=files, labels=labels, feat_type=feat_type, **kwargs
)

def _get_meta_info(self, files) -> List[collections.namedtuple]:
def _get_meta_info(self, files) -> list[collections.namedtuple]:
ret = []
for file in files:
basename_without_extend = os.path.basename(file)[:-4]
Expand All @@ -120,7 +121,7 @@ def _get_meta_info(self, files) -> List[collections.namedtuple]:

def _get_data(
self, mode: str, n_folds: int, split: int
) -> Tuple[List[str], List[int]]:
) -> tuple[list[str], list[int]]:
if not os.path.isdir(os.path.join(DATA_HOME, self.audio_path)):
download.get_path_from_url(
self.archive['url'],
Expand Down
9 changes: 5 additions & 4 deletions python/paddle/audio/functional/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
from __future__ import annotations

import math
from typing import List, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -39,7 +40,7 @@ def get(self, name):


@window_function_register.register()
def _cat(x: List[Tensor], data_type: str) -> Tensor:
def _cat(x: list[Tensor], data_type: str) -> Tensor:
l = []
for t in x:
if np.isscalar(t) and not isinstance(t, str):
Expand All @@ -50,7 +51,7 @@ def _cat(x: List[Tensor], data_type: str) -> Tensor:


@window_function_register.register()
def _acosh(x: Union[Tensor, float]) -> Tensor:
def _acosh(x: Tensor | float) -> Tensor:
if isinstance(x, float):
return math.log(x + math.sqrt(x**2 - 1))
return paddle.log(x + paddle.sqrt(paddle.square(x) - 1))
Expand Down Expand Up @@ -333,7 +334,7 @@ def _cosine(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:


def get_window(
window: Union[str, Tuple[str, float]],
window: str | tuple[str, float],
win_length: int,
fftbins: bool = True,
dtype: str = 'float64',
Expand Down
17 changes: 9 additions & 8 deletions python/paddle/autograd/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from typing import Optional, Sequence, Tuple, Union
from collections.abc import Sequence

import paddle
from paddle.base import framework
Expand Down Expand Up @@ -448,10 +449,10 @@ def _multi_index(indexes, shape):


def jacobian(
ys: Union[paddle.Tensor, Tuple[paddle.Tensor, ...]],
xs: Union[paddle.Tensor, Tuple[paddle.Tensor, ...]],
batch_axis: Optional[int] = None,
) -> Union[Tuple[Tuple[Jacobian, ...], ...], Tuple[Jacobian, ...], Jacobian]:
ys: paddle.Tensor | tuple[paddle.Tensor, ...],
xs: paddle.Tensor | tuple[paddle.Tensor, ...],
batch_axis: int | None = None,
) -> tuple[tuple[Jacobian, ...], ...] | tuple[Jacobian, ...] | Jacobian:
r"""
Computes the Jacobian of the dependent variable ``ys`` versus the independent
variable ``xs``.
Expand Down Expand Up @@ -543,9 +544,9 @@ def jacobian(

def hessian(
ys: paddle.Tensor,
xs: Union[paddle.Tensor, Tuple[paddle.Tensor, ...]],
batch_axis: Optional[int] = None,
) -> Union[Tuple[Tuple[Hessian, ...], ...], Hessian]:
xs: paddle.Tensor | tuple[paddle.Tensor, ...],
batch_axis: int | None = None,
) -> tuple[tuple[Hessian, ...], ...] | Hessian:
r"""
Computes the Jacobian of the dependent variable ``ys`` versus the independent
variable ``xs``.
Expand Down
4 changes: 3 additions & 1 deletion python/paddle/autograd/backward_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Sequence
from typing import TYPE_CHECKING

import paddle
from paddle.base import core, framework
from paddle.base.backward import gradients_with_optimizer # noqa: F401

if TYPE_CHECKING:
from collections.abc import Sequence

from paddle import Tensor


Expand Down
4 changes: 3 additions & 1 deletion python/paddle/autograd/py_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, Sequence, TypeVar
from typing import TYPE_CHECKING, Any, Callable, TypeVar

from typing_extensions import Concatenate

import paddle
from paddle.base import core

if TYPE_CHECKING:
from collections.abc import Sequence

from paddle import Tensor


Expand Down
7 changes: 3 additions & 4 deletions python/paddle/base/dygraph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
TYPE_CHECKING,
Any,
Callable,
ContextManager,
Sequence,
TypeVar,
overload,
)
Expand All @@ -41,8 +39,9 @@

if TYPE_CHECKING:
from collections import OrderedDict
from collections.abc import Generator, Sequence
from contextlib import AbstractAsyncContextManager
SigureMo marked this conversation as resolved.
Show resolved Hide resolved
from types import TracebackType
from typing import Generator

from typing_extensions import Self

Expand Down Expand Up @@ -299,7 +298,7 @@ def _switch_tracer_mode_guard_(


@overload
def no_grad(func: None = ...) -> ContextManager:
def no_grad(func: None = ...) -> AbstractAsyncContextManager:
Caogration marked this conversation as resolved.
Show resolved Hide resolved
...


Expand Down