Skip to content

Commit 268a3fd

Browse files
authored
Merge pull request github#8680 from RasmusWL/subclass
Python: Refactor how we find a `Class` from `API::Node`
2 parents ef9b6a1 + f8f4142 commit 268a3fd

File tree

7 files changed

+20
-16
lines changed

7 files changed

+20
-16
lines changed

python/ql/lib/semmle/python/frameworks/Aiohttp.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ module AiohttpWebModel {
243243

244244
/** A class that has a super-type which is an aiohttp.web View class. */
245245
class AiohttpViewClassFromSuperClass extends AiohttpViewClass {
246-
AiohttpViewClassFromSuperClass() { this.getABase() = View::subclassRef().getAUse().asExpr() }
246+
AiohttpViewClassFromSuperClass() {
247+
this.getParent() = View::subclassRef().getAnImmediateUse().asExpr()
248+
}
247249
}
248250

249251
/** A class that is used in a route-setup, therefore being considered an aiohttp.web View class. */

python/ql/lib/semmle/python/frameworks/Django.qll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ module PrivateDjango {
829829

830830
/** Gets the (AST) class of the Django model class `modelClass`. */
831831
Class getModelClassClass(API::Node modelClass) {
832-
result.getParent() = modelClass.getAUse().asExpr().(ClassExpr) and
832+
result.getParent() = modelClass.getAnImmediateUse().asExpr() and
833833
modelClass = Model::subclassRef()
834834
}
835835

@@ -2162,7 +2162,9 @@ module PrivateDjango {
21622162
* thereby handling user input.
21632163
*/
21642164
class DjangoFormClass extends Class, SelfRefMixin {
2165-
DjangoFormClass() { this.getABase() = Django::Forms::Form::subclassRef().getAUse().asExpr() }
2165+
DjangoFormClass() {
2166+
this.getParent() = Django::Forms::Form::subclassRef().getAnImmediateUse().asExpr()
2167+
}
21662168
}
21672169

21682170
/**
@@ -2195,7 +2197,7 @@ module PrivateDjango {
21952197
*/
21962198
class DjangoFormFieldClass extends Class {
21972199
DjangoFormFieldClass() {
2198-
this.getABase() = Django::Forms::Field::subclassRef().getAUse().asExpr()
2200+
this.getParent() = Django::Forms::Field::subclassRef().getAnImmediateUse().asExpr()
21992201
}
22002202
}
22012203

@@ -2298,7 +2300,7 @@ module PrivateDjango {
22982300
*/
22992301
class DjangoViewClassFromSuperClass extends DjangoViewClass {
23002302
DjangoViewClassFromSuperClass() {
2301-
this.getABase() = Django::Views::View::subclassRef().getAUse().asExpr()
2303+
this.getParent() = Django::Views::View::subclassRef().getAnImmediateUse().asExpr()
23022304
}
23032305
}
23042306

python/ql/lib/semmle/python/frameworks/Flask.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ module Flask {
194194
API::Node api_node;
195195

196196
FlaskViewClass() {
197-
this.getABase() = Views::View::subclassRef().getAUse().asExpr() and
198-
api_node.getAnImmediateUse().asExpr() = this.getParent()
197+
api_node = Views::View::subclassRef() and
198+
this.getParent() = api_node.getAnImmediateUse().asExpr()
199199
}
200200

201201
/** Gets a function that could handle incoming requests, if any. */
@@ -219,8 +219,8 @@ module Flask {
219219
*/
220220
class FlaskMethodViewClass extends FlaskViewClass {
221221
FlaskMethodViewClass() {
222-
this.getABase() = Views::MethodView::subclassRef().getAUse().asExpr() and
223-
api_node.getAnImmediateUse().asExpr() = this.getParent()
222+
api_node = Views::MethodView::subclassRef() and
223+
this.getParent() = api_node.getAnImmediateUse().asExpr()
224224
}
225225

226226
override Function getARequestHandler() {

python/ql/lib/semmle/python/frameworks/RestFramework.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private module RestFramework {
115115
*/
116116
class RestFrameworkApiViewClass extends PrivateDjango::DjangoViewClassFromSuperClass {
117117
RestFrameworkApiViewClass() {
118-
this.getABase() = any(ModeledApiViewClasses c).getASubclass*().getAUse().asExpr()
118+
this.getParent() = any(ModeledApiViewClasses c).getASubclass*().getAnImmediateUse().asExpr()
119119
}
120120

121121
override Function getARequestHandler() {

python/ql/lib/semmle/python/frameworks/Stdlib.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ private module StdlibPrivate {
19341934

19351935
/** A HttpRequestHandler class definition (most likely in project code). */
19361936
class HttpRequestHandlerClassDef extends Class {
1937-
HttpRequestHandlerClassDef() { this.getParent() = subclassRef().getAUse().asExpr() }
1937+
HttpRequestHandlerClassDef() { this.getParent() = subclassRef().getAnImmediateUse().asExpr() }
19381938
}
19391939

19401940
/** DEPRECATED: Alias for HttpRequestHandlerClassDef */
@@ -2027,12 +2027,12 @@ private module StdlibPrivate {
20272027
private module WsgirefSimpleServer {
20282028
class WsgiServerSubclass extends Class, SelfRefMixin {
20292029
WsgiServerSubclass() {
2030-
this.getABase() =
2030+
this.getParent() =
20312031
API::moduleImport("wsgiref")
20322032
.getMember("simple_server")
20332033
.getMember("WSGIServer")
20342034
.getASubclass*()
2035-
.getAUse()
2035+
.getAnImmediateUse()
20362036
.asExpr()
20372037
}
20382038
}

python/ql/lib/semmle/python/frameworks/Tornado.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private module Tornado {
9292

9393
/** A RequestHandler class (most likely in project code). */
9494
class RequestHandlerClass extends Class {
95-
RequestHandlerClass() { this.getParent() = subclassRef().getAUse().asExpr() }
95+
RequestHandlerClass() { this.getParent() = subclassRef().getAnImmediateUse().asExpr() }
9696

9797
/** Gets a function that could handle incoming requests, if any. */
9898
Function getARequestHandler() {

python/ql/lib/semmle/python/frameworks/Twisted.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ private module Twisted {
2727
*/
2828
class TwistedResourceSubclass extends Class {
2929
TwistedResourceSubclass() {
30-
this.getABase() =
30+
this.getParent() =
3131
API::moduleImport("twisted")
3232
.getMember("web")
3333
.getMember("resource")
3434
.getMember("Resource")
3535
.getASubclass*()
36-
.getAUse()
36+
.getAnImmediateUse()
3737
.asExpr()
3838
}
3939

0 commit comments

Comments
 (0)