Skip to content

Commit

Permalink
Renamed types and vars
Browse files Browse the repository at this point in the history
  • Loading branch information
surge119 committed Jul 24, 2024
1 parent f30e925 commit 8bb6ecb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/fixit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Config,
FileContent,
LintViolation,
LoggerHook,
MetricsHook,
Options,
OutputFormat,
Result,
Expand Down Expand Up @@ -100,7 +100,7 @@ def fixit_bytes(
*,
config: Config,
autofix: bool = False,
logger_hook: Optional[LoggerHook] = None,
logger_hook: Optional[MetricsHook] = None,
) -> Generator[Result, bool, Optional[FileContent]]:
"""
Lint raw bytes content representing a single path, using the given configuration.
Expand Down Expand Up @@ -155,7 +155,7 @@ def fixit_stdin(
*,
autofix: bool = False,
options: Optional[Options] = None,
logger_hook: Optional[LoggerHook] = None,
logger_hook: Optional[MetricsHook] = None,
) -> Generator[Result, bool, None]:
"""
Wrapper around :func:`fixit_bytes` for formatting content from STDIN.
Expand Down Expand Up @@ -188,7 +188,7 @@ def fixit_file(
*,
autofix: bool = False,
options: Optional[Options] = None,
logger_hook: Optional[LoggerHook] = None,
logger_hook: Optional[MetricsHook] = None,
) -> Generator[Result, bool, None]:
"""
Lint a single file on disk, detecting and generating appropriate configuration.
Expand Down Expand Up @@ -224,7 +224,7 @@ def _fixit_file_wrapper(
*,
autofix: bool = False,
options: Optional[Options] = None,
logger_hook: Optional[LoggerHook] = None,
logger_hook: Optional[MetricsHook] = None,
) -> List[Result]:
"""
Wrapper because generators can't be pickled or used directly via multiprocessing
Expand All @@ -241,7 +241,7 @@ def fixit_paths(
autofix: bool = False,
options: Optional[Options] = None,
parallel: bool = True,
logger_hook: Optional[LoggerHook] = None,
logger_hook: Optional[MetricsHook] = None,
) -> Generator[Result, bool, None]:
"""
Lint multiple files or directories, recursively expanding each path.
Expand Down
16 changes: 8 additions & 8 deletions src/fixit/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Config,
FileContent,
LintViolation,
LoggableMetric,
LoggerHook,
Metrics,
MetricsHook,
NodeReplacement,
)
from .rule import LintRule
Expand Down Expand Up @@ -53,17 +53,17 @@ def __init__(self, path: Path, source: FileContent) -> None:
self.path = path
self.source = source
self.module: Module = parse_module(source)
self.log_event: LoggableMetric = defaultdict(lambda: 0)
self.metrics: Metrics = defaultdict(lambda: 0)

def collect_violations(
self,
rules: Collection[LintRule],
config: Config,
logger_hook: Optional[LoggerHook] = None,
metrics_hook: Optional[MetricsHook] = None,
) -> Generator[LintViolation, None, int]:
"""Run multiple `LintRule`s and yield any lint violations.
The optional `logger_hook` parameter will be called (if provided) after all
The optional `metrics_hook` parameter will be called (if provided) after all
lint rules have finished running, passing in a dictionary of
``RuleName.visit_function_name`` -> ``duration in microseconds``.
"""
Expand All @@ -76,7 +76,7 @@ def visit_hook(name: str) -> Iterator[None]:
finally:
duration_us = int(1000 * 1000 * (time.perf_counter() - start))
LOG.debug(f"PERF: {name} took {duration_us} µs")
self.log_event[name] += duration_us
self.metrics[name] += duration_us

metadata_cache: Mapping[ProviderT, object] = {}
needs_repo_manager: Set[ProviderT] = set()
Expand Down Expand Up @@ -111,8 +111,8 @@ def visit_hook(name: str) -> Iterator[None]:
violation = replace(violation, diff=diff)

yield violation
if logger_hook:
logger_hook(self.log_event)
if metrics_hook:
metrics_hook(self.metrics)

return count

Expand Down
4 changes: 2 additions & 2 deletions src/fixit/ftypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

NodeReplacement = Union[CSTNodeT, FlattenSentinel[CSTNodeT], RemovalSentinel]

LoggableMetric = Dict[str, Any]
LoggerHook = Callable[[LoggableMetric], None]
Metrics = Dict[str, Any]
MetricsHook = Callable[[Metrics], None]

VisitorMethod = Callable[[CSTNode], None]
VisitHook = Callable[[str], ContextManager[None]]
Expand Down
8 changes: 4 additions & 4 deletions src/fixit/tests/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def test_timing(self) -> None:
rule = NoopRule()
for _ in self.runner.collect_violations([rule], Config()):
pass # exhaust the generator
self.assertIn("NoopRule.visit_Module", self.runner.log_event)
self.assertIn("NoopRule.leave_Module", self.runner.log_event)
self.assertGreaterEqual(self.runner.log_event["NoopRule.visit_Module"], 0)
self.assertIn("NoopRule.visit_Module", self.runner.metrics)
self.assertIn("NoopRule.leave_Module", self.runner.metrics)
self.assertGreaterEqual(self.runner.metrics["NoopRule.visit_Module"], 0)

def test_timing_hook(self) -> None:
rule = NoopRule()
hook = MagicMock()
for i, _ in enumerate(
self.runner.collect_violations([rule], Config(), logger_hook=hook)
self.runner.collect_violations([rule], Config(), metrics_hook=hook)
):
if i <= 1:
# only called at the end
Expand Down

0 comments on commit 8bb6ecb

Please sign in to comment.