-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: Modernize py/not-named-self and py/not-named-cls queries #18599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python: Modernize py/not-named-self and py/not-named-cls queries #18599
Conversation
e739f6e
to
287cf01
Compare
QHelp previews: python/ql/src/Functions/NonCls.qhelpFirst parameter of a class method is not named 'cls'The first parameter of a class method (including certain special methods such as RecommendationEnsure that the first parameter of class methods is named ExampleIn the following example, the first parameter of the class method class Entry(object):
@classmethod
def make(self):
return Entry() References
python/ql/src/Functions/NonSelf.qhelpFirst parameter of a method is not named 'self'Normal methods should have at least one parameter and the first parameter should be called RecommendationEnsure that the first parameter of a normal method is named If a ExampleIn the following cases, the first argument of class Point:
def __init__(val, x, y): # BAD: first parameter is mis-named 'val'
val._x = x
val._y = y
class Point2:
def __init__(self, x, y): # GOOD: first parameter is correctly named 'self'
self._x = x
self._y = y References
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally very nice, thank you for also updating the tests and the docs, I do think they read nicer.
Only one actual gripe, where the logic could read easier. You may also be able to reuse some existing predicates.
c.getABase() = API::builtin("type").getASubclass*().asSource().asExpr() | ||
} | ||
|
||
/** Holds if `f` is a class method. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two predicates also exist in DataFlowDispatch.qll
.
/** Holds if the first parameter of `f` should be named `cls`, but isn't. */ | ||
predicate firstArgShouldBeNamedClsAndIsnt(Function f) { | ||
shouldBeCls(f, _) and | ||
not firstArgNamedCls(f) and | ||
not metaclassNamedSelf(f, _) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads a bit confusing at the moment. If you want to group metaclass method argument named self with class arguments named class, could we at least rename shouldBeClass
to shouldReferToClass
and firstArgShouldBeNamedClsAndIsnt
to firstArgShouldReferToClsAndDosnt
?
Alternatively, rename firstArgNamedSelf
to something like normalFirstArgNamedSelf
and treat the metaclassNamedSelf
case inside firstArgShouldBeNamedSelfAndIsnt
. Then you will have to update the shouldBeXXX
logic, of course..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Stage timings look quite a bit worse 6-7x, but then we are finding nearly 1000 new alerts.. Are the new results looking good? |
Wasn't certain about the stage timings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we were only going to allow the two decorators found during triage, but I think I actually like this approach better; we get a higher precision this way.
Rewrites queries to not rely on
pointsTo
analysis and instead use API graphs and dataflow where necessary.