Skip to content

Commit

Permalink
Make merge_pyi not to overwrite existing type annotations. When we as…
Browse files Browse the repository at this point in the history
…sume that the existing type annotations are correct, we should totally not overwrite it as the same thing can be named in different ways depending on how you import it. But it's not a good idea to change the way how the users have written the import already.

The former change was introduced in #1644, but it seems that respecting the original code best suits what we think this tool should behave.

PiperOrigin-RevId: 678640645
  • Loading branch information
h-joo authored and copybara-github committed Sep 27, 2024
1 parent 8b1d8bb commit d5437c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions pytype/tools/merge_pyi/merge_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _merge_csts(*, py_tree, pyi_tree):
vis.store_stub_in_context(context, pyi_tree)
return vis(
context,
overwrite_existing_annotations=True,
overwrite_existing_annotations=False,
strict_posargs_matching=False,
strict_annotation_matching=True,
).transform_module(py_tree)
Expand Down Expand Up @@ -80,7 +80,7 @@ class Mode(enum.Enum):
OVERWRITE = 3


def _get_diff(a, b):
def _get_diff(a, b) -> str:
a, b = a.split("\n"), b.split("\n")
diff = difflib.Differ().compare(a, b)
return "\n".join(diff)
Expand All @@ -90,16 +90,25 @@ def merge_files(
*, py_path: str, pyi_path: str, mode: Mode, backup: Optional[str] = None
) -> bool:
"""Merges a .py and a .pyi (experimental: or a pickled pytd) file."""

with open(py_path) as f:
py_src = f.read()
_, ext = os.path.splitext(pyi_path)
if re.fullmatch(r"\.pickled(-\d+)?", ext):
with open(pyi_path, "rb") as file:
pyi_src = pytd_utils.Print(pickle_utils.DecodeAst(file.read()).ast)
else:
with open(pyi_path) as f:
pyi_src = f.read()
return merge_files_src(py_path, pyi_src, mode, backup)


def merge_files_src(
py_path: str,
pyi_src: str,
mode: Mode,
backup: Optional[str] = None,
) -> bool:
"""Merges annotations from pyi_src content into the .py file py_path."""
with open(py_path) as f:
py_src = f.read()
annotated_src = merge_sources(py=py_src, pyi=pyi_src)
changed = annotated_src != py_src
if mode == Mode.PRINT:
Expand Down
2 changes: 1 addition & 1 deletion pytype/tools/merge_pyi/test_data/defaults.pep484.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def f6(x:e6=int) -> r6:
pass

# static analysis would give error here
def f7(x:e7=int) -> r7:
def f7(x : int=int):
pass

def f8(x:e8={1:2}) -> r8:
Expand Down
4 changes: 2 additions & 2 deletions pytype/tools/merge_pyi/test_data/partial.pep484.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def f1(a: e1, b: e2) -> r1:
def f1(a, b : int):
pass

def f2(a: e1, b: e2) -> r2:
def f2(a, b) -> int:
pass

def f3(a) -> r3:
Expand Down

0 comments on commit d5437c0

Please sign in to comment.