Skip to content

Commit ce091b1

Browse files
authored
Use pyupgrade --py39-plus to improve code (#36843)
1 parent 3e8f0fb commit ce091b1

33 files changed

+285
-308
lines changed

src/transformers/audio_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2023 The HuggingFace Inc. team and the librosa & torchaudio authors.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,7 +17,7 @@
1817
"""
1918

2019
import warnings
21-
from typing import List, Optional, Tuple, Union
20+
from typing import Optional, Union
2221

2322
import numpy as np
2423

@@ -146,7 +145,7 @@ def chroma_filter_bank(
146145
sampling_rate: int,
147146
tuning: float = 0.0,
148147
power: Optional[float] = 2.0,
149-
weighting_parameters: Optional[Tuple[float, float]] = (5.0, 2.0),
148+
weighting_parameters: Optional[tuple[float, float]] = (5.0, 2.0),
150149
start_at_c_chroma: Optional[bool] = True,
151150
):
152151
"""
@@ -592,7 +591,7 @@ def spectrogram(
592591

593592

594593
def spectrogram_batch(
595-
waveform_list: List[np.ndarray],
594+
waveform_list: list[np.ndarray],
596595
window: np.ndarray,
597596
frame_length: int,
598597
hop_length: int,
@@ -611,7 +610,7 @@ def spectrogram_batch(
611610
db_range: Optional[float] = None,
612611
remove_dc_offset: Optional[bool] = None,
613612
dtype: np.dtype = np.float32,
614-
) -> List[np.ndarray]:
613+
) -> list[np.ndarray]:
615614
"""
616615
Calculates spectrograms for a list of waveforms using the Short-Time Fourier Transform, optimized for batch processing.
617616
This function extends the capabilities of the `spectrogram` function to handle multiple waveforms efficiently by leveraging broadcasting.

src/transformers/convert_graph_to_onnx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from argparse import ArgumentParser
1717
from os import listdir, makedirs
1818
from pathlib import Path
19-
from typing import Dict, List, Optional, Tuple
19+
from typing import Optional
2020

2121
from packaging.version import Version, parse
2222

@@ -159,7 +159,7 @@ def ensure_valid_input(model, tokens, input_names):
159159
return ordered_input_names, tuple(model_args)
160160

161161

162-
def infer_shapes(nlp: Pipeline, framework: str) -> Tuple[List[str], List[str], Dict, BatchEncoding]:
162+
def infer_shapes(nlp: Pipeline, framework: str) -> tuple[list[str], list[str], dict, BatchEncoding]:
163163
"""
164164
Attempt to infer the static vs dynamic axes for each input and output tensors for a specific model
165165

src/transformers/convert_pytorch_checkpoint_to_tf2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2018 The HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");

src/transformers/convert_slow_tokenizer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2018 The HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,7 +19,6 @@
2019
"""
2120

2221
import warnings
23-
from typing import Dict, List, Tuple
2422

2523
from packaging import version
2624
from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, processors
@@ -91,7 +89,7 @@ def __init__(self, model: str):
9189
self.sp = SentencePieceProcessor()
9290
self.sp.Load(model)
9391

94-
def extract(self, vocab_scores=None) -> Tuple[Dict[str, int], List[Tuple]]:
92+
def extract(self, vocab_scores=None) -> tuple[dict[str, int], list[tuple]]:
9593
"""
9694
By default will return vocab and merges with respect to their order, by sending `vocab_scores` we're going to
9795
order the merges with respect to the piece scores instead.
@@ -105,7 +103,7 @@ def extract(self, vocab_scores=None) -> Tuple[Dict[str, int], List[Tuple]]:
105103

106104

107105
class GemmaSentencePieceExtractor(SentencePieceExtractor):
108-
def extract(self, vocab_scores=None) -> Tuple[Dict[str, int], List[Tuple]]:
106+
def extract(self, vocab_scores=None) -> tuple[dict[str, int], list[tuple]]:
109107
"""
110108
By default will return vocab and merges with respect to their order, by sending `vocab_scores` we're going to
111109
order the merges with respect to the piece scores instead.
@@ -328,7 +326,7 @@ def converted(self) -> Tokenizer:
328326

329327

330328
class GPT2Converter(Converter):
331-
def converted(self, vocab: Dict[str, int] = None, merges: List[Tuple[str, str]] = None) -> Tokenizer:
329+
def converted(self, vocab: dict[str, int] = None, merges: list[tuple[str, str]] = None) -> Tokenizer:
332330
if not vocab:
333331
vocab = self.original_tokenizer.encoder
334332
if not merges:
@@ -397,7 +395,7 @@ def converted(self) -> Tokenizer:
397395

398396

399397
class Qwen2Converter(Converter):
400-
def converted(self, vocab: Dict[str, int] = None, merges: List[Tuple[str, str]] = None) -> Tokenizer:
398+
def converted(self, vocab: dict[str, int] = None, merges: list[tuple[str, str]] = None) -> Tokenizer:
401399
if not vocab:
402400
vocab = self.original_tokenizer.encoder
403401
if not merges:

src/transformers/convert_slow_tokenizers_checkpoints_to_fast.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2018 The HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");

src/transformers/convert_tf_hub_seq_to_seq_bert_to_pytorch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2020 The HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");

src/transformers/feature_extraction_sequence_utils.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2021 The HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +15,7 @@
1615
Sequence feature extraction class for common feature extractors to preprocess sequences.
1716
"""
1817

19-
from typing import Dict, List, Optional, Union
18+
from typing import Optional, Union
2019

2120
import numpy as np
2221

@@ -54,10 +53,10 @@ def pad(
5453
self,
5554
processed_features: Union[
5655
BatchFeature,
57-
List[BatchFeature],
58-
Dict[str, BatchFeature],
59-
Dict[str, List[BatchFeature]],
60-
List[Dict[str, BatchFeature]],
56+
list[BatchFeature],
57+
dict[str, BatchFeature],
58+
dict[str, list[BatchFeature]],
59+
list[dict[str, BatchFeature]],
6160
],
6261
padding: Union[bool, str, PaddingStrategy] = True,
6362
max_length: Optional[int] = None,
@@ -226,7 +225,7 @@ def pad(
226225

227226
def _pad(
228227
self,
229-
processed_features: Union[Dict[str, np.ndarray], BatchFeature],
228+
processed_features: Union[dict[str, np.ndarray], BatchFeature],
230229
max_length: Optional[int] = None,
231230
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
232231
pad_to_multiple_of: Optional[int] = None,
@@ -298,7 +297,7 @@ def _pad(
298297

299298
def _truncate(
300299
self,
301-
processed_features: Union[Dict[str, np.ndarray], BatchFeature],
300+
processed_features: Union[dict[str, np.ndarray], BatchFeature],
302301
max_length: Optional[int] = None,
303302
pad_to_multiple_of: Optional[int] = None,
304303
truncation: Optional[bool] = None,

src/transformers/hf_argparser.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
import sys
1919
import types
2020
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError
21+
from collections.abc import Iterable
2122
from copy import copy
2223
from enum import Enum
2324
from inspect import isclass
2425
from pathlib import Path
25-
from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints
26+
from typing import Any, Callable, Literal, NewType, Optional, Union, get_type_hints
2627

2728
import yaml
2829

@@ -62,7 +63,7 @@ def make_choice_type_function(choices: list) -> Callable[[str], Any]:
6263

6364
def HfArg(
6465
*,
65-
aliases: Union[str, List[str]] = None,
66+
aliases: Union[str, list[str]] = None,
6667
help: str = None,
6768
default: Any = dataclasses.MISSING,
6869
default_factory: Callable[[], Any] = dataclasses.MISSING,
@@ -254,7 +255,7 @@ def _add_dataclass_arguments(self, dtype: DataClassType):
254255
parser = self
255256

256257
try:
257-
type_hints: Dict[str, type] = get_type_hints(dtype)
258+
type_hints: dict[str, type] = get_type_hints(dtype)
258259
except NameError:
259260
raise RuntimeError(
260261
f"Type resolution failed for {dtype}. Try declaring the class in global scope or "
@@ -288,7 +289,7 @@ def parse_args_into_dataclasses(
288289
look_for_args_file=True,
289290
args_filename=None,
290291
args_file_flag=None,
291-
) -> Tuple[DataClass, ...]:
292+
) -> tuple[DataClass, ...]:
292293
"""
293294
Parse command-line args into instances of the specified dataclass types.
294295
@@ -367,7 +368,7 @@ def parse_args_into_dataclasses(
367368

368369
return (*outputs,)
369370

370-
def parse_dict(self, args: Dict[str, Any], allow_extra_keys: bool = False) -> Tuple[DataClass, ...]:
371+
def parse_dict(self, args: dict[str, Any], allow_extra_keys: bool = False) -> tuple[DataClass, ...]:
371372
"""
372373
Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass
373374
types.
@@ -397,7 +398,7 @@ def parse_dict(self, args: Dict[str, Any], allow_extra_keys: bool = False) -> Tu
397398

398399
def parse_json_file(
399400
self, json_file: Union[str, os.PathLike], allow_extra_keys: bool = False
400-
) -> Tuple[DataClass, ...]:
401+
) -> tuple[DataClass, ...]:
401402
"""
402403
Alternative helper method that does not use `argparse` at all, instead loading a json file and populating the
403404
dataclass types.
@@ -421,7 +422,7 @@ def parse_json_file(
421422

422423
def parse_yaml_file(
423424
self, yaml_file: Union[str, os.PathLike], allow_extra_keys: bool = False
424-
) -> Tuple[DataClass, ...]:
425+
) -> tuple[DataClass, ...]:
425426
"""
426427
Alternative helper method that does not use `argparse` at all, instead loading a yaml file and populating the
427428
dataclass types.

src/transformers/hyperparameter_search.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2023-present the HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");

src/transformers/image_processing_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding=utf-8
21
# Copyright 2022 The HuggingFace Inc. team.
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,7 +13,8 @@
1413
# limitations under the License.
1514

1615
import math
17-
from typing import Dict, Iterable, Optional, Union
16+
from collections.abc import Iterable
17+
from typing import Optional, Union
1818

1919
import numpy as np
2020

@@ -116,7 +116,7 @@ def normalize(
116116
def center_crop(
117117
self,
118118
image: np.ndarray,
119-
size: Dict[str, int],
119+
size: dict[str, int],
120120
data_format: Optional[Union[str, ChannelDimension]] = None,
121121
input_data_format: Optional[Union[str, ChannelDimension]] = None,
122122
**kwargs,
@@ -207,7 +207,7 @@ def convert_to_size_dict(
207207

208208

209209
def get_size_dict(
210-
size: Union[int, Iterable[int], Dict[str, int]] = None,
210+
size: Union[int, Iterable[int], dict[str, int]] = None,
211211
max_size: Optional[int] = None,
212212
height_width_order: bool = True,
213213
default_to_square: bool = True,

0 commit comments

Comments
 (0)