From 4bd53c1395493b09683961db733c70ad30291f62 Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Thu, 13 Jun 2024 23:37:17 +1000 Subject: [PATCH] Update docs for E711, E712 (#4560) 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, plus a link to a GH issue with more detail. The sections are identical for each rule. - Minor formatting tweak for E711's docs. --- .../pycodestyle/rules/literal_comparisons.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs index 25bad88d50d52..c5117544f0427 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/literal_comparisons.rs @@ -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 @@ -48,7 +48,15 @@ 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 `==`/`!=`. For more +/// information, see [this issue]. +/// /// [PEP 8]: https://peps.python.org/pep-0008/#programming-recommendations +/// [this issue]: https://github.com/astral-sh/ruff/issues/4560 #[violation] pub struct NoneComparison(EqCmpOp); @@ -101,7 +109,15 @@ 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 `==`/`!=`. For more +/// information, see [this issue]. +/// /// [PEP 8]: https://peps.python.org/pep-0008/#programming-recommendations +/// [this issue]: https://github.com/astral-sh/ruff/issues/4560 #[violation] pub struct TrueFalseComparison { value: bool,