openrewrite-migrate-python 0.35.0__py3-none-any.whl → 0.37.0__py3-none-any.whl

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.
@@ -13,7 +13,7 @@ from typing import Any, List, Tuple
13
13
  from rewrite import Cursor
14
14
  from rewrite.markers import Marker
15
15
  from rewrite.java.markers import TrailingComma
16
- from rewrite.java.support_types import Space
16
+ from rewrite.java.support_types import Comment, Space
17
17
  from rewrite.java.tree import Block, ClassDeclaration, If, J, MethodDeclaration
18
18
  from rewrite.python.tree import CompilationUnit
19
19
  from rewrite.python.visitor import PythonVisitor
@@ -34,9 +34,14 @@ class _CommentFinder(PythonVisitor[List[Space]]):
34
34
 
35
35
  def has_comments(tree: J) -> bool:
36
36
  """Whether any trivia within ``tree``, its own prefix included, holds a comment."""
37
+ return bool(comments_in(tree))
38
+
39
+
40
+ def comments_in(tree: J) -> List[Comment]:
41
+ """Every comment in the trivia within ``tree``, its own prefix included."""
37
42
  found: List[Space] = []
38
43
  _CommentFinder().visit(tree, found)
39
- return bool(found)
44
+ return [comment for space in found for comment in space.comments]
40
45
 
41
46
 
42
47
  def split_line(space: Space) -> Tuple[Space, Space] | None:
@@ -3,7 +3,7 @@
3
3
  from typing import Any, Dict
4
4
 
5
5
  from rewrite import ExecutionContext, Preconditions, TreeVisitor
6
- from rewrite.java.tree import Import, J
6
+ from rewrite.java.tree import If, Import, J, Try
7
7
  from rewrite.python.binding_utils import dotted_path
8
8
  from rewrite.python.import_utils import get_name_string, get_qualid_name
9
9
  from rewrite.python.preconditions import uses_import
@@ -14,22 +14,34 @@ from ._markers import mark_deprecated
14
14
 
15
15
 
16
16
  def find_module_imports(
17
- module: str, message: str, detail: str | None = None
17
+ module: str, message: str, detail: str | None = None, guarded_message: str | None = None,
18
+ skip_guarded: bool = False,
18
19
  ) -> TreeVisitor[Any, ExecutionContext]:
19
20
  """Marks each import of ``module`` or a submodule of it, wherever in the file it stands,
20
21
  ``from package import module`` included."""
21
- return find_imports_of_modules({module: message}, detail)
22
+ return find_imports_of_modules({module: message}, detail, guarded_message, skip_guarded)
22
23
 
23
24
 
24
25
  def find_imports_of_modules(
25
- messages: Dict[str, str], detail: str | None = None
26
+ messages: Dict[str, str], detail: str | None = None, guarded_message: str | None = None,
27
+ skip_guarded: bool = False,
26
28
  ) -> TreeVisitor[Any, ExecutionContext]:
27
- """Marks each import of a module in ``messages``, or a submodule of it, with that module's message."""
29
+ """Marks each import of a module in ``messages``, or a submodule of it, with that module's message.
30
+
31
+ With ``guarded_message``, an import under an ``if`` or a ``try``, such as a fallback for an older
32
+ Python chosen by ``sys.version_info``, is marked with that message instead. With ``skip_guarded``,
33
+ such an import is not marked at all."""
28
34
 
29
35
  def module_of(imported: str) -> str | None:
30
36
  return next((m for m in messages if imported == m or imported.startswith(m + ".")), None)
31
37
 
32
38
  class Visitor(PythonVisitor[ExecutionContext]):
39
+ def guarded(self) -> bool:
40
+ return self.cursor.first_enclosing(If) is not None or self.cursor.first_enclosing(Try) is not None
41
+
42
+ def message(self, module: str) -> str:
43
+ return guarded_message if guarded_message is not None and self.guarded() else messages[module]
44
+
33
45
  def visit_multi_import(self, multi: MultiImport, p: ExecutionContext) -> J:
34
46
  mi = super().visit_multi_import(multi, p)
35
47
  if not isinstance(mi, MultiImport):
@@ -44,7 +56,9 @@ def find_imports_of_modules(
44
56
  module_of(f"{package}.{get_qualid_name(imp.qualid)}") for imp in mi.names)), None)
45
57
  else:
46
58
  found = next(filter(None, (module_of(get_qualid_name(imp.qualid)) for imp in mi.names)), None)
47
- return mark_deprecated(mi, messages[found], detail) if found else mi
59
+ if not found or (skip_guarded and self.guarded()):
60
+ return mi
61
+ return mark_deprecated(mi, self.message(found), detail)
48
62
 
49
63
  def visit_import(self, import_: Import, p: ExecutionContext) -> J:
50
64
  i = super().visit_import(import_, p)
@@ -52,7 +66,9 @@ def find_imports_of_modules(
52
66
  if not isinstance(i, Import) or self.cursor.first_enclosing(MultiImport) is not None:
53
67
  return i
54
68
  found = module_of(get_qualid_name(i.qualid))
55
- return mark_deprecated(i, messages[found], detail) if found else i
69
+ if not found or (skip_guarded and self.guarded()):
70
+ return i
71
+ return mark_deprecated(i, self.message(found), detail)
56
72
 
57
73
  gates = [uses_import(module) for module in messages]
58
74
  return Preconditions.check(gates[0] if len(gates) == 1 else Preconditions.or_(*gates), Visitor())