Skip to content

Conversation

@VascoSch92
Copy link
Contributor

@VascoSch92 VascoSch92 commented May 14, 2025

The PR add the fix safety section for rule SIM103 (#15584 )

Unsafe Fix Example

class Foo:
    def __eq__(self, other):
        return 1
    
def foo():
    if Foo() == 1:
        return True
    return False

def foo_fix():
    return Foo() == 1
    
print(foo()) # True
print(foo_fix()) # 1

Note

I updated the code snippet example, because I thought it was cool to have a correct example, i.e., that I can paste inside the playground and it works :-)

@VascoSch92 VascoSch92 mentioned this pull request May 14, 2025
71 tasks
@github-actions
Copy link
Contributor

github-actions bot commented May 14, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added the documentation Improvements or additions to documentation label May 14, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Great catch again on the __eq__ example too. Just one comment about comments and an idea for incorporating the findings from your example.

Comment on lines 51 to 52
/// This fix is marked as unsafe because it may change the program’s behavior if the condition does not
/// return a proper Boolean. Additionally, the fix could remove comments.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the part about comments is true. The code checks for comments in the fix range and avoids a fix in that case (although that happens way earlier in the code than the fix generation, so it's not that clear).

I wanted to say that the boolean condition part wasn't true either because we do try to add bool calls in cases where it doesn't look like a boolean, but your clever example does change the behavior. Do you think it's worth mentioning __eq__ or the other comparison methods that could cause problems? Maybe a second sentence like:

While the fix will try to wrap non-boolean values in a call to bool, custom implementations of comparison functions like __eq__ can avoid the bool call and still lead to altered behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the part about comments is true. The code checks for comments in the fix range and avoids a fix in that case (although that happens way earlier in the code than the fix generation, so it's not that clear).

You are right. I delete this part.

I wanted to say that the boolean condition part wasn't true either because we do try to add bool calls in cases where it doesn't look like a boolean, but your clever example does change the behavior. Do you think it's worth mentioning eq or the other comparison methods that could cause problems? Maybe a second sentence like:

Fine for me. I update the fix safety section.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you!

@ntBre ntBre merged commit 68559fc into astral-sh:main May 14, 2025
34 checks passed
dcreager added a commit that referenced this pull request May 14, 2025
…rals

* origin/main:
  [ty] Add type-expression syntax link to invalid-type-expression (#18104)
  [`flake8-simplify`] add fix safety section (`SIM103`) (#18086)
  [ty] mypy_primer: fix static-frame setup (#18103)
  [`flake8-simplify`] Correct behavior for `str.split`/`rsplit` with `maxsplit=0` (`SIM905`) (#18075)
  [ty] Fix more generics-related TODOs (#18062)
ntBre pushed a commit that referenced this pull request May 15, 2025
The PR add the `fix safety` section for rule `SIM210` (#15584 )

It is a little cheating, as the Fix safety section is copy/pasted by
#18086 as the problem is the same.

### Unsafe Fix Example

```python
class Foo():
    def __eq__(self, other):
        return 0

def foo():
    return True if Foo() == 0 else False

def foo_fix():
    return Foo() == 0

print(foo()) # False
print(foo_fix()) # 0
```
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
The PR add the `fix safety` section for rule `SIM103` (astral-sh#15584 )

### Unsafe Fix Example

```python
class Foo:
    def __eq__(self, other):
        return 1
    
def foo():
    if Foo() == 1:
        return True
    return False

def foo_fix():
    return Foo() == 1
    
print(foo()) # True
print(foo_fix()) # 1
```

### Note

I updated the code snippet example, because I thought it was cool to
have a correct example, i.e., that I can paste inside the playground and
it works :-)
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
The PR add the `fix safety` section for rule `SIM210` (astral-sh#15584 )

It is a little cheating, as the Fix safety section is copy/pasted by
astral-sh#18086 as the problem is the same.

### Unsafe Fix Example

```python
class Foo():
    def __eq__(self, other):
        return 0

def foo():
    return True if Foo() == 0 else False

def foo_fix():
    return Foo() == 0

print(foo()) # False
print(foo_fix()) # 0
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants