1-
21import python
32import semmle.python.dependencies.DependencyKind
43
54private predicate importDependency ( Object target , AstNode source ) {
6- source .getScope ( ) != target .getOrigin ( ) and /* Imports of own module are ignored */
5+ source .getScope ( ) != target .getOrigin ( ) and
6+ /* Imports of own module are ignored */
77 (
88 exists ( ModuleObject importee , ImportingStmt imp_stmt |
99 source = imp_stmt and
10- importee = target |
10+ importee = target
11+ |
1112 exists ( ImportMember im | imp_stmt .contains ( im ) |
1213 importee .importedAs ( im .getImportedModuleName ( ) )
1314 )
@@ -23,28 +24,22 @@ private predicate importDependency(Object target, AstNode source) {
2324 )
2425 or
2526 /* from m import name, where m.name is not a submodule */
26- exists ( PythonModuleObject importee , ImportingStmt imp_stmt |
27- source = imp_stmt |
27+ exists ( PythonModuleObject importee , ImportingStmt imp_stmt | source = imp_stmt |
2828 exists ( ImportMember im | imp_stmt .contains ( im ) |
29- importee .importedAs ( im .getModule ( ) .( ImportExpr ) .getImportedModuleName ( ) )
30- and
29+ importee .importedAs ( im .getModule ( ) .( ImportExpr ) .getImportedModuleName ( ) ) and
3130 defn_of_module_attribute ( target , importee .getModule ( ) , im .getName ( ) )
3231 )
3332 )
3433 )
3534}
3635
3736class PythonImport extends DependencyKind {
38-
39- PythonImport ( ) {
40- this = "import"
41- }
37+ PythonImport ( ) { this = "import" }
4238
4339 override predicate isADependency ( AstNode source , Object target ) {
4440 this = this and
4541 importDependency ( target , source )
4642 }
47-
4843}
4944
5045private predicate interesting ( Object target ) {
@@ -58,10 +53,7 @@ private predicate interesting(Object target) {
5853}
5954
6055class PythonUse extends DependencyKind {
61-
62- PythonUse ( ) {
63- this = "use"
64- }
56+ PythonUse ( ) { this = "use" }
6557
6658 override predicate isADependency ( AstNode source , Object target ) {
6759 interesting ( target ) and
@@ -71,58 +63,46 @@ class PythonUse extends DependencyKind {
7163 use .getNode ( ) = source and
7264 use .refersTo ( obj ) and
7365 use .isLoad ( )
74- |
66+ |
7567 interesting ( obj ) and target = obj
76- )
77- and
68+ ) and
7869 not has_more_specific_dependency_source ( source )
7970 }
80-
8171}
8272
83- /** Whether there is a more specific dependency source than this one.
73+ /**
74+ * Whether there is a more specific dependency source than this one.
8475 * E.g. if the expression pack.mod.func is a dependency on the function 'func' in 'pack.mod'
8576 * don't make pack.mod depend on the module 'pack.mod'
8677 */
8778private predicate has_more_specific_dependency_source ( Expr e ) {
88- exists ( Attribute member |
89- member .getObject ( ) = e |
79+ exists ( Attribute member | member .getObject ( ) = e |
9080 attribute_access_dependency ( _, member )
9181 or
9282 has_more_specific_dependency_source ( member )
9383 )
9484}
9585
9686class PythonInheritance extends DependencyKind {
97-
98- PythonInheritance ( ) {
99- this = "inheritance"
100- }
87+ PythonInheritance ( ) { this = "inheritance" }
10188
10289 override predicate isADependency ( AstNode source , Object target ) {
10390 this = this and
104- exists ( ClassObject cls |
105- source = cls .getOrigin ( )
106- |
91+ exists ( ClassObject cls | source = cls .getOrigin ( ) |
10792 target = cls .getASuperType ( )
10893 or
10994 target = cls .getAnInferredType ( )
11095 )
11196 }
112-
11397}
11498
11599class PythonAttribute extends DependencyKind {
116-
117- PythonAttribute ( ) {
118- this = "attribute"
119- }
100+ PythonAttribute ( ) { this = "attribute" }
120101
121102 override predicate isADependency ( AstNode source , Object target ) {
122103 this = this and
123104 attribute_access_dependency ( target , source )
124105 }
125-
126106}
127107
128108private predicate attribute_access_dependency ( Object target , AstNode source ) {
@@ -133,30 +113,23 @@ private predicate attribute_access_dependency(Object target, AstNode source) {
133113}
134114
135115private predicate use_of_attribute ( Attribute attr , Scope s , string name ) {
136- exists ( AttrNode cfg |
137- cfg .isLoad ( ) and cfg .getNode ( ) = attr
138- |
139- exists ( Object obj |
140- cfg .getObject ( name ) .refersTo ( obj ) |
116+ exists ( AttrNode cfg | cfg .isLoad ( ) and cfg .getNode ( ) = attr |
117+ exists ( Object obj | cfg .getObject ( name ) .refersTo ( obj ) |
141118 s = obj .( PythonModuleObject ) .getModule ( ) or
142119 s = obj .( ClassObject ) .getPyClass ( )
143- ) or
144- exists ( ClassObject cls |
145- cfg .getObject ( name ) .refersTo ( _, cls , _) |
146- s = cls .getPyClass ( )
147120 )
121+ or
122+ exists ( ClassObject cls | cfg .getObject ( name ) .refersTo ( _, cls , _) | s = cls .getPyClass ( ) )
148123 )
149124 or
150- exists ( SelfAttributeRead sar |
151- sar = attr |
125+ exists ( SelfAttributeRead sar | sar = attr |
152126 sar .getClass ( ) = s and
153127 sar .getName ( ) = name
154128 )
155129}
156130
157131private predicate defn_of_attribute ( Object target , Scope s , string name ) {
158- exists ( Assign asgn |
159- target .( ControlFlowNode ) .getNode ( ) = asgn |
132+ exists ( Assign asgn | target .( ControlFlowNode ) .getNode ( ) = asgn |
160133 defn_of_instance_attribute ( asgn , s , name )
161134 or
162135 defn_of_class_attribute ( asgn , s , name )
@@ -165,13 +138,14 @@ private predicate defn_of_attribute(Object target, Scope s, string name) {
165138 defn_of_module_attribute ( target , s , name )
166139}
167140
168- /* Whether asgn defines an instance attribute, that is does
141+ /*
142+ * Whether asgn defines an instance attribute, that is does
169143 * asgn take the form self.name = ... where self is an instance
170144 * of class c and asgn is not a redefinition.
171145 */
146+
172147private predicate defn_of_instance_attribute ( Assign asgn , Class c , string name ) {
173- exists ( SelfAttributeStore sas |
174- asgn .getATarget ( ) = sas |
148+ exists ( SelfAttributeStore sas | asgn .getATarget ( ) = sas |
175149 sas .getClass ( ) = c and
176150 sas .getName ( ) = name and
177151 not exists ( SelfAttributeStore in_init |
0 commit comments