@@ -85,73 +85,87 @@ def imported_exports(self) -> list[Exportable]:
8585 @noapidoc
8686 @reader
8787 def resolve_import (self , base_path : str | None = None , * , add_module_name : str | None = None ) -> ImportResolution [PyFile ] | None :
88- base_path = base_path or self .ctx .projects [0 ].base_path or ""
89- module_source = self .module .source if self .module else ""
90- symbol_name = self .symbol_name .source if self .symbol_name else ""
91- if add_module_name :
92- module_source += f".{ symbol_name } "
93- symbol_name = add_module_name
94- # If import is relative, convert to absolute path
95- if module_source .startswith ("." ):
96- module_source = self ._relative_to_absolute_import (module_source )
97-
98- # =====[ Check if we are importing an entire file ]=====
99- if self .is_module_import ():
100- # covers `import a.b.c` case and `from a.b.c import *` case
101- filepath = os .path .join (base_path , module_source .replace ("." , "/" ) + ".py" )
102- else :
103- # This is the case where you do:
104- # `from a.b.c import foo`
105- filepath = os .path .join (
106- base_path ,
107- module_source .replace ("." , "/" ) + "/" + symbol_name + ".py" ,
108- )
109-
110- # =====[ Check if we are importing an entire file with custom resolve path or sys.path enabled ]=====
111- if len (self .ctx .config .import_resolution_paths ) > 0 or self .ctx .config .py_resolve_syspath :
112- # Handle resolve overrides first if both is set
113- resolve_paths : list [str ] = self .ctx .config .import_resolution_paths + (sys .path if self .ctx .config .py_resolve_syspath else [])
114- if file := self ._file_by_custom_resolve_paths (resolve_paths , filepath ):
88+ try :
89+ base_path = base_path or self .ctx .projects [0 ].base_path or ""
90+ module_source = self .module .source if self .module else ""
91+ symbol_name = self .symbol_name .source if self .symbol_name else ""
92+ if add_module_name :
93+ module_source += f".{ symbol_name } "
94+ symbol_name = add_module_name
95+ # If import is relative, convert to absolute path
96+ if module_source .startswith ("." ):
97+ module_source = self ._relative_to_absolute_import (module_source )
98+
99+ # =====[ Check if we are importing an entire file ]=====
100+ if self .is_module_import ():
101+ # covers `import a.b.c` case and `from a.b.c import *` case
102+ filepath = os .path .join (base_path , module_source .replace ("." , "/" ) + ".py" )
103+ else :
104+ # This is the case where you do:
105+ # `from a.b.c import foo`
106+ filepath = os .path .join (
107+ base_path ,
108+ module_source .replace ("." , "/" ) + "/" + symbol_name + ".py" ,
109+ )
110+
111+ # =====[ Check if we are importing an entire file with custom resolve path or sys.path enabled ]=====
112+ if len (self .ctx .config .import_resolution_paths ) > 0 or self .ctx .config .py_resolve_syspath :
113+ # Handle resolve overrides first if both is set
114+ resolve_paths : list [str ] = self .ctx .config .import_resolution_paths + (sys .path if self .ctx .config .py_resolve_syspath else [])
115+ if file := self ._file_by_custom_resolve_paths (resolve_paths , filepath ):
116+ return ImportResolution (from_file = file , symbol = None , imports_file = True )
117+
118+ # =====[ Default path ]=====
119+ if file := self .ctx .get_file (filepath ):
120+ return ImportResolution (from_file = file , symbol = None , imports_file = True )
121+
122+ filepath = filepath .replace (".py" , "/__init__.py" )
123+ if file := self .ctx .get_file (filepath ):
124+ # TODO - I think this is another edge case, due to `dao/__init__.py` etc.
125+ # You can't do `from a.b.c import foo` => `foo.utils.x` right now since `foo` is just a file...
115126 return ImportResolution (from_file = file , symbol = None , imports_file = True )
116127
117- # =====[ Default path ]=====
118- if file := self .ctx .get_file (filepath ):
119- return ImportResolution (from_file = file , symbol = None , imports_file = True )
120-
121- filepath = filepath .replace (".py" , "/__init__.py" )
122- if file := self .ctx .get_file (filepath ):
123- # TODO - I think this is another edge case, due to `dao/__init__.py` etc.
124- # You can't do `from a.b.c import foo` => `foo.utils.x` right now since `foo` is just a file...
125- return ImportResolution (from_file = file , symbol = None , imports_file = True )
126-
127- # =====[ Check if `module.py` file exists in the graph with custom resolve path or sys.path enabled ]=====
128- filepath = module_source .replace ("." , "/" ) + ".py"
129- if len (self .ctx .config .import_resolution_paths ) > 0 or self .ctx .config .py_resolve_syspath :
130- # Handle resolve overrides first if both is set
131- resolve_paths : list [str ] = self .ctx .config .import_resolution_paths + (sys .path if self .ctx .config .py_resolve_syspath else [])
132- if file := self ._file_by_custom_resolve_paths (resolve_paths , filepath ):
128+ # =====[ Check if `module.py` file exists in the graph with custom resolve path or sys.path enabled ]=====
129+ filepath = module_source .replace ("." , "/" ) + ".py"
130+ if len (self .ctx .config .import_resolution_paths ) > 0 or self .ctx .config .py_resolve_syspath :
131+ # Handle resolve overrides first if both is set
132+ resolve_paths : list [str ] = self .ctx .config .import_resolution_paths + (sys .path if self .ctx .config .py_resolve_syspath else [])
133+ if file := self ._file_by_custom_resolve_paths (resolve_paths , filepath ):
134+ symbol = file .get_node_by_name (symbol_name )
135+ return ImportResolution (from_file = file , symbol = symbol )
136+
137+ # =====[ Check if `module.py` file exists in the graph ]=====
138+ filepath = os .path .join (base_path , filepath )
139+ if file := self .ctx .get_file (filepath ):
133140 symbol = file .get_node_by_name (symbol_name )
134- return ImportResolution (from_file = file , symbol = symbol )
135-
136- # =====[ Check if `module.py` file exists in the graph ]=====
137- filepath = os .path .join (base_path , filepath )
138- if file := self .ctx .get_file (filepath ):
139- symbol = file .get_node_by_name (symbol_name )
140- if symbol is None :
141- if file .get_node_from_wildcard_chain (symbol_name ):
142- return ImportResolution (from_file = file , symbol = None , imports_file = True )
141+ if symbol is None :
142+ if file .get_node_from_wildcard_chain (symbol_name ):
143+ return ImportResolution (from_file = file , symbol = None , imports_file = True )
144+ else :
145+ # This is most likely a broken import
146+ return ImportResolution (from_file = file , symbol = None )
143147 else :
144- # This is most likely a broken import
145- return ImportResolution (from_file = file , symbol = None )
146- else :
147- return ImportResolution (from_file = file , symbol = symbol )
148-
149- # =====[ Check if `module/__init__.py` file exists in the graph with custom resolve path or sys.path enabled ]=====
150- filepath = filepath .replace (".py" , "/__init__.py" )
151- if len (self .ctx .config .import_resolution_paths ) > 0 or self .ctx .config .py_resolve_syspath :
152- # Handle resolve overrides first if both is set
153- resolve_paths : list [str ] = self .ctx .config .import_resolution_paths + (sys .path if self .ctx .config .py_resolve_syspath else [])
154- if from_file := self ._file_by_custom_resolve_paths (resolve_paths , filepath ):
148+ return ImportResolution (from_file = file , symbol = symbol )
149+
150+ # =====[ Check if `module/__init__.py` file exists in the graph with custom resolve path or sys.path enabled ]=====
151+ filepath = filepath .replace (".py" , "/__init__.py" )
152+ if len (self .ctx .config .import_resolution_paths ) > 0 or self .ctx .config .py_resolve_syspath :
153+ # Handle resolve overrides first if both is set
154+ resolve_paths : list [str ] = self .ctx .config .import_resolution_paths + (sys .path if self .ctx .config .py_resolve_syspath else [])
155+ if from_file := self ._file_by_custom_resolve_paths (resolve_paths , filepath ):
156+ symbol = from_file .get_node_by_name (symbol_name )
157+ if symbol is None :
158+ if from_file .get_node_from_wildcard_chain (symbol_name ):
159+ return ImportResolution (from_file = from_file , symbol = None , imports_file = True )
160+ else :
161+ # This is most likely a broken import
162+ return ImportResolution (from_file = from_file , symbol = None )
163+
164+ else :
165+ return ImportResolution (from_file = from_file , symbol = symbol )
166+
167+ # =====[ Check if `module/__init__.py` file exists in the graph ]=====
168+ if from_file := self .ctx .get_file (filepath ):
155169 symbol = from_file .get_node_by_name (symbol_name )
156170 if symbol is None :
157171 if from_file .get_node_from_wildcard_chain (symbol_name ):
@@ -163,40 +177,30 @@ def resolve_import(self, base_path: str | None = None, *, add_module_name: str |
163177 else :
164178 return ImportResolution (from_file = from_file , symbol = symbol )
165179
166- # =====[ Check if `module/__init__.py` file exists in the graph ]=====
167- if from_file := self .ctx .get_file (filepath ):
168- symbol = from_file .get_node_by_name (symbol_name )
169- if symbol is None :
170- if from_file .get_node_from_wildcard_chain (symbol_name ):
171- return ImportResolution (from_file = from_file , symbol = None , imports_file = True )
172- else :
173- # This is most likely a broken import
174- return ImportResolution (from_file = from_file , symbol = None )
175-
176- else :
177- return ImportResolution (from_file = from_file , symbol = symbol )
178-
179- # =====[ Case: Can't resolve the import ]=====
180- if base_path == "" :
181- # Try to resolve with "src" as the base path
182- return self .resolve_import (base_path = "src" , add_module_name = add_module_name )
183- if base_path == "src" :
184- # Try "test" next
185- return self .resolve_import (base_path = "test" , add_module_name = add_module_name )
180+ # =====[ Case: Can't resolve the import ]=====
181+ if base_path == "" :
182+ # Try to resolve with "src" as the base path
183+ return self .resolve_import (base_path = "src" , add_module_name = add_module_name )
184+ if base_path == "src" :
185+ # Try "test" next
186+ return self .resolve_import (base_path = "test" , add_module_name = add_module_name )
186187
187- # if not G_override:
188- # for resolver in ctx.import_resolvers:
189- # if imp := resolver.resolve(self):
190- # return imp
188+ # if not G_override:
189+ # for resolver in ctx.import_resolvers:
190+ # if imp := resolver.resolve(self):
191+ # return imp
191192
192- return None
193- # # =====[ Check if we are importing an external module in the graph ]=====
194- # if ext := self.ctx.get_external_module(self.source, self._unique_node.source):
195- # return ImportResolution(symbol=ext)
196- # # Implies we are not importing the symbol from the current repo.
197- # # In these cases, consider the import as an ExternalModule and add to graph
198- # ext = ExternalModule.from_import(self)
199- # return ImportResolution(symbol=ext)
193+ return None
194+ # # =====[ Check if we are importing an external module in the graph ]=====
195+ # if ext := self.ctx.get_external_module(self.source, self._unique_node.source):
196+ # return ImportResolution(symbol=ext)
197+ # # Implies we are not importing the symbol from the current repo.
198+ # # In these cases, consider the import as an ExternalModule and add to graph
199+ # ext = ExternalModule.from_import(self)
200+ # return ImportResolution(symbol=ext)
201+ except AssertionError :
202+ # Codebase is probably trying to import file from outside repo
203+ return None
200204
201205 @noapidoc
202206 @reader
0 commit comments