Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-bugbear] Consider replacing f"'{foo}'" with f"{foo!r}" which is both easier to read and will escape quotes inside foo if that would appear (B907) #13600

Closed
wants to merge 89 commits into from

Conversation

agpt8
Copy link

@agpt8 agpt8 commented Oct 2, 2024

B907: Consider replacing f"'{foo}'" with f"{foo!r}" which is both easier to read and will escape quotes inside foo if that would appear. The check tries to filter out any format specs that are invalid together with !r. If you’re using other conversion flags then e.g. f"'{foo!a}'" can be replaced with f"{ascii(foo)!r}". Not currently implemented for python<3.8 or str.format() calls.

Implement B907 flake8-bugbear rules in Ruff.

  • Add crates/ruff_linter/src/rules/flake8_bugbear/rules/b907.rs to implement B907 rule to replace f"'{foo}'" with f"{foo!r}".
  • Update crates/ruff_linter/src/rules/flake8_bugbear/mod.rs to include B907 rules.
  • Add test cases for B907 rules in crates/ruff_linter/src/rules/flake8_bugbear/tests.rs.

agpt8 and others added 22 commits October 2, 2024 15:27
Fixes #3758

Implement B903 and B907 flake8-bugbear rules in Ruff.

* Add `crates/ruff_linter/src/rules/flake8_bugbear/rules/b903.rs` to implement B903 rule for data classes that only set attributes in an `__init__` method.
* Add `crates/ruff_linter/src/rules/flake8_bugbear/rules/b907.rs` to implement B907 rule to replace f"'{foo}'" with f"{foo!r}".
* Update `crates/ruff_linter/src/rules/flake8_bugbear/mod.rs` to include B903 and B907 rules.
* Add test cases for B903 and B907 rules in `crates/ruff_linter/src/rules/flake8_bugbear/tests.rs`.
* **New Rules**
  - Add `UseDataclassesForDataClasses` variant to `Rule` enum
  - Add `FStringSingleQuotes` variant to `Rule` enum
* **Update `Rule` enum**
  - Add `UseDataclassesForDataClasses` for B903
  - Add `FStringSingleQuotes` for B907

* **Update imports in `mod.rs`**
  - Add `use_dataclasses_for_data_classes::*`
  - Add `f_string_single_quotes::*`
