Skip to content

Commit a04aa30

Browse files
[pre-commit.ci] pre-commit autoupdate (#333)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.11.4 → v0.12.2](astral-sh/ruff-pre-commit@v0.11.4...v0.12.2) - [github.com/google/yamlfmt: v0.16.0 → v0.17.2](google/yamlfmt@v0.16.0...v0.17.2) * Add `project.requires-python` instead of `target-version` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix ruff linting errors after targeting Python 3.10 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Edoardo Baldi <edoardob90@gmail.com>
1 parent 5ca8d8d commit a04aa30

17 files changed

+160
-148
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: check-added-large-files
1212
args: [--maxkb=6000]
1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.11.4
14+
rev: v0.12.2
1515
hooks:
1616
# Ruff fix
1717
- id: ruff
@@ -23,7 +23,7 @@ repos:
2323
types_or: [python, pyi]
2424
name: ruff (format)
2525
- repo: https://github.com/google/yamlfmt
26-
rev: v0.16.0
26+
rev: v0.17.2
2727
hooks:
2828
- id: yamlfmt
2929
name: YAML (format)

pyproject.toml

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
[project]
2+
name = "python-tutorial"
3+
requires-python = ">=3.10"
4+
5+
# pytest
6+
[tool.pytest.ini_options]
7+
addopts = "-v --tb=short"
8+
19
[tool.ruff]
210
# Line length (Black default)
311
line-length = 88
412

5-
# Python target version
6-
target-version = "py38"
7-
813
# Ignored rules for the entire project
914
[tool.ruff.lint]
1015
ignore = [
11-
"E501", # Line too long
12-
"E203", # Whitespace before ':'
13-
# "TRY301", # Raise within try block (this is actually a good practice)
14-
# "W503" # Line break before binary operator (not PEP8 enforced, so not implemented in Ruff)
16+
"E501", # Line too long
17+
"E203", # Whitespace before ':'
18+
# "TRY301", # Raise within try block (this is actually a good practice)
19+
# "W503" # Line break before binary operator (not PEP8 enforced, so not implemented in Ruff)
1520
]
16-
1721
# flake8 plugins to enable:
1822
# - flake8-bugbear B
1923
# - flake8-builtins A
@@ -24,39 +28,33 @@ ignore = [
2428
# - pyflakes F
2529
# - tryceratops TRY
2630
select = [
27-
"A", # flake8-builtins
28-
"B", # flake8-bugbear
29-
"C4", # flake8-comprehensions
30-
"T10", # flake8-debugger
31-
"G", # flake8-logging-format
32-
"N", # pep8-naming
33-
"F", # pyflakes
34-
"TRY", # tryceratops
35-
"I", # isort
36-
"E", # pycodestyle errors
37-
"UP", # pyupgrade
31+
"A", # flake8-builtins
32+
"B", # flake8-bugbear
33+
"C4", # flake8-comprehensions
34+
"T10", # flake8-debugger
35+
"G", # flake8-logging-format
36+
"N", # pep8-naming
37+
"F", # pyflakes
38+
"TRY", # tryceratops
39+
"I", # isort
40+
"E", # pycodestyle errors
41+
"UP", # pyupgrade
3842
]
3943

4044
# Per-file rule ignores
4145
[tool.ruff.lint.per-file-ignores]
4246
# Trailing whitespace in comment
4347
"binder/ipython_config.py" = ["E266"]
44-
4548
# suppress `raise ... from err`
4649
# Why we ignore B904 from the object-oriented tests?
4750
# We do want to raise an assertion error if the check on the solution function attributes fails,
4851
# but Python by default will raise a TypeError via vars(solution_result)
4952
# if the result is not a class and therefore doesn't have a __dict__ attribute.
5053
"tutorial/tests/test_object_oriented_programming.py" = ["B904"]
51-
5254
# Ignore invalid names like `import torch.nn.functional as F`
5355
"25_library_pytorch_language_modeling.ipynb" = ["N812"]
5456

5557
# Ruff formatting
5658
[tool.ruff.format]
5759
quote-style = "double"
5860
indent-style = "space"
59-
60-
# pytest
61-
[tool.pytest.ini_options]
62-
addopts = "-v --tb=short"

tutorial/pandas_helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def low_med_high_bins_viz(data, column, ylabel, title, figsize=(15, 3)):
1414
["low", "med", "high"],
1515
["///", "", "\\\\\\"],
1616
pd.cut(data[column], bins=3).unique().categories.values,
17+
strict=False,
1718
):
1819
plt.axhspan(
1920
bounds.left,
@@ -45,6 +46,7 @@ def quartile_bins_viz(data, column, ylabel, title, figsize=(15, 8)):
4546
[r"$Q_1$", r"$Q_2$", r"$Q_3$", r"$Q_4$"],
4647
["\\\\\\", "", "///", "||||"],
4748
pd.qcut(data.volume, q=4).unique().categories.values,
49+
strict=False,
4850
):
4951
plt.axhspan(
5052
bounds.left,

tutorial/tests/test_01_basic_datatypes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import math
3-
from typing import Any, Callable, Hashable, Iterable
3+
from collections.abc import Callable, Hashable, Iterable
4+
from typing import Any
45

56
import pytest
67

@@ -364,7 +365,7 @@ def reference_dict_return_value(my_dict: dict[Hashable, Any], key: Any) -> Any:
364365

365366

366367
@pytest.mark.parametrize(
367-
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1)))
368+
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1), strict=False))
368369
)
369370
def test_dict_return_value(my_dict, key, function_to_test):
370371
my_dict_to_try = my_dict.copy()
@@ -380,7 +381,7 @@ def reference_dict_return_delete_value(my_dict: dict[Hashable, Any], key: Any) -
380381

