Skip to content

Commit

Permalink
Update docs for E711, E712 (astral-sh#4560)
Browse files Browse the repository at this point in the history
The fixes for rules E711 `NoneComparison` and E712 `TrueFalseComparison` are marked unsafe due to possible runtime behavior changes with libraries that override `__eq__` and `__ne__` methods.

- Add a "Fix safety" section to each rule explaining why the fixes are unsafe, commonly affected library methods, and alternatives. The sections are identical for each rule.
- Minor formatting tweak for E711's docs.
  • Loading branch information
psychedelicious committed Jun 13, 2024
1 parent c5bc368 commit 93819e2
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl EqCmpOp {
///
/// ## Why is this bad?
/// According to [PEP 8], "Comparisons to singletons like None should always be done with
/// is or is not, never the equality operators."
/// `is` or `is not`, never the equality operators."
///
/// ## Example
/// ```python
Expand All @@ -48,7 +48,22 @@ impl EqCmpOp {
/// pass
/// ```
///
/// ## Fix safety
///
/// This rule's fix is marked as unsafe, as it may alter runtime behavior when
/// used with libraries that override the `==`/`__eq__` or `!=`/`__ne__` operators.
/// In these cases, `is`/`is not` may not be equivalent to `==`/`!=`.
///
/// Notable examples and alternatives include:
/// - `pandas.DataFrame`, often used with `DataFrame.mask`, `DataFrame.where` - see [pandas.DataFrame.eq].
/// - `pandas.Series`, often used with `Series.mask`, `Series.where` - see [pandas.Series.eq].
/// - `sqlalchemy.Column`, often used with `Query.having`, `Query.filter`, `Query.where` - see [sqlalchemy.sql.expression.ColumnOperators.is_].
/// - `numpy.Array`, often used with `Array.where` - no alternative.
///
/// [PEP 8]: https://peps.python.org/pep-0008/#programming-recommendations
/// [pandas.Series.eq]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.eq.html
/// [pandas.DataFrame.eq]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.eq.html
/// [sqlalchemy.sql.expression.ColumnOperators.is_]: https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.ColumnOperators.is_
#[violation]
pub struct NoneComparison(EqCmpOp);

Expand Down Expand Up @@ -101,7 +116,22 @@ impl AlwaysFixableViolation for NoneComparison {
/// ...
/// ```
///
/// ## Fix safety
///
/// This rule's fix is marked as unsafe, as it may alter runtime behavior when
/// used with libraries that override the `==`/`__eq__` or `!=`/`__ne__` operators.
/// In these cases, `is`/`is not` may not be equivalent to `==`/`!=`.
///
/// Notable examples and alternatives include:
/// - `pandas.DataFrame`, often used with `DataFrame.mask`, `DataFrame.where` - see [pandas.DataFrame.eq].
/// - `pandas.Series`, often used with `Series.mask`, `Series.where` - see [pandas.Series.eq].
/// - `sqlalchemy.Column`, often used with `Query.having`, `Query.filter`, `Query.where` - see [sqlalchemy.sql.expression.ColumnOperators.is_].
/// - `numpy.Array`, often used with `Array.where` - no alternative.
///
/// [PEP 8]: https://peps.python.org/pep-0008/#programming-recommendations
/// [pandas.Series.eq]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.eq.html
/// [pandas.DataFrame.eq]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.eq.html
/// [sqlalchemy.sql.expression.ColumnOperators.is_]: https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.ColumnOperators.is_
#[violation]
pub struct TrueFalseComparison {
value: bool,
Expand Down

0 comments on commit 93819e2

Please sign in to comment.