…collections.abc.AsyncGenerator` (#13611)

## Summary

fix #13602 

Currently, `UP043` only applies to typing.Generator, but it should also
support collections.abc.Generator.

This update ensures `UP043` correctly handles both
`collections.abc.Generator` and `collections.abc.AsyncGenerator`

### UP043
> `UP043`
> Python 3.13 introduced the ability for type parameters to specify
default values. As such, the default type arguments for some types in
the standard library (e.g., Generator, AsyncGenerator) are now optional.
> Omitting type parameters that match the default values can make the
code more concise and easier to read.

```py
Generator[int, None, None] -> Generator[int]
```
https://github.com/pre-commit/mirrors-prettier has been archived and is
no longer maintained.

Signed-off-by: Bernát Gábor <[email protected]>
Closes #13545

As described in the issue, we move comments before the inner `if`
statement to before the newly constructed `elif` statement (previously
`else`).
Resolves #13321.

Contents of overlay:
```bash
/private/var/folders/v0/l8q3ghks2gs5ns2_p63tyqh40000gq/T/pip-build-env-e0ukpbvo/overlay/bin:
total 26M
-rwxr-xr-x 1 bgabor8 staff 26M Oct  1 08:22 ruff
drwxr-xr-x 3 bgabor8 staff  96 Oct  1 08:22 .
drwxr-xr-x 4 bgabor8 staff 128 Oct  1 08:22 ..
```

Python executable:
```bash
'/Users/bgabor8/git/github/ruff-find-bin-during-build/.venv/bin/python'
```
PATH is:
```bash
['/private/var/folders/v0/l8q3ghks2gs5ns2_p63tyqh40000gq/T/pip-build-env-e0ukpbvo/overlay/bin',
 '/private/var/folders/v0/l8q3ghks2gs5ns2_p63tyqh40000gq/T/pip-build-env-e0ukpbvo/normal/bin',
'/Library/Frameworks/Python.framework/Versions/3.11/bin',
'/Library/Frameworks/Python.framework/Versions/3.12/bin',
```
Not sure where to add tests, there does not seem to be any existing one.
Can someone help me with that?
…13601)

## Summary
Resolves #9962 by allowing a
configuration setting `allowed-unused-imports`

TODO:
- [x] Figure out the correct name and place for the setting; currently,
I have added it top level.
- [x] The comparison is pretty naive. I tried using `glob::Pattern` but
couldn't get it to work in the configuration.
- [x] Add tests
- [x] Update documentations

## Test Plan

`cargo test`
@dhruvmanila
Copy link
Member

FYI, there's another implementation of B903 at #12259

@agpt8
Copy link
Author

agpt8 commented Oct 4, 2024

Thanks @dhruvmanila. I'll remove the B903 implementation and instead create a PR for B907

dhruvmanila and others added 6 commits October 4, 2024 18:51
…13616)

Closes #12871

Includes some minor semantic type inference extensions changes to help
with reliably detecting integers
## Summary

Implements the comparison operator for `[Type::IntLiteral]` and
`[Type::BooleanLiteral]` (as an artifact of special handling of `True` and
`False` in python).
Sets the framework to implement more comparison for types known at
static time (e.g. `BooleanLiteral`, `StringLiteral`), allowing us to only
implement cases of the triplet `<left> Type`, `<right> Type`, `CmpOp`.
Contributes to #12701 (without checking off an item yet).

## Test Plan

- Added a test for the comparison of literals that should include most
cases of note.
- Added a test for the comparison of int instances

Please note that the cases do not cover 100% of the branches as there
are many and the current testing strategy with variables make this
fairly confusing once we have too many in one test.

---------

Co-authored-by: Carl Meyer <[email protected]>
Co-authored-by: Alex Waygood <[email protected]>
Closes #13343

---------

Co-authored-by: Alex Waygood <[email protected]>
renovate bot and others added 11 commits October 14, 2024 07:39
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
## Summary

fixes: #13718 

## Test Plan

Using the notebook as mentioned in
#13718 (comment),
this PR does not give the "F821 Undefined name `test_sorted`"
diagnostic.
Implements inference for `BytesLiteral` comparisons along the lines of
#13634.

closes #13687

Co-authored-by: Alex Waygood <[email protected]>
…ument pitfalls of using `rstest` in combination with `mdtest` (#13747)
This is a follow-up on #13746:

- Use `memmem::find` instead of rolling our own inferior version.
- Avoid `x.as_ref()` calls using `&**x`
@agpt8 agpt8 changed the title Implement B903 and B907 flake8-bugbear rules [flake8-bugbear] Consider replacing f"'{foo}'" with f"{foo!r}" which is both easier to read and will escape quotes inside foo if that would appear (B907) Oct 14, 2024
Fixes #3758

Implement B903 and B907 flake8-bugbear rules in Ruff.

* Add `crates/ruff_linter/src/rules/flake8_bugbear/rules/b903.rs` to implement B903 rule for data classes that only set attributes in an `__init__` method.
* Add `crates/ruff_linter/src/rules/flake8_bugbear/rules/b907.rs` to implement B907 rule to replace f"'{foo}'" with f"{foo!r}".
* Update `crates/ruff_linter/src/rules/flake8_bugbear/mod.rs` to include B903 and B907 rules.
* Add test cases for B903 and B907 rules in `crates/ruff_linter/src/rules/flake8_bugbear/tests.rs`.
* **New Rules**
  - Add `UseDataclassesForDataClasses` variant to `Rule` enum
  - Add `FStringSingleQuotes` variant to `Rule` enum
* **Update `Rule` enum**
  - Add `UseDataclassesForDataClasses` for B903
  - Add `FStringSingleQuotes` for B907

* **Update imports in `mod.rs`**
  - Add `use_dataclasses_for_data_classes::*`
  - Add `f_string_single_quotes::*`
@agpt8 agpt8 closed this Oct 14, 2024
@agpt8 agpt8 deleted the implement-b903-b907 branch October 14, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.