@@ -18,19 +18,12 @@ def __init__(self) -> None:
1818 self ._cached_modules : Set [str ] | None = None
1919 self ._rustgraph = rust .Graph ()
2020
21- # TODO(peter) Be wary about `if X in graph.modules` since we need to
22- # convert the entire graph from rust -> python, which can hurt performance.
23- # Prefer `graph.contains_module`. Is there a way to make this clearer, it feels like
24- # a performance footgun.
2521 @property
2622 def modules (self ) -> Set [str ]:
2723 if self ._cached_modules is None :
2824 self ._cached_modules = self ._rustgraph .get_modules ()
2925 return self ._cached_modules
3026
31- def contains_module (self , module : str ) -> bool :
32- return self ._rustgraph .contains_module (module )
33-
3427 def add_module (self , module : str , is_squashed : bool = False ) -> None :
3528 self ._cached_modules = None
3629 self ._rustgraph .add_module (module , is_squashed )
@@ -41,12 +34,12 @@ def remove_module(self, module: str) -> None:
4134
4235 def squash_module (self , module : str ) -> None :
4336 self ._cached_modules = None
44- if not self .contains_module (module ):
37+ if not self ._rustgraph . contains_module (module ):
4538 raise ModuleNotPresent (f'"{ module } " not present in the graph.' )
4639 self ._rustgraph .squash_module (module )
4740
4841 def is_module_squashed (self , module : str ) -> bool :
49- if not self .contains_module (module ):
42+ if not self ._rustgraph . contains_module (module ):
5043 raise ModuleNotPresent (f'"{ module } " not present in the graph.' )
5144 return self ._rustgraph .is_module_squashed (module )
5245
@@ -98,7 +91,7 @@ def find_modules_directly_imported_by(self, module: str) -> Set[str]:
9891 return self ._rustgraph .find_modules_directly_imported_by (module )
9992
10093 def find_modules_that_directly_import (self , module : str ) -> Set [str ]:
101- if module in self ._rustgraph .get_modules ( ):
94+ if self ._rustgraph .contains_module ( module ):
10295 # TODO panics if module isn't in modules.
10396 return self ._rustgraph .find_modules_that_directly_import (module )
10497 return set ()
@@ -117,7 +110,7 @@ def find_upstream_modules(self, module: str, as_package: bool = False) -> Set[st
117110
118111 def find_shortest_chain (self , importer : str , imported : str ) -> tuple [str , ...] | None :
119112 for module in (importer , imported ):
120- if not self .contains_module (module ):
113+ if not self ._rustgraph . contains_module (module ):
121114 raise ValueError (f"Module { module } is not present in the graph." )
122115
123116 chain = self ._rustgraph .find_shortest_chain (importer , imported )
0 commit comments