mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Merge pull request #20052 from joefarebrother/python-qual-minor-doc-updates
Python: Minor documantation updates to several quality queries
This commit is contained in:
@@ -45,10 +45,10 @@ leaving <code>KeyboardInterrupt</code> to propagate.
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python Language Reference: <a href="http://docs.python.org/2.7/reference/compound_stmts.html#try">The try statement</a>,
|
||||
<a href="http://docs.python.org/2.7/reference/executionmodel.html#exceptions">Exceptions</a>.</li>
|
||||
<li>Python Language Reference: <a href="http://docs.python.org/3/reference/compound_stmts.html#try">The try statement</a>,
|
||||
<a href="http://docs.python.org/3/reference/executionmodel.html#exceptions">Exceptions</a>.</li>
|
||||
<li>M. Lutz, Learning Python, Section 35.3: Exception Design Tips and Gotchas, O'Reilly Media, 2013.</li>
|
||||
<li>Python Tutorial: <a href="https://docs.python.org/2/tutorial/errors.html">Errors and Exceptions</a>.</li>
|
||||
<li>Python Tutorial: <a href="https://docs.python.org/3/tutorial/errors.html">Errors and Exceptions</a>.</li>
|
||||
|
||||
|
||||
</references>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
The loss of information can lead to hard to debug errors and incomplete log files.
|
||||
It is even possible that ignoring an exception can cause a security vulnerability.
|
||||
An empty <code>except</code> block may be an indication that the programmer intended to
|
||||
handle the exception but never wrote the code to do so.</p>
|
||||
handle the exception, but never wrote the code to do so.</p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
@@ -15,7 +15,7 @@ handle the exception but never wrote the code to do so.</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>In this example the program keeps running with the same privileges if it fails to drop to lower
|
||||
<p>In this example, the program keeps running with the same privileges if it fails to drop to lower
|
||||
privileges.</p>
|
||||
<sample src="EmptyExcept.py" />
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ However, this may result in incorrect object initialization if the enclosing cla
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>
|
||||
In this example the call to <code>super(Vehicle, self)</code> in <code>Car.__init__</code> is incorrect as it
|
||||
In this example, the call to <code>super(Vehicle, self)</code> in <code>Car.__init__</code> is incorrect, as it
|
||||
passes <code>Vehicle</code> rather than <code>Car</code> as the first argument to <code>super</code>.
|
||||
As a result, <code>super(SportsCar, self).__init__()</code> in the <code>SportsCar.__init__</code> method will not call
|
||||
all <code>__init__()</code> methods because the call to <code>super(Vehicle, self).__init__()</code>
|
||||
@@ -37,7 +37,7 @@ skips <code>StatusSymbol.__init__()</code>.
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python Standard Library: <a href="https://docs.python.org/2/library/functions.html#super">super</a>.</li>
|
||||
<li>Python Standard Library: <a href="https://docs.python.org/3/library/functions.html#super">super</a>.</li>
|
||||
<li>Artima Developer: <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=236275">Things to Know About Python Super</a>.</li>
|
||||
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
dictionary = {1:"a", 2:"b", 2:"c"}
|
||||
print dictionary[2]
|
||||
dictionary = {1:"a", 2:"b", 2:"c"} # BAD: The `2` key is duplicated.
|
||||
print(dictionary[2])
|
||||
@@ -4,8 +4,8 @@
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>Dictionary literals are constructed in the order given in the source.
|
||||
This means that if a key is duplicated the second key-value pair will overwrite
|
||||
the first as a dictionary can only have one value per key.
|
||||
This means that if a key is duplicated, the second key-value pair will overwrite
|
||||
the first; as a dictionary can only have one value per key.
|
||||
</p>
|
||||
|
||||
</overview>
|
||||
@@ -15,14 +15,14 @@ If they are then decide which value is wanted and delete the other one.</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>This example will output "c" because the mapping between 2 and "b" is overwritten by the
|
||||
mapping from 2 to "c". The programmer may have meant to map 3 to "c" instead.</p>
|
||||
<p>The following example will output <code>"c"</code>, because the mapping between 2 and <code>"b"</code> is overwritten by the
|
||||
mapping from 2 to <code>"c"</code>. The programmer may have meant to map 3 to <code>"c"</code> instead.</p>
|
||||
<sample src="DuplicateKeyInDictionaryLiteral.py" />
|
||||
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python: <a href="http://docs.python.org/2/reference/expressions.html#dictionary-displays">Dictionary literals</a>.</li>
|
||||
<li>Python: <a href="http://docs.python.org/3/reference/expressions.html#dictionary-displays">Dictionary literals</a>.</li>
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
|
||||
@@ -17,7 +17,7 @@ wrap the use of the object in a <code>with</code> statement.
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>In the first example, rather than close the zip file in a conventional manner the programmer has called <code>__del__</code>.
|
||||
<p>In the first example, rather than close the zip file in a conventional manner, the programmer has called <code>__del__</code>.
|
||||
A safer alternative is shown in the second example.
|
||||
</p>
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ either of the alternatives below.
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python Standard Library: <a href="http://docs.python.org/2/library/stdtypes.html#comparisons">Comparisons</a>.</li>
|
||||
<li>Python Standard Library: <a href="http://docs.python.org/3/library/stdtypes.html#comparisons">Comparisons</a>.</li>
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* @name Comparison using is when operands support `__eq__`
|
||||
* @description Comparison using 'is' when equivalence is not the same as identity
|
||||
* @description Comparison using `is` when equivalence is not the same as identity
|
||||
* @kind problem
|
||||
* @tags quality
|
||||
* reliability
|
||||
|
||||
@@ -3,18 +3,19 @@
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>A format string, that is the string on the left hand side of an expression like <code>fmt % arguments</code>, must consist of legal conversion specifiers.
|
||||
<p>A printf-style format string (i.e. a string that is used as the left hand side of the <code>%</code> operator, such as <code>fmt % arguments</code>)
|
||||
must consist of valid conversion specifiers, such as <code>%s</code>, <code>%d</code>, etc.
|
||||
Otherwise, a <code>ValueError</code> will be raised.
|
||||
|
||||
</p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
<p>Choose a legal conversion specifier.</p>
|
||||
<p>Ensure a valid conversion specifier is used.</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
<p>In <code>format_as_tuple_incorrect</code>, "t" is not a legal conversion specifier.
|
||||
<p>In the following example, <code>format_as_tuple_incorrect</code>, <code>%t</code> is not a valid conversion specifier.
|
||||
|
||||
</p>
|
||||
<sample src="UnsupportedFormatCharacter.py" />
|
||||
@@ -22,7 +23,7 @@ Otherwise, a <code>ValueError</code> will be raised.
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python Library Reference: <a href="http://docs.python.org/library/stdtypes.html#string-formatting">String Formatting.</a> </li>
|
||||
<li>Python Library Reference: <a href="https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting">printf-style String Formatting.</a> </li>
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<overview>
|
||||
<p>When a function contains both explicit returns (<code>return value</code>) and implicit returns
|
||||
(where code falls off the end of a function) this often indicates that a return
|
||||
(where code falls off the end of a function), this often indicates that a return
|
||||
statement has been forgotten. It is best to return an explicit return value even when returning
|
||||
<code>None</code> because this makes it easier for other developers to read your code.
|
||||
</p>
|
||||
@@ -29,7 +29,7 @@ return value of <code>None</code> as this equates to <code>False</code>. However
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python Language Reference: <a href="http://docs.python.org/2/reference/compound_stmts.html#function">Function definitions</a>.
|
||||
<li>Python Language Reference: <a href="http://docs.python.org/3/reference/compound_stmts.html#function">Function definitions</a>.
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* @name Explicit returns mixed with implicit (fall through) returns
|
||||
* @description Mixing implicit and explicit returns indicates a likely error as implicit returns always return 'None'.
|
||||
* @description Mixing implicit and explicit returns indicates a likely error as implicit returns always return `None`.
|
||||
* @kind problem
|
||||
* @tags quality
|
||||
* reliability
|
||||
@@ -31,4 +31,4 @@ predicate has_implicit_return(Function func) {
|
||||
from Function func
|
||||
where explicitly_returns_non_none(func) and has_implicit_return(func)
|
||||
select func,
|
||||
"Mixing implicit and explicit returns may indicate an error as implicit returns always return None."
|
||||
"Mixing implicit and explicit returns may indicate an error, as implicit returns always return None."
|
||||
|
||||
@@ -22,7 +22,7 @@ not logical in the context of an initializer.</p>
|
||||
</example>
|
||||
<references>
|
||||
|
||||
<li>Python: <a href="http://docs.python.org/2.7/reference/datamodel.html#object.__init__">The __init__ method</a>.</li>
|
||||
<li>Python: <a href="http://docs.python.org/3/reference/datamodel.html#object.__init__">The __init__ method</a>.</li>
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
|
||||
@@ -37,7 +37,7 @@ function with a default of <code>default=None</code>, check if the parameter is
|
||||
<references>
|
||||
|
||||
<li>Effbot: <a href="https://web.archive.org/web/20201112004749/http://effbot.org/zone/default-values.htm">Default Parameter Values in Python</a>.</li>
|
||||
<li>Python Language Reference: <a href="http://docs.python.org/2/reference/compound_stmts.html#function-definitions">Function definitions</a>.</li>
|
||||
<li>Python Language Reference: <a href="http://docs.python.org/3/reference/compound_stmts.html#function-definitions">Function definitions</a>.</li>
|
||||
|
||||
|
||||
</references>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
| functions_test.py:18:1:18:11 | Function cr1 | Mixing implicit and explicit returns may indicate an error as implicit returns always return None. |
|
||||
| functions_test.py:22:1:22:11 | Function cr2 | Mixing implicit and explicit returns may indicate an error as implicit returns always return None. |
|
||||
| functions_test.py:336:1:336:16 | Function ok_match | Mixing implicit and explicit returns may indicate an error as implicit returns always return None. |
|
||||
| functions_test.py:344:1:344:17 | Function ok_match2 | Mixing implicit and explicit returns may indicate an error as implicit returns always return None. |
|
||||
| functions_test.py:18:1:18:11 | Function cr1 | Mixing implicit and explicit returns may indicate an error, as implicit returns always return None. |
|
||||
| functions_test.py:22:1:22:11 | Function cr2 | Mixing implicit and explicit returns may indicate an error, as implicit returns always return None. |
|
||||
| functions_test.py:336:1:336:16 | Function ok_match | Mixing implicit and explicit returns may indicate an error, as implicit returns always return None. |
|
||||
| functions_test.py:344:1:344:17 | Function ok_match2 | Mixing implicit and explicit returns may indicate an error, as implicit returns always return None. |
|
||||
|
||||
Reference in New Issue
Block a user