Skip to content

Commit

Permalink
Use Iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 19, 2023
1 parent c2374d5 commit 73de55b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ isort, pyupgrade, and others. Regardless of the rule's origin, Ruff re-implement
Rust as a first-party feature.

By default, Ruff enables Flake8's `F` rules, along with a subset of the `E` rules, omitting any
stylistic rules that conflict with the use of a formatter, like [Black](https://github.com/psf/black).
stylistic rules that overlap with the use of a formatter, like
[Black](https://github.com/psf/black).

If you're just getting started with Ruff, **the default rule set is a great place to start**: it
catches a wide variety of common errors (like unused imports) with zero configuration.
Expand Down
37 changes: 19 additions & 18 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ numbers
Where `numbers.py` contains the following code:

```py
from typing import Sequence
from typing import Iterable

import os


def sum_even_numbers(numbers: Sequence[int]) -> int:
def sum_even_numbers(numbers: Iterable[int]) -> int:
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
return sum(num for num in numbers if num % 2 == 0)
```
Expand Down Expand Up @@ -55,12 +55,12 @@ Running `git diff` shows the following:
--- a/numbers/numbers.py
+++ b/numbers/numbers.py
@@ -1,7 +1,5 @@
from typing import Sequence
from typing import Iterable

-import os
-

def sum_even_numbers(numbers: Sequence[int]) -> int:
def sum_even_numbers(numbers: Iterable[int]) -> int:
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
return sum(num for num in numbers if num % 2 == 0)
```
Expand All @@ -78,7 +78,7 @@ Let's create a `pyproject.toml` file in our project's root directory:
```toml
[tool.ruff]
# Add the `line-too-long` rule to the enforced rule set. By default, Ruff omits rules that
# conflict with the use of a formatter, like Black, but we can override this behavior by
# overlap with the use of a formatter, like Black, but we can override this behavior by
# explicitly adding the rule.
extend-select = ["E501"]
# Set the maximum line length to 79 characters.
Expand Down Expand Up @@ -115,7 +115,8 @@ determining the right set of rules will depend on your project's needs: some rul
strict, some are framework-specific, and so on.

By default, Ruff enables Flake8's `F` rules, along with a subset of the `E` rules, omitting any
stylistic rules that conflict with the use of a formatter, like [Black](https://github.com/psf/black).
stylistic rules that overlap with the use of a formatter, like
[Black](https://github.com/psf/black).

If you're introducing a linter for the first time, **the default rule set is a great place to
start**: it's narrow and focused while catching a wide variety of common errors (like unused
Expand All @@ -136,11 +137,11 @@ extend-select = [
```

If we run Ruff again, we'll see that it now enforces the pyupgrade rules. In particular, Ruff flags
the use of the deprecated `typing.Sequence` instead of `collections.abc.Sequence`:
the use of the deprecated `typing.Iterable` instead of `collections.abc.Iterable`:

```shell
❯ ruff check .
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Sequence`
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
Found 1 error.
[*] 1 fixable with the `--fix` option.
```
Expand All @@ -167,7 +168,7 @@ If we run Ruff again, we'll see that it now enforces the pydocstyle rules:
```shell
❯ ruff check .
numbers/__init__.py:1:1: D104 Missing docstring in public package
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Sequence`
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
numbers/numbers.py:1:1: D100 Missing docstring in public module
Found 3 errors.
[*] 1 fixable with the `--fix` option.
Expand All @@ -176,18 +177,18 @@ Found 3 errors.
### Ignoring Errors

Any lint rule can be ignored by adding a `# noqa` comment to the line in question. For example,
let's ignore the `UP035` rule for the `Sequence` import:
let's ignore the `UP035` rule for the `Iterable` import:

```py
from typing import Sequence # noqa: UP035
from typing import Iterable # noqa: UP035


def sum_even_numbers(numbers: Sequence[int]) -> int:
def sum_even_numbers(numbers: Iterable[int]) -> int:
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
return sum(num for num in numbers if num % 2 == 0)
```

Running Ruff again, we'll see that it no longer flags the `Sequence` import:
Running Ruff again, we'll see that it no longer flags the `Iterable` import:

```shell
❯ ruff check .
Expand All @@ -201,10 +202,10 @@ the file:

```py
# ruff: noqa: UP035
from typing import Sequence
from typing import Iterable


def sum_even_numbers(numbers: Sequence[int]) -> int:
def sum_even_numbers(numbers: Iterable[int]) -> int:
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
return sum(num for num in numbers if num % 2 == 0)
```
Expand Down Expand Up @@ -233,11 +234,11 @@ index b9291c5ca..b9f15b8c1 100644
--- a/numbers/numbers.py
+++ b/numbers/numbers.py
@@ -1,4 +1,4 @@
-from typing import Sequence
+from typing import Sequence # noqa: UP035
-from typing import Iterable
+from typing import Iterable # noqa: UP035


def sum_even_numbers(numbers: Sequence[int]) -> int:
def sum_even_numbers(numbers: Iterable[int]) -> int:
```

## Continuous Integration
Expand Down
1 change: 0 additions & 1 deletion scripts/generate_mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Section(NamedTuple):
),
"https://docs.astral.sh/ruff/installation/": "installation.md",
"https://docs.astral.sh/ruff/rules/": "rules.md",
"https://docs.astral.sh/ruff/rules/#error-e": "rules.md#error-e",
"https://docs.astral.sh/ruff/settings/": "settings.md",
}

Expand Down

0 comments on commit 73de55b

Please sign in to comment.