62
62
class StmtVisitor (ast .NodeVisitor ):
63
63
def __init__ (self , allow_local_directory_imports = True ):
64
64
self ._allow_local_modules = allow_local_directory_imports
65
- self .bb_or_bi_aliases = {}
66
65
super ().__init__ ()
67
66
68
67
def visit_Module (self , node ):
@@ -628,7 +627,8 @@ def add_blackbox_or_builtin_call(self, node, blackbox): # noqa: C901
628
627
629
628
# Check if function call matches a blackbox/built-in alias and if so, resolve it
630
629
# This resolves aliases like "from os import system as mysys" as: mysys -> os.system
631
- call_function_label = fully_qualify_alias_labels (call_function_label , self .bb_or_bi_aliases )
630
+ local_definitions = self .module_definitions_stack [- 1 ]
631
+ call_function_label = fully_qualify_alias_labels (call_function_label , local_definitions .import_alias_mapping )
632
632
633
633
# Create e.g. ~call_1 = ret_func_foo
634
634
LHS = CALL_IDENTIFIER + 'call_' + str (saved_function_call_index )
@@ -924,10 +924,10 @@ def from_directory_import(
924
924
if init_exists and not skip_init :
925
925
package_name = os .path .split (module_path )[1 ]
926
926
return self .add_module (
927
- (module [0 ], init_file_location ),
928
- package_name ,
929
- local_names ,
930
- import_alias_mapping ,
927
+ module = (module [0 ], init_file_location ),
928
+ module_or_package_name = package_name ,
929
+ local_names = local_names ,
930
+ import_alias_mapping = import_alias_mapping ,
931
931
is_init = True ,
932
932
from_from = True
933
933
)
@@ -937,10 +937,10 @@ def from_directory_import(
937
937
new_init_file_location = os .path .join (full_name , '__init__.py' )
938
938
if os .path .isfile (new_init_file_location ):
939
939
self .add_module (
940
- (real_name , new_init_file_location ),
941
- real_name ,
942
- local_names ,
943
- import_alias_mapping ,
940
+ module = (real_name , new_init_file_location ),
941
+ module_or_package_name = real_name ,
942
+ local_names = local_names ,
943
+ import_alias_mapping = import_alias_mapping ,
944
944
is_init = True ,
945
945
from_from = True ,
946
946
from_fdid = True
@@ -950,10 +950,10 @@ def from_directory_import(
950
950
else :
951
951
file_module = (real_name , full_name + '.py' )
952
952
self .add_module (
953
- file_module ,
954
- real_name ,
955
- local_names ,
956
- import_alias_mapping ,
953
+ module = file_module ,
954
+ module_or_package_name = real_name ,
955
+ local_names = local_names ,
956
+ import_alias_mapping = import_alias_mapping ,
957
957
from_from = True
958
958
)
959
959
return IgnoredNode ()
@@ -964,10 +964,10 @@ def import_package(self, module, module_name, local_name, import_alias_mapping):
964
964
init_exists = os .path .isfile (init_file_location )
965
965
if init_exists :
966
966
return self .add_module (
967
- (module [0 ], init_file_location ),
968
- module_name ,
969
- local_name ,
970
- import_alias_mapping ,
967
+ module = (module [0 ], init_file_location ),
968
+ module_or_package_name = module_name ,
969
+ local_names = local_name ,
970
+ import_alias_mapping = import_alias_mapping ,
971
971
is_init = True
972
972
)
973
973
else :
@@ -1010,10 +1010,10 @@ def handle_relative_import(self, node):
1010
1010
# Is it a file?
1011
1011
if name_with_dir .endswith ('.py' ):
1012
1012
return self .add_module (
1013
- (node .module , name_with_dir ),
1014
- None ,
1015
- as_alias_handler (node .names ),
1016
- retrieve_import_alias_mapping (node .names ),
1013
+ module = (node .module , name_with_dir ),
1014
+ module_or_package_name = None ,
1015
+ local_names = as_alias_handler (node .names ),
1016
+ import_alias_mapping = retrieve_import_alias_mapping (node .names ),
1017
1017
from_from = True
1018
1018
)
1019
1019
return self .from_directory_import (
@@ -1036,10 +1036,10 @@ def visit_Import(self, node):
1036
1036
retrieve_import_alias_mapping (node .names )
1037
1037
)
1038
1038
return self .add_module (
1039
- module ,
1040
- name .name ,
1041
- name .asname ,
1042
- retrieve_import_alias_mapping (node .names )
1039
+ module = module ,
1040
+ module_or_package_name = name .name ,
1041
+ local_names = name .asname ,
1042
+ import_alias_mapping = retrieve_import_alias_mapping (node .names )
1043
1043
)
1044
1044
for module in self .project_modules :
1045
1045
if name .name == module [0 ]:
@@ -1051,68 +1051,69 @@ def visit_Import(self, node):
1051
1051
retrieve_import_alias_mapping (node .names )
1052
1052
)
1053
1053
return self .add_module (
1054
- module ,
1055
- name .name ,
1056
- name .asname ,
1057
- retrieve_import_alias_mapping (node .names )
1054
+ module = module ,
1055
+ module_or_package_name = name .name ,
1056
+ local_names = name .asname ,
1057
+ import_alias_mapping = retrieve_import_alias_mapping (node .names )
1058
1058
)
1059
1059
for alias in node .names :
1060
- if alias . name in uninspectable_modules :
1061
- # The module is uninspectable (so blackbox or built-in). If it has an alias, we remember
1062
- # the alias so we can do fully qualified name resolution for blackbox- and built-in trigger words
1063
- # e.g. we want a call to "os.system" be recognised, even if we do "import os as myos"
1064
- if alias . asname is not None and alias . asname != alias . name :
1065
- self . bb_or_bi_aliases [ alias .asname ] = alias .name
1066
- else :
1067
- log .warn ("Cannot inspect module %s" , alias .name )
1060
+ # The module is uninspectable (so blackbox or built-in). If it has an alias, we remember
1061
+ # the alias so we can do fully qualified name resolution for blackbox- and built-in trigger words
1062
+ # e.g. we want a call to "os.system" be recognised, even if we do "import os as myos"
1063
+ if alias . asname is not None and alias . asname != alias . name :
1064
+ local_definitions = self . module_definitions_stack [ - 1 ]
1065
+ local_definitions . import_alias_mapping [ name .asname ] = alias .name
1066
+ if alias . name not in uninspectable_modules :
1067
+ log .warning ("Cannot inspect module %s" , alias .name )
1068
1068
uninspectable_modules .add (alias .name ) # Don't repeatedly warn about this
1069
1069
return IgnoredNode ()
1070
1070
1071
1071
def visit_ImportFrom (self , node ):
1072
1072
# Is it relative?
1073
1073
if node .level > 0 :
1074
1074
return self .handle_relative_import (node )
1075
- else :
1076
- for module in self .local_modules :
1077
- if node .module == module [0 ]:
1078
- if os .path .isdir (module [1 ]):
1079
- return self .from_directory_import (
1080
- module ,
1081
- not_as_alias_handler (node .names ),
1082
- as_alias_handler (node .names )
1083
- )
1084
- return self .add_module (
1075
+ # not relative
1076
+ for module in self .local_modules :
1077
+ if node .module == module [0 ]:
1078
+ if os .path .isdir (module [1 ]):
1079
+ return self .from_directory_import (
1085
1080
module ,
1086
- None ,
1087
- as_alias_handler (node .names ),
1088
- retrieve_import_alias_mapping (node .names ),
1089
- from_from = True
1081
+ not_as_alias_handler (node .names ),
1082
+ as_alias_handler (node .names )
1090
1083
)
1091
- for module in self .project_modules :
1092
- name = module [0 ]
1093
- if node .module == name :
1094
- if os .path .isdir (module [1 ]):
1095
- return self .from_directory_import (
1096
- module ,
1097
- not_as_alias_handler (node .names ),
1098
- as_alias_handler (node .names ),
1099
- retrieve_import_alias_mapping (node .names )
1100
- )
1101
- return self .add_module (
1084
+ return self .add_module (
1085
+ module = module ,
1086
+ module_or_package_name = None ,
1087
+ local_names = as_alias_handler (node .names ),
1088
+ import_alias_mapping = retrieve_import_alias_mapping (node .names ),
1089
+ from_from = True
1090
+ )
1091
+ for module in self .project_modules :
1092
+ name = module [0 ]
1093
+ if node .module == name :
1094
+ if os .path .isdir (module [1 ]):
1095
+ return self .from_directory_import (
1102
1096
module ,
1103
- None ,
1097
+ not_as_alias_handler ( node . names ) ,
1104
1098
as_alias_handler (node .names ),
1105
- retrieve_import_alias_mapping (node .names ),
1106
- from_from = True
1099
+ retrieve_import_alias_mapping (node .names )
1107
1100
)
1101
+ return self .add_module (
1102
+ module = module ,
1103
+ module_or_package_name = None ,
1104
+ local_names = as_alias_handler (node .names ),
1105
+ import_alias_mapping = retrieve_import_alias_mapping (node .names ),
1106
+ from_from = True
1107
+ )
1108
1108
1109
- if node .module in uninspectable_modules :
1110
- # Remember aliases for blackboxed and built-in imports such that we can label them fully qualified
1111
- # e.g. we want a call to "os.system" be recognised, even if we do "from os import system"
1112
- # from os import system as mysystem -> module=os, name=system, asname=mysystem
1113
- for name in node .names :
1114
- self .bb_or_bi_aliases [name .asname or name .name ] = "{}.{}" .format (node .module , name .name )
1115
- else :
1116
- log .warn ("Cannot inspect module %s" , node .module )
1109
+ # Remember aliases for uninspecatble modules such that we can label them fully qualified
1110
+ # e.g. we want a call to "os.system" be recognised, even if we do "from os import system"
1111
+ # from os import system as mysystem -> module=os, name=system, asname=mysystem
1112
+ for name in node .names :
1113
+ local_definitions = self .module_definitions_stack [- 1 ]
1114
+ local_definitions .import_alias_mapping [name .asname or name .name ] = "{}.{}" .format (node .module , name .name )
1115
+
1116
+ if node .module not in uninspectable_modules :
1117
+ log .warning ("Cannot inspect module %s" , node .module )
1117
1118
uninspectable_modules .add (node .module )
1118
1119
return IgnoredNode ()
0 commit comments