Files
codeql/python/ql/src/Classes/ShouldBeContextManager.ql
Taus 059693ce89 Python: Restrict ShouldBeContextManager.ql results
By limiting the results to the class that actually defines the `__del__`
method, we eliminate a bunch of FPs where a _subclass_ of such a class
would also get flagged.
2026-03-24 13:04:44 +00:00

26 lines
755 B
Plaintext

/**
* @name Class should be a context manager
* @description Making a class a context manager allows instances to be used in a 'with' statement.
* This improves resource handling and code readability.
* @kind problem
* @tags quality
* maintainability
* readability
* performance
* @problem.severity recommendation
* @sub-severity high
* @precision medium
* @id py/should-be-context-manager
*/
import python
private import semmle.python.dataflow.new.internal.DataFlowDispatch
from Class c
where
not DuckTyping::isContextManager(c) and
exists(c.getMethod("__del__"))
select c,
"Class " + c.getName() +
" implements __del__ (presumably to release some resource). Consider making it a context manager."