Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't reset context.scratch between files #1151

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libcst/codemod/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import sys
import time
import traceback
from copy import deepcopy
from dataclasses import dataclass, replace
from multiprocessing import cpu_count, Pool
from pathlib import Path
Expand Down Expand Up @@ -214,6 +215,7 @@ def _execute_transform( # noqa: C901
transformer: Codemod,
filename: str,
config: ExecutionConfig,
scratch: Dict[str, object],
) -> ExecutionResult:
for pattern in config.blacklist_patterns:
if re.fullmatch(pattern, filename):
Expand Down Expand Up @@ -251,7 +253,7 @@ def _execute_transform( # noqa: C901
transformer.context = replace(
transformer.context,
filename=filename,
scratch={},
scratch=deepcopy(scratch),
)

# determine the module and package name for this file
Expand Down Expand Up @@ -634,6 +636,7 @@ def parallel_exec_transform_with_prettyprint( # noqa: C901
"transformer": transform,
"filename": filename,
"config": config,
"scratch": transform.context.scratch,
}
for filename in files
]
Expand Down
9 changes: 8 additions & 1 deletion libcst/tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Generator
from typing import Dict, Generator
from unittest import TestCase

from libcst import BaseExpression, Call, matchers as m, Name
Expand All @@ -16,7 +16,14 @@


class PrintToPPrintCommand(VisitorBasedCodemodCommand):
def __init__(self, context: CodemodContext, **kwargs: Dict[str, object]) -> None:
super().__init__(context, **kwargs)
self.context.scratch["PPRINT_WAS_HERE"] = True

def leave_Call(self, original_node: Call, updated_node: Call) -> BaseExpression:
if not self.context.scratch["PPRINT_WAS_HERE"]:
raise AssertionError("Scratch space lost")

Check warning on line 25 in libcst/tests/test_e2e.py

View check run for this annotation

Codecov / codecov/patch

libcst/tests/test_e2e.py#L25

Added line #L25 was not covered by tests

if m.matches(updated_node, m.Call(func=m.Name("print"))):
AddImportsVisitor.add_needed_import(
self.context,
Expand Down
Loading