Skip to content

Commit d8831c6

Browse files
authored
Merge pull request #9148 from bluetech/lru-cache-method
Avoid `@lru_cache` on methods
2 parents 3a6eaa9 + 16e5fbe commit d8831c6

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/_pytest/assertion/rewrite.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ def _fix(node, lineno, col_offset):
554554
return node
555555

556556

557+
@functools.lru_cache(maxsize=1)
557558
def _get_assertion_exprs(src: bytes) -> Dict[int, str]:
558559
"""Return a mapping from {lineno: "assertion test expression"}."""
559560
ret: Dict[int, str] = {}
@@ -675,10 +676,6 @@ def __init__(
675676
self.enable_assertion_pass_hook = False
676677
self.source = source
677678

678-
@functools.lru_cache(maxsize=1)
679-
def _assert_expr_to_lineno(self) -> Dict[int, str]:
680-
return _get_assertion_exprs(self.source)
681-
682679
def run(self, mod: ast.Module) -> None:
683680
"""Find all assert statements in *mod* and rewrite them."""
684681
if not mod.body:
@@ -906,7 +903,7 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]:
906903

907904
# Passed
908905
fmt_pass = self.helper("_format_explanation", msg)
909-
orig = self._assert_expr_to_lineno()[assert_.lineno]
906+
orig = _get_assertion_exprs(self.source)[assert_.lineno]
910907
hook_call_pass = ast.Expr(
911908
self.helper(
912909
"_call_assertion_pass",

src/_pytest/config/__init__.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ def _try_load_conftest(
522522
if x.is_dir():
523523
self._getconftestmodules(x, importmode, rootpath)
524524

525-
@lru_cache(maxsize=128)
526525
def _getconftestmodules(
527526
self, path: Path, importmode: Union[str, ImportMode], rootpath: Path
528527
) -> List[types.ModuleType]:
@@ -534,12 +533,19 @@ def _getconftestmodules(
534533
else:
535534
directory = path
536535

536+
# Optimization: avoid repeated searches in the same directory.
537+
# Assumes always called with same importmode and rootpath.
538+
existing_clist = self._dirpath2confmods.get(directory)
539+
if existing_clist:
540+
return existing_clist
541+
537542
# XXX these days we may rather want to use config.rootpath
538543
# and allow users to opt into looking into the rootdir parent
539544
# directories instead of requiring to specify confcutdir.
540545
clist = []
546+
confcutdir_parents = self._confcutdir.parents if self._confcutdir else []
541547
for parent in reversed((directory, *directory.parents)):
542-
if self._confcutdir and parent in self._confcutdir.parents:
548+
if parent in confcutdir_parents:
543549
continue
544550
conftestpath = parent / "conftest.py"
545551
if conftestpath.is_file():

0 commit comments

Comments
 (0)