reorder-python-imports 3.16.0__tar.gz → 3.17.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: reorder_python_imports
3
- Version: 3.16.0
3
+ Version: 3.17.0
4
4
  Summary: Tool for reordering python imports
5
5
  Home-page: https://github.com/asottile/reorder-python-imports
6
6
  Author: Anthony Sottile
@@ -10,10 +10,10 @@ Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Programming Language :: Python :: 3 :: Only
11
11
  Classifier: Programming Language :: Python :: Implementation :: CPython
12
12
  Classifier: Programming Language :: Python :: Implementation :: PyPy
13
- Requires-Python: >=3.9
13
+ Requires-Python: >=3.10
14
14
  Description-Content-Type: text/markdown
15
15
  License-File: LICENSE
16
- Requires-Dist: classify-imports>=4.1
16
+ Requires-Dist: classify-imports>=4.5
17
17
 
18
18
  [![build status](https://github.com/asottile/reorder-python-imports/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/reorder-python-imports/actions/workflows/main.yml)
19
19
  [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/reorder-python-imports/main.svg)](https://results.pre-commit.ci/latest/github/asottile/reorder-python-imports/main)
@@ -61,7 +61,7 @@ Sample `.pre-commit-config.yaml`
61
61
 
62
62
  ```yaml
63
63
  - repo: https://github.com/asottile/reorder-python-imports
64
- rev: v3.16.0
64
+ rev: v3.17.0
65
65
  hooks:
66
66
  - id: reorder-python-imports
67
67
  ```
@@ -44,7 +44,7 @@ Sample `.pre-commit-config.yaml`
44
44
 
45
45
  ```yaml
46
46
  - repo: https://github.com/asottile/reorder-python-imports
47
- rev: v3.16.0
47
+ rev: v3.17.0
48
48
  hooks:
49
49
  - id: reorder-python-imports
50
50
  ```
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: reorder_python_imports
3
- Version: 3.16.0
3
+ Version: 3.17.0
4
4
  Summary: Tool for reordering python imports
5
5
  Home-page: https://github.com/asottile/reorder-python-imports
6
6
  Author: Anthony Sottile
@@ -10,10 +10,10 @@ Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Programming Language :: Python :: 3 :: Only
11
11
  Classifier: Programming Language :: Python :: Implementation :: CPython
12
12
  Classifier: Programming Language :: Python :: Implementation :: PyPy
13
- Requires-Python: >=3.9
13
+ Requires-Python: >=3.10
14
14
  Description-Content-Type: text/markdown
15
15
  License-File: LICENSE
16
- Requires-Dist: classify-imports>=4.1
16
+ Requires-Dist: classify-imports>=4.5
17
17
 
18
18
  [![build status](https://github.com/asottile/reorder-python-imports/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/reorder-python-imports/actions/workflows/main.yml)
19
19
  [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/reorder-python-imports/main.svg)](https://results.pre-commit.ci/latest/github/asottile/reorder-python-imports/main)
@@ -61,7 +61,7 @@ Sample `.pre-commit-config.yaml`
61
61
 
62
62
  ```yaml
63
63
  - repo: https://github.com/asottile/reorder-python-imports
64
- rev: v3.16.0
64
+ rev: v3.17.0
65
65
  hooks:
66
66
  - id: reorder-python-imports
67
67
  ```
@@ -0,0 +1 @@
1
+ classify-imports>=4.5
@@ -14,11 +14,22 @@ from collections.abc import Sequence
14
14
  from typing import NamedTuple
15
15
 
16
16
  from classify_imports import Import
17
+ from classify_imports import import_from_replace
17
18
  from classify_imports import import_obj_from_str
19
+ from classify_imports import import_replace
18
20
  from classify_imports import ImportFrom
19
21
  from classify_imports import Settings
20
22
  from classify_imports import sort
21
23
 
24
+
25
+ if sys.version_info >= (3, 15): # pragma: >=3.15 cover
26
+ def _preserve_lazy(o: ast.ImportFrom) -> dict[str, int]:
27
+ return {'is_lazy': o.is_lazy}
28
+ else: # pragma: <3.15 cover
29
+ def _preserve_lazy(o: ast.ImportFrom) -> dict[str, int]:
30
+ return {}
31
+
32
+
22
33
  CodeType = enum.Enum('CodeType', 'PRE_IMPORT_CODE IMPORT NON_CODE CODE')
23
34
 
24
35
  Tok = enum.Enum('Tok', 'IMPORT STRING NEWLINE ERROR')
@@ -34,7 +45,7 @@ SINGLE_1 = r"'[^'\\]*(?:\\.[^'\\]*)*'"
34
45
  # END GENERATED
35
46
 
36
47
  WS = r'[ \f\t]+'
37
- IMPORT = fr'(?:from|import)(?={WS})'
48
+ IMPORT = fr'(?:lazy{WS})?(?:from|import)(?={WS})'
38
49
  EMPTY = fr'[ \f\t]*(?=\n|{COMMENT})'
39
50
  OP = '[,.*]'
40
51
  ESCAPED_NL = r'\\\n'
@@ -182,10 +193,11 @@ def replace_imports(
182
193
 
183
194
  for s, import_obj in imports:
184
195
  if isinstance(import_obj, Import):
185
- mod, asname = import_obj.key
196
+ mod, asname, _ = import_obj.key_with_lazy
186
197
  if asname:
187
198
  if mod in to_replace.mods:
188
- node_i = ast.Import(
199
+ node_i = import_replace(
200
+ import_obj.node,
189
201
  names=[ast.alias(to_replace.mods[mod], asname)],
190
202
  )
191
203
  obj_i = Import(node_i)
@@ -195,7 +207,8 @@ def replace_imports(
195
207
  if mod_name in to_replace.mods:
196
208
  new_mod = to_replace.mods[mod_name]
197
209
  new_mod_s = f'{new_mod}{mod[len(mod_name):]}'
198
- node_i = ast.Import(
210
+ node_i = import_replace(
211
+ import_obj.node,
199
212
  names=[ast.alias(new_mod_s, asname)],
200
213
  )
201
214
  obj_i = Import(node_i)
@@ -206,14 +219,14 @@ def replace_imports(
206
219
  else:
207
220
  ret.append((s, import_obj))
208
221
  else:
209
- mod, symbol, asname = import_obj.key
222
+ mod, symbol, asname, _ = import_obj.key_with_lazy
210
223
  mod_symbol = f'{mod}.{symbol}'
211
224
 
212
225
  # from a.b.c import d => from e.f.g import d
213
226
  if (mod, symbol) in to_replace.exact:
214
- node = ast.ImportFrom(
227
+ node = import_from_replace(
228
+ import_obj.node,
215
229
  module=to_replace.exact[mod, symbol],
216
- names=import_obj.node.names,
217
230
  level=0,
218
231
  )
219
232
  obj = ImportFrom(node)
@@ -228,7 +241,8 @@ def replace_imports(
228
241
  new_mod = to_replace.mods[mod_symbol]
229
242
  new_mod, dot, new_sym = new_mod.rpartition('.')
230
243
  if new_mod:
231
- node = ast.ImportFrom(
244
+ node = import_from_replace(
245
+ import_obj.node,
232
246
  module=new_mod,
233
247
  names=[ast.alias(new_sym, asname)],
234
248
  level=0,
@@ -236,16 +250,19 @@ def replace_imports(
236
250
  obj = ImportFrom(node)
237
251
  ret.append((str(obj), obj))
238
252
  elif not dot:
239
- node_i = ast.Import(names=[ast.alias(new_sym, asname)])
253
+ node_i = ast.Import(
254
+ names=[ast.alias(new_sym, asname)],
255
+ **_preserve_lazy(import_obj.node),
256
+ )
240
257
  obj_i = Import(node_i)
241
258
  ret.append((str(obj_i), obj_i))
242
259
  else:
243
260
  ret.append((s, import_obj))
244
261
  # from a.b.c import d => from e import d
245
262
  elif mod in to_replace.mods:
246
- node = ast.ImportFrom(
263
+ node = import_from_replace(
264
+ import_obj.node,
247
265
  module=to_replace.mods[mod],
248
- names=import_obj.node.names,
249
266
  level=0,
250
267
  )
251
268
  obj = ImportFrom(node)
@@ -254,9 +271,9 @@ def replace_imports(
254
271
  for mod_name in _module_to_base_modules(mod):
255
272
  if mod_name in to_replace.mods:
256
273
  new_mod = to_replace.mods[mod_name]
257
- node = ast.ImportFrom(
274
+ node = import_from_replace(
275
+ import_obj.node,
258
276
  module=f'{new_mod}{mod[len(mod_name):]}',
259
- names=import_obj.node.names,
260
277
  level=0,
261
278
  )
262
279
  obj = ImportFrom(node)
@@ -281,18 +298,18 @@ def _module_to_base_modules(s: str) -> Generator[str]:
281
298
  def remove_duplicated_imports(
282
299
  imports: list[tuple[str, Import | ImportFrom]],
283
300
  *,
284
- to_remove: set[tuple[str, ...]],
301
+ to_remove: set[tuple[object, ...]],
285
302
  ) -> list[tuple[str, Import | ImportFrom]]:
286
303
  seen = set(to_remove)
287
304
  seen_module_names: set[str] = set()
288
305
  without_exact_duplicates = []
289
306
 
290
307
  for s, import_obj in imports:
291
- if import_obj.key not in seen:
292
- seen.add(import_obj.key)
308
+ if import_obj.key_with_lazy not in seen:
309
+ seen.add(import_obj.key_with_lazy)
293
310
  if (
294
311
  isinstance(import_obj, Import) and
295
- not import_obj.key.asname
312
+ not import_obj.key_with_lazy.asname
296
313
  ):
297
314
  seen_module_names.update(
298
315
  _module_to_base_modules(import_obj.module),
@@ -303,8 +320,8 @@ def remove_duplicated_imports(
303
320
  for s, import_obj in without_exact_duplicates:
304
321
  if (
305
322
  isinstance(import_obj, Import) and
306
- not import_obj.key.asname and
307
- import_obj.key.module in seen_module_names
323
+ not import_obj.key_with_lazy.asname and
324
+ import_obj.key_with_lazy.module in seen_module_names
308
325
  ):
309
326
  continue
310
327
  ret.append((s, import_obj))
@@ -339,7 +356,7 @@ def fix_file_contents(
339
356
  contents: str,
340
357
  *,
341
358
  to_add: tuple[str, ...] = (),
342
- to_remove: set[tuple[str, ...]],
359
+ to_remove: set[tuple[object, ...]],
343
360
  to_replace: Replacements,
344
361
  settings: Settings = Settings(),
345
362
  ) -> str:
@@ -361,7 +378,7 @@ def _fix_file(
361
378
  filename: str,
362
379
  args: argparse.Namespace,
363
380
  *,
364
- to_remove: set[tuple[str, ...]],
381
+ to_remove: set[tuple[object, ...]],
365
382
  to_replace: Replacements,
366
383
  settings: Settings = Settings(),
367
384
  ) -> int:
@@ -858,11 +875,11 @@ def main(argv: Sequence[str] | None = None) -> int:
858
875
  args = parser.parse_args(argv)
859
876
 
860
877
  to_remove = {
861
- obj.key
878
+ obj.key_with_lazy
862
879
  for s in args.remove_import
863
880
  for obj in import_obj_from_str(s).split()
864
881
  } | {
865
- import_obj_from_str(s).key
882
+ import_obj_from_str(s).key_with_lazy
866
883
  for k, v in REMOVALS.items()
867
884
  if args.min_version >= k
868
885
  for s in v
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = reorder_python_imports
3
- version = 3.16.0
3
+ version = 3.17.0
4
4
  description = Tool for reordering python imports
5
5
  long_description = file: README.md
6
6
  long_description_content_type = text/markdown
@@ -18,8 +18,8 @@ classifiers =
18
18
  [options]
19
19
  py_modules = reorder_python_imports
20
20
  install_requires =
21
- classify-imports>=4.1
22
- python_requires = >=3.9
21
+ classify-imports>=4.5
22
+ python_requires = >=3.10
23
23
 
24
24
  [options.entry_points]
25
25
  console_scripts =
@@ -1 +0,0 @@
1
- classify-imports>=4.1