14
14
from typing import (
15
15
Any ,
16
16
Collection ,
17
- Dict ,
18
17
Generator ,
19
18
Iterator ,
20
- List ,
21
19
MutableMapping ,
22
20
Optional ,
23
21
Pattern ,
24
22
Sequence ,
25
- Set ,
26
23
Sized ,
27
- Tuple ,
28
24
Union ,
29
25
)
30
26
@@ -176,7 +172,7 @@ def read_pyproject_toml(
176
172
"line-ranges" , "Cannot use line-ranges in the pyproject.toml file."
177
173
)
178
174
179
- default_map : Dict [str , Any ] = {}
175
+ default_map : dict [str , Any ] = {}
180
176
if ctx .default_map :
181
177
default_map .update (ctx .default_map )
182
178
default_map .update (config )
@@ -186,9 +182,9 @@ def read_pyproject_toml(
186
182
187
183
188
184
def spellcheck_pyproject_toml_keys (
189
- ctx : click .Context , config_keys : List [str ], config_file_path : str
185
+ ctx : click .Context , config_keys : list [str ], config_file_path : str
190
186
) -> None :
191
- invalid_keys : List [str ] = []
187
+ invalid_keys : list [str ] = []
192
188
available_config_options = {param .name for param in ctx .command .params }
193
189
for key in config_keys :
194
190
if key not in available_config_options :
@@ -202,8 +198,8 @@ def spellcheck_pyproject_toml_keys(
202
198
203
199
204
200
def target_version_option_callback (
205
- c : click .Context , p : Union [click .Option , click .Parameter ], v : Tuple [str , ...]
206
- ) -> List [TargetVersion ]:
201
+ c : click .Context , p : Union [click .Option , click .Parameter ], v : tuple [str , ...]
202
+ ) -> list [TargetVersion ]:
207
203
"""Compute the target versions from a --target-version flag.
208
204
209
205
This is its own function because mypy couldn't infer the type correctly
@@ -213,8 +209,8 @@ def target_version_option_callback(
213
209
214
210
215
211
def enable_unstable_feature_callback (
216
- c : click .Context , p : Union [click .Option , click .Parameter ], v : Tuple [str , ...]
217
- ) -> List [Preview ]:
212
+ c : click .Context , p : Union [click .Option , click .Parameter ], v : tuple [str , ...]
213
+ ) -> list [Preview ]:
218
214
"""Compute the features from an --enable-unstable-feature flag."""
219
215
return [Preview [val ] for val in v ]
220
216
@@ -519,7 +515,7 @@ def main( # noqa: C901
519
515
ctx : click .Context ,
520
516
code : Optional [str ],
521
517
line_length : int ,
522
- target_version : List [TargetVersion ],
518
+ target_version : list [TargetVersion ],
523
519
check : bool ,
524
520
diff : bool ,
525
521
line_ranges : Sequence [str ],
@@ -533,7 +529,7 @@ def main( # noqa: C901
533
529
skip_magic_trailing_comma : bool ,
534
530
preview : bool ,
535
531
unstable : bool ,
536
- enable_unstable_feature : List [Preview ],
532
+ enable_unstable_feature : list [Preview ],
537
533
quiet : bool ,
538
534
verbose : bool ,
539
535
required_version : Optional [str ],
@@ -543,7 +539,7 @@ def main( # noqa: C901
543
539
force_exclude : Optional [Pattern [str ]],
544
540
stdin_filename : Optional [str ],
545
541
workers : Optional [int ],
546
- src : Tuple [str , ...],
542
+ src : tuple [str , ...],
547
543
config : Optional [str ],
548
544
) -> None :
549
545
"""The uncompromising code formatter."""
@@ -643,7 +639,7 @@ def main( # noqa: C901
643
639
enabled_features = set (enable_unstable_feature ),
644
640
)
645
641
646
- lines : List [ Tuple [int , int ]] = []
642
+ lines : list [ tuple [int , int ]] = []
647
643
if line_ranges :
648
644
if ipynb :
649
645
err ("Cannot use --line-ranges with ipynb files." )
@@ -733,7 +729,7 @@ def main( # noqa: C901
733
729
def get_sources (
734
730
* ,
735
731
root : Path ,
736
- src : Tuple [str , ...],
732
+ src : tuple [str , ...],
737
733
quiet : bool ,
738
734
verbose : bool ,
739
735
include : Pattern [str ],
@@ -742,14 +738,14 @@ def get_sources(
742
738
force_exclude : Optional [Pattern [str ]],
743
739
report : "Report" ,
744
740
stdin_filename : Optional [str ],
745
- ) -> Set [Path ]:
741
+ ) -> set [Path ]:
746
742
"""Compute the set of files to be formatted."""
747
- sources : Set [Path ] = set ()
743
+ sources : set [Path ] = set ()
748
744
749
745
assert root .is_absolute (), f"INTERNAL ERROR: `root` must be absolute but is { root } "
750
746
using_default_exclude = exclude is None
751
747
exclude = re_compile_maybe_verbose (DEFAULT_EXCLUDES ) if exclude is None else exclude
752
- gitignore : Optional [Dict [Path , PathSpec ]] = None
748
+ gitignore : Optional [dict [Path , PathSpec ]] = None
753
749
root_gitignore = get_gitignore (root )
754
750
755
751
for s in src :
@@ -841,7 +837,7 @@ def reformat_code(
841
837
mode : Mode ,
842
838
report : Report ,
843
839
* ,
844
- lines : Collection [Tuple [int , int ]] = (),
840
+ lines : Collection [tuple [int , int ]] = (),
845
841
) -> None :
846
842
"""
847
843
Reformat and print out `content` without spawning child processes.
@@ -874,7 +870,7 @@ def reformat_one(
874
870
mode : Mode ,
875
871
report : "Report" ,
876
872
* ,
877
- lines : Collection [Tuple [int , int ]] = (),
873
+ lines : Collection [tuple [int , int ]] = (),
878
874
) -> None :
879
875
"""Reformat a single file under `src` without spawning child processes.
880
876
@@ -930,7 +926,7 @@ def format_file_in_place(
930
926
write_back : WriteBack = WriteBack .NO ,
931
927
lock : Any = None , # multiprocessing.Manager().Lock() is some crazy proxy
932
928
* ,
933
- lines : Collection [Tuple [int , int ]] = (),
929
+ lines : Collection [tuple [int , int ]] = (),
934
930
) -> bool :
935
931
"""Format file under `src` path. Return True if changed.
936
932
@@ -997,7 +993,7 @@ def format_stdin_to_stdout(
997
993
content : Optional [str ] = None ,
998
994
write_back : WriteBack = WriteBack .NO ,
999
995
mode : Mode ,
1000
- lines : Collection [Tuple [int , int ]] = (),
996
+ lines : Collection [tuple [int , int ]] = (),
1001
997
) -> bool :
1002
998
"""Format file on stdin. Return True if changed.
1003
999
@@ -1048,7 +1044,7 @@ def check_stability_and_equivalence(
1048
1044
dst_contents : str ,
1049
1045
* ,
1050
1046
mode : Mode ,
1051
- lines : Collection [Tuple [int , int ]] = (),
1047
+ lines : Collection [tuple [int , int ]] = (),
1052
1048
) -> None :
1053
1049
"""Perform stability and equivalence checks.
1054
1050
@@ -1065,7 +1061,7 @@ def format_file_contents(
1065
1061
* ,
1066
1062
fast : bool ,
1067
1063
mode : Mode ,
1068
- lines : Collection [Tuple [int , int ]] = (),
1064
+ lines : Collection [tuple [int , int ]] = (),
1069
1065
) -> FileContent :
1070
1066
"""Reformat contents of a file and return new contents.
1071
1067
@@ -1196,7 +1192,7 @@ def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileCon
1196
1192
1197
1193
1198
1194
def format_str (
1199
- src_contents : str , * , mode : Mode , lines : Collection [Tuple [int , int ]] = ()
1195
+ src_contents : str , * , mode : Mode , lines : Collection [tuple [int , int ]] = ()
1200
1196
) -> str :
1201
1197
"""Reformat a string and return new contents.
1202
1198
@@ -1243,10 +1239,10 @@ def f(
1243
1239
1244
1240
1245
1241
def _format_str_once (
1246
- src_contents : str , * , mode : Mode , lines : Collection [Tuple [int , int ]] = ()
1242
+ src_contents : str , * , mode : Mode , lines : Collection [tuple [int , int ]] = ()
1247
1243
) -> str :
1248
1244
src_node = lib2to3_parse (src_contents .lstrip (), mode .target_versions )
1249
- dst_blocks : List [LinesBlock ] = []
1245
+ dst_blocks : list [LinesBlock ] = []
1250
1246
if mode .target_versions :
1251
1247
versions = mode .target_versions
1252
1248
else :
@@ -1296,7 +1292,7 @@ def _format_str_once(
1296
1292
return "" .join (dst_contents )
1297
1293
1298
1294
1299
- def decode_bytes (src : bytes ) -> Tuple [FileContent , Encoding , NewLine ]:
1295
+ def decode_bytes (src : bytes ) -> tuple [FileContent , Encoding , NewLine ]:
1300
1296
"""Return a tuple of (decoded_contents, encoding, newline).
1301
1297
1302
1298
`newline` is either CRLF or LF but `decoded_contents` is decoded with
@@ -1314,8 +1310,8 @@ def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
1314
1310
1315
1311
1316
1312
def get_features_used ( # noqa: C901
1317
- node : Node , * , future_imports : Optional [Set [str ]] = None
1318
- ) -> Set [Feature ]:
1313
+ node : Node , * , future_imports : Optional [set [str ]] = None
1314
+ ) -> set [Feature ]:
1319
1315
"""Return a set of (relatively) new Python features used in this file.
1320
1316
1321
1317
Currently looking for:
@@ -1333,7 +1329,7 @@ def get_features_used( # noqa: C901
1333
1329
- except* clause;
1334
1330
- variadic generics;
1335
1331
"""
1336
- features : Set [Feature ] = set ()
1332
+ features : set [Feature ] = set ()
1337
1333
if future_imports :
1338
1334
features |= {
1339
1335
FUTURE_FLAG_TO_FEATURE [future_import ]
@@ -1471,20 +1467,20 @@ def _contains_asexpr(node: Union[Node, Leaf]) -> bool:
1471
1467
1472
1468
1473
1469
def detect_target_versions (
1474
- node : Node , * , future_imports : Optional [Set [str ]] = None
1475
- ) -> Set [TargetVersion ]:
1470
+ node : Node , * , future_imports : Optional [set [str ]] = None
1471
+ ) -> set [TargetVersion ]:
1476
1472
"""Detect the version to target based on the nodes used."""
1477
1473
features = get_features_used (node , future_imports = future_imports )
1478
1474
return {
1479
1475
version for version in TargetVersion if features <= VERSION_TO_FEATURES [version ]
1480
1476
}
1481
1477
1482
1478
1483
- def get_future_imports (node : Node ) -> Set [str ]:
1479
+ def get_future_imports (node : Node ) -> set [str ]:
1484
1480
"""Return a set of __future__ imports in the file."""
1485
- imports : Set [str ] = set ()
1481
+ imports : set [str ] = set ()
1486
1482
1487
- def get_imports_from_children (children : List [LN ]) -> Generator [str , None , None ]:
1483
+ def get_imports_from_children (children : list [LN ]) -> Generator [str , None , None ]:
1488
1484
for child in children :
1489
1485
if isinstance (child , Leaf ):
1490
1486
if child .type == token .NAME :
@@ -1571,7 +1567,7 @@ def assert_equivalent(src: str, dst: str) -> None:
1571
1567
1572
1568
1573
1569
def assert_stable (
1574
- src : str , dst : str , mode : Mode , * , lines : Collection [Tuple [int , int ]] = ()
1570
+ src : str , dst : str , mode : Mode , * , lines : Collection [tuple [int , int ]] = ()
1575
1571
) -> None :
1576
1572
"""Raise AssertionError if `dst` reformats differently the second time."""
1577
1573
if lines :
0 commit comments