381382

382383
@pytest.mark.parametrize(
383-
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1)))
384+
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1), strict=False))
384385
)
385386
def test_dict_return_delete_value(my_dict, key, function_to_test):
386387
my_dict_original1 = my_dict.copy()
@@ -400,7 +401,8 @@ def reference_update_one_dict_with_another(
400401

401402

402403
@pytest.mark.parametrize(
403-
"my_dict1, my_dict2", list(zip(copy.deepcopy(DICTS1), copy.deepcopy(DICTS2)))
404+
"my_dict1, my_dict2",
405+
list(zip(copy.deepcopy(DICTS1), copy.deepcopy(DICTS2), strict=False)),
404406
)
405407
def test_update_one_dict_with_another(my_dict1, my_dict2, function_to_test):
406408
my_dict1_original1 = my_dict1.copy()

tutorial/tests/test_02_control_flow.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import contextlib
22
import pathlib
33
from math import isclose, sqrt
4-
from typing import Any, List, Optional, Tuple
4+
from typing import Any
55

66
import pytest
77

@@ -16,7 +16,7 @@ def read_data(name: str, data_dir: str = "data") -> pathlib.Path:
1616
#
1717

1818

19-
def reference_indexed_string(string: str) -> List[Tuple[str, int]]:
19+
def reference_indexed_string(string: str) -> list[tuple[str, int]]:
2020
"""Reference solution warm-up 1"""
2121
result = []
2222
for i, char in enumerate(string):
@@ -45,7 +45,7 @@ def test_indexed_string(string: str, function_to_test) -> None:
4545
assert reference_indexed_string(string) == result
4646

4747

48-
def reference_range_of_nums(start: int, end: int) -> List[int]:
48+
def reference_range_of_nums(start: int, end: int) -> list[int]:
4949
"""Reference solution warm-up 2"""
5050
step = 1 if start < end else -1
5151
return list(range(start, end + step, step))
@@ -67,7 +67,7 @@ def test_range_of_nums(start: int, end: int, function_to_test) -> None:
6767
)
6868

6969

