From d736a367a4985552e4cdf1d1cc7e7b0d118c1723 Mon Sep 17 00:00:00 2001 From: Sachaa-Thanasius Date: Sun, 8 Sep 2024 09:47:49 +0530 Subject: [PATCH] Minor comment/docstring/test id adjustments. --- src/defer_imports/_core.py | 5 +++-- tests/test_deferred.py | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/defer_imports/_core.py b/src/defer_imports/_core.py index 7140c12..3c8d5cb 100644 --- a/src/defer_imports/_core.py +++ b/src/defer_imports/_core.py @@ -55,7 +55,7 @@ def instrument(self, mode: str = "exec") -> _tp.Any: return ast.fix_missing_locations(self.visit(to_visit)) def _visit_scope(self, node: ast.AST) -> ast.AST: - """Track Python scope changes. Used to determine if defer_imports.until_use usage is valid.""" + """Track Python scope changes. Used to determine if defer_imports.until_use usage is global.""" self.scope_depth += 1 try: @@ -199,8 +199,9 @@ def _substitute_import_keys(self, import_nodes: list[ast.stmt]) -> list[ast.stmt @staticmethod def check_With_for_defer_usage(node: ast.With) -> bool: + """Only accept "with defer_imports.until_use".""" + return len(node.items) == 1 and ( - # Allow "with defer_imports.until_use". isinstance(node.items[0].context_expr, ast.Attribute) and isinstance(node.items[0].context_expr.value, ast.Name) and node.items[0].context_expr.value.id == "defer_imports" diff --git a/tests/test_deferred.py b/tests/test_deferred.py index b8705f1..a253006 100644 --- a/tests/test_deferred.py +++ b/tests/test_deferred.py @@ -63,7 +63,7 @@ def temp_cache_module(name: str, module: ModuleType): from defer_imports._core import DeferredImportKey as @DeferredImportKey, DeferredImportProxy as @DeferredImportProxy del @DeferredImportKey, @DeferredImportProxy ''', - id="Inserts statements after module docstring", + id="inserts statements after module docstring", ), pytest.param( """from __future__ import annotations""", @@ -91,7 +91,7 @@ def temp_cache_module(name: str, module: ModuleType): import inspect del @DeferredImportKey, @DeferredImportProxy """, - id="does nothing if used at same time as another context manager", + id="does nothing if used with another context manager", ), pytest.param( """\ @@ -140,7 +140,7 @@ def temp_cache_module(name: str, module: ModuleType): del @temp_proxy, @local_ns del @DeferredImportKey, @DeferredImportProxy """, - id="mixed import 1", + id="mixed import", ), pytest.param( """\ @@ -162,7 +162,7 @@ def temp_cache_module(name: str, module: ModuleType): del @temp_proxy, @local_ns del @DeferredImportKey, @DeferredImportProxy """, - id="relative import 1", + id="relative import", ), ], )