45
45
from typing import Type
46
46
from typing_extensions import Literal
47
47
48
- from _pytest .python import Package
49
-
50
48
51
49
def pytest_addoption (parser : Parser ) -> None :
52
50
parser .addini (
@@ -443,23 +441,6 @@ def __init__(self, config: Config) -> None:
443
441
self .startdir = config .invocation_dir
444
442
self ._initialpaths = frozenset () # type: FrozenSet[py.path.local]
445
443
446
- # Keep track of any collected nodes in here, so we don't duplicate fixtures.
447
- self ._collection_node_cache1 = (
448
- {}
449
- ) # type: Dict[py.path.local, Sequence[nodes.Collector]]
450
- self ._collection_node_cache2 = (
451
- {}
452
- ) # type: Dict[Tuple[Type[nodes.Collector], py.path.local], nodes.Collector]
453
-
454
- # Keep track of any collected collectors in matchnodes paths, so they
455
- # are not collected more than once.
456
- self ._collection_matchnodes_cache = (
457
- {}
458
- ) # type: Dict[Tuple[Type[nodes.Collector], str], CollectReport]
459
-
460
- # Dirnames of pkgs with dunder-init files.
461
- self ._collection_pkg_roots = {} # type: Dict[str, Package]
462
-
463
444
self ._bestrelpathcache = _bestrelpath_cache (
464
445
config .rootdir
465
446
) # type: Dict[py.path.local, str]
@@ -625,6 +606,21 @@ def perform_collect( # noqa: F811
625
606
def collect (self ) -> Iterator [Union [nodes .Item , nodes .Collector ]]:
626
607
from _pytest .python import Package
627
608
609
+ # Keep track of any collected nodes in here, so we don't duplicate fixtures.
610
+ node_cache1 = {} # type: Dict[py.path.local, Sequence[nodes.Collector]]
611
+ node_cache2 = (
612
+ {}
613
+ ) # type: Dict[Tuple[Type[nodes.Collector], py.path.local], nodes.Collector]
614
+
615
+ # Keep track of any collected collectors in matchnodes paths, so they
616
+ # are not collected more than once.
617
+ matchnodes_cache = (
618
+ {}
619
+ ) # type: Dict[Tuple[Type[nodes.Collector], str], CollectReport]
620
+
621
+ # Dirnames of pkgs with dunder-init files.
622
+ pkg_roots = {} # type: Dict[str, Package]
623
+
628
624
for argpath , names in self ._initial_parts :
629
625
self .trace ("processing argument" , (argpath , names ))
630
626
self .trace .root .indent += 1
@@ -641,14 +637,12 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
641
637
if parent .isdir ():
642
638
pkginit = parent .join ("__init__.py" )
643
639
if pkginit .isfile ():
644
- if pkginit not in self . _collection_node_cache1 :
640
+ if pkginit not in node_cache1 :
645
641
col = self ._collectfile (pkginit , handle_dupes = False )
646
642
if col :
647
643
if isinstance (col [0 ], Package ):
648
- self ._collection_pkg_roots [str (parent )] = col [0 ]
649
- self ._collection_node_cache1 [col [0 ].fspath ] = [
650
- col [0 ]
651
- ]
644
+ pkg_roots [str (parent )] = col [0 ]
645
+ node_cache1 [col [0 ].fspath ] = [col [0 ]]
652
646
653
647
# If it's a directory argument, recurse and look for any Subpackages.
654
648
# Let the Package collector deal with subnodes, don't collect here.
@@ -671,28 +665,28 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
671
665
for x in self ._collectfile (pkginit ):
672
666
yield x
673
667
if isinstance (x , Package ):
674
- self . _collection_pkg_roots [str (dirpath )] = x
675
- if str (dirpath ) in self . _collection_pkg_roots :
668
+ pkg_roots [str (dirpath )] = x
669
+ if str (dirpath ) in pkg_roots :
676
670
# Do not collect packages here.
677
671
continue
678
672
679
673
for x in self ._collectfile (path ):
680
674
key = (type (x ), x .fspath )
681
- if key in self . _collection_node_cache2 :
682
- yield self . _collection_node_cache2 [key ]
675
+ if key in node_cache2 :
676
+ yield node_cache2 [key ]
683
677
else :
684
- self . _collection_node_cache2 [key ] = x
678
+ node_cache2 [key ] = x
685
679
yield x
686
680
else :
687
681
assert argpath .check (file = 1 )
688
682
689
- if argpath in self . _collection_node_cache1 :
690
- col = self . _collection_node_cache1 [argpath ]
683
+ if argpath in node_cache1 :
684
+ col = node_cache1 [argpath ]
691
685
else :
692
- collect_root = self . _collection_pkg_roots .get (argpath .dirname , self )
686
+ collect_root = pkg_roots .get (argpath .dirname , self )
693
687
col = collect_root ._collectfile (argpath , handle_dupes = False )
694
688
if col :
695
- self . _collection_node_cache1 [argpath ] = col
689
+ node_cache1 [argpath ] = col
696
690
697
691
matching = []
698
692
work = [
@@ -710,11 +704,11 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
710
704
if not isinstance (node , nodes .Collector ):
711
705
continue
712
706
key = (type (node ), node .nodeid )
713
- if key in self . _collection_matchnodes_cache :
714
- rep = self . _collection_matchnodes_cache [key ]
707
+ if key in matchnodes_cache :
708
+ rep = matchnodes_cache [key ]
715
709
else :
716
710
rep = collect_one_node (node )
717
- self . _collection_matchnodes_cache [key ] = rep
711
+ matchnodes_cache [key ] = rep
718
712
if rep .passed :
719
713
submatchnodes = []
720
714
for r in rep .result :
@@ -762,10 +756,6 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
762
756
yield from matching
763
757
764
758
self .trace .root .indent -= 1
765
- self ._collection_node_cache1 .clear ()
766
- self ._collection_node_cache2 .clear ()
767
- self ._collection_matchnodes_cache .clear ()
768
- self ._collection_pkg_roots .clear ()
769
759
770
760
def genitems (
771
761
self , node : Union [nodes .Item , nodes .Collector ]
0 commit comments