70-
def reference_sqrt_of_nums(numbers: List[int]) -> List[float]:
70+
def reference_sqrt_of_nums(numbers: list[int]) -> list[float]:
7171
"""Reference solution warm-up 3"""
7272
result = []
7373
for num in numbers:
@@ -104,7 +104,7 @@ def test_sqrt_of_nums(nums: list[int], function_to_test) -> None:
104104
assert len(reference) == len(result), (
105105
"The function should return a list of the same length"
106106
)
107-
assert all(isclose(x, y) for x, y in zip(reference, result)), (
107+
assert all(isclose(x, y) for x, y in zip(reference, result, strict=False)), (
108108
"The function should return the square root of each number"
109109
)
110110

@@ -128,7 +128,7 @@ def test_divide_until(num: int, function_to_test) -> None:
128128
#
129129

130130

131-
def reference_filter_by_position(numbers: List[int]) -> List[int]:
131+
def reference_filter_by_position(numbers: list[int]) -> list[int]:
132132
result = set()
133133
for pos, number in enumerate(numbers, start=1):
134134
if number > pos:
@@ -149,7 +149,7 @@ def reference_filter_by_position(numbers: List[int]) -> List[int]:
149149
[10, 20, 1, 2, 3], # Mixed large and small numbers = {10, 20}
150150
],
151151
)
152-
def test_filter_by_position(numbers: List[int], function_to_test) -> None:
152+
def test_filter_by_position(numbers: list[int], function_to_test) -> None:
153153
"""Test filtering numbers by position."""
154154
assert function_to_test(numbers) == reference_filter_by_position(numbers)
155155

@@ -159,7 +159,7 @@ def test_filter_by_position(numbers: List[int], function_to_test) -> None:
159159
#
160160

161161

162-
def reference_find_even_multiple_three(numbers: List[int]) -> Optional[int]:
162+
def reference_find_even_multiple_three(numbers: list[int]) -> int | None:
163163
result = None
164164
for number in numbers:
165165
if number % 2 == 0 and number % 3 == 0:
@@ -179,7 +179,7 @@ def reference_find_even_multiple_three(numbers: List[int]) -> Optional[int]:
179179
[1, 3, 5, 7, 12], # Valid number at the end = 12
180180
],
181181
)
182-
def test_find_even_multiple_three(numbers: List[int], function_to_test) -> None:
182+
def test_find_even_multiple_three(numbers: list[int], function_to_test) -> None:
183183
"""Test finding first even multiple of 3."""
184184
assert function_to_test(numbers) == reference_find_even_multiple_three(numbers)
185185

@@ -233,7 +233,7 @@ def test_is_pure_number(text: str, function_to_test) -> None:
233233
#
234234

235235

236-
def reference_find_factors(num: int) -> List[int]:
236+
def reference_find_factors(num: int) -> list[int]:
237237
"""Reference solution to find the factors of an integer"""
238238
factors = []
239239
for m in range(1, num + 1):
@@ -257,21 +257,21 @@ def test_find_factors(num: int, function_to_test) -> None:
257257
)
258258

259259

260-
def reference_find_pair(nums: List[int]):
260+
def reference_find_pair(nums: list[int]):
261261
"""
262262
Reference solutions:
263263
- A solution with two nested loops
264264
- A solution using a dictionary and a single loop
265265
"""
266266

267-
def find_pair_with_double_loop(nums: List[int]) -> Optional[int]:
267+
def find_pair_with_double_loop(nums: list[int]) -> int | None:
268268
"""Two nested loops"""
269269
for i in nums:
270270
for j in nums:
271271
if i + j == 2020:
272272
return i * j
273273

274-
def find_pair_with_sets(nums: List[int]) -> Optional[int]:
274+
def find_pair_with_sets(nums: list[int]) -> int | None:
275275
"""Using a dictionary and a single loop"""
276276
complements = {}
277277
for num in nums:
@@ -280,7 +280,7 @@ def find_pair_with_sets(nums: List[int]) -> Optional[int]:
280280
complements[2020 - num] = num
281281

282282

283-
def __reference_find_pair(nums: List[int]) -> Optional[int]:
283+
def __reference_find_pair(nums: list[int]) -> int | None:
284284
"""Reference solution (part 1)"""
285285
complements = {}
286286
for num in nums:
@@ -290,18 +290,18 @@ def __reference_find_pair(nums: List[int]) -> Optional[int]:
290290

291291

292292
@pytest.mark.parametrize("nums", [nums_1, nums_2])
293-
def test_find_pair(nums: List[int], function_to_test) -> None:
293+
def test_find_pair(nums: list[int], function_to_test) -> None:
294294
assert function_to_test(nums) == __reference_find_pair(nums)
295295

296296

297-
def reference_find_triplet(nums: List[int]):
297+
def reference_find_triplet(nums: list[int]):
298298
"""
299299
Reference solutions:
300300
- A slow solution with three nested loops
301301
- A fast solution using only two loops
302302
"""
303303

304-
def find_triplet_slow(nums: List[int]) -> Optional[int]:
304+
def find_triplet_slow(nums: list[int]) -> int | None:
305305
"""Slow solution with a triple loop"""
306306
n = len(nums)
307307
for i in range(n - 2):
@@ -310,7 +310,7 @@ def find_triplet_slow(nums: List[int]) -> Optional[int]:
310310
if nums[i] + nums[j] + nums[k] == 2020:
311311
return nums[i] * nums[j] * nums[k]
312312

313-
def find_triplet_best(nums: List[int]) -> Optional[int]:
313+
def find_triplet_best(nums: list[int]) -> int | None:
314314
"""Fast solution with two loops"""
315315
n = len(nums)
316316
for i in range(n - 1):
@@ -323,7 +323,7 @@ def find_triplet_best(nums: List[int]) -> Optional[int]:
323323
s.add(nums[j])
324324

325325

326-
def __reference_find_triplet(nums: List[int]) -> Optional[int]:
326+
def __reference_find_triplet(nums: list[int]) -> int | None:
327327
"""Reference solution (part 2), O(n^2)"""
328328
n = len(nums)
329329
for i in range(n - 1):
@@ -337,7 +337,7 @@ def __reference_find_triplet(nums: List[int]) -> Optional[int]:
337337

338338

339339
@pytest.mark.parametrize("nums", [nums_1, nums_2])
340-
def test_find_triplet(nums: List[int], function_to_test) -> None:
340+
def test_find_triplet(nums: list[int], function_to_test) -> None:
341341
assert function_to_test(nums) == __reference_find_triplet(nums)
342342

343343

tutorial/tests/test_03_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pathlib
33
from collections import Counter
44
from string import ascii_lowercase, ascii_uppercase
5-
from typing import Any, List
5+
from typing import Any
66

77
import pytest
88

@@ -270,7 +270,7 @@ def test_combine_anything(args: Any, function_to_test) -> None:
270270
#
271271

272272

273-
def reference_longest_sequence(nums: List[int]) -> int:
273+
def reference_longest_sequence(nums: list[int]) -> int:
274274
"""
275275
Find the longest consecutive sequence of integers
276276
@@ -301,7 +301,7 @@ def reference_longest_sequence(nums: List[int]) -> int:
301301
[0, 2, 14, 12, 4, 18, 16, 8, 10, 6],
302302
],
303303
)
304-
def test_longest_sequence(input_nums: List[int], function_to_test) -> None:
304+
def test_longest_sequence(input_nums: list[int], function_to_test) -> None:
305305
assert function_to_test(input_nums) == reference_longest_sequence(input_nums)
306306

307307

@@ -312,7 +312,7 @@ def test_longest_sequence(input_nums: List[int], function_to_test) -> None:
312312
[int(x) for x in read_data("longest_10000.txt").read_text().splitlines()],
313313
],
314314
)
315-
def test_longest_sequence_best(input_nums: List[int], function_to_test) -> None:
315+
def test_longest_sequence_best(input_nums: list[int], function_to_test) -> None:
316316
assert function_to_test(input_nums) == reference_longest_sequence(input_nums)
317317

318318

0 commit comments

Comments
 (0)