From a276f721f72dedf9afe754463af88055eab36179 Mon Sep 17 00:00:00 2001 From: Taus Date: Mon, 23 Mar 2026 15:21:27 +0000 Subject: [PATCH] Python: Add ternary `overridesMethod` This one also allows easy access to the method being overridden and the class on which it resides. This let's us simplify DocStrings.ql accordingly. --- .../dataflow/new/internal/DataFlowDispatch.qll | 14 ++++++++++++-- python/ql/src/Statements/DocStrings.ql | 4 +--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll b/python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll index abdf03daa13..d73b0cfabb6 100644 --- a/python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll +++ b/python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll @@ -2135,8 +2135,18 @@ module DuckTyping { /** * Holds if `f` overrides a method in a superclass with the same name. */ - predicate overridesMethod(Function f) { - exists(Class cls | f.getScope() = cls | hasMethod(getADirectSuperclass(cls), f.getName())) + predicate overridesMethod(Function f) { overridesMethod(f, _, _) } + + /** + * Holds if `f` overrides `overridden` declared in `superclass`. + */ + predicate overridesMethod(Function f, Class superclass, Function overridden) { + exists(Class cls | + f.getScope() = cls and + superclass = getADirectSuperclass+(cls) and + overridden = superclass.getAMethod() and + overridden.getName() = f.getName() + ) } /** diff --git a/python/ql/src/Statements/DocStrings.ql b/python/ql/src/Statements/DocStrings.ql index f71b204c018..d1f8c07abba 100644 --- a/python/ql/src/Statements/DocStrings.ql +++ b/python/ql/src/Statements/DocStrings.ql @@ -30,9 +30,7 @@ predicate needs_docstring(Scope s) { predicate function_needs_docstring(FunctionMetrics f) { not exists(Function base | - DuckTyping::overridesMethod(f) and - base.getScope() = getADirectSuperclass+(f.getScope()) and - base.getName() = f.getName() and + DuckTyping::overridesMethod(f, _, base) and not function_needs_docstring(base) ) and f.getName() != "lambda" and