Skip to content

Commit d40e9d3

Browse files
Sourcery refactored main branch (#201)
* ♻️ Refactored by Sourcery * 🧹 Run precommit on changes and fix typing * ♻️⬆️ Use future annotations Co-authored-by: Sourcery AI <> Co-authored-by: s-weigand <s.weigand.phy@gmail.com>
1 parent bac52ee commit d40e9d3

6 files changed

Lines changed: 85 additions & 91 deletions

File tree

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ repos:
1919
hooks:
2020
- id: mypy
2121
exclude: "^(docs|tests|.github)"
22+
additional_dependencies: [types-all]
2223

2324
- repo: https://github.com/MarcoGorelli/absolufy-imports
2425
rev: v0.3.1
@@ -31,7 +32,7 @@ repos:
3132
rev: v2.31.0
3233
hooks:
3334
- id: pyupgrade
34-
args: [--py36-plus]
35+
args: [--py37-plus]
3536
- repo: https://github.com/python/black
3637
rev: 22.1.0
3738
hooks:

flake8_nb/__main__.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Command-line implementation of flake8_nb."""
2+
from __future__ import annotations
3+
24
import sys
3-
from typing import List
4-
from typing import Optional
55

66
from flake8_nb import FLAKE8_VERSION_TUPLE
77
from flake8_nb.flake8_integration.cli import Flake8NbApplication
88

99

10-
def main(argv: Optional[List[str]] = None) -> None:
10+
def main(argv: list[str] | None = None) -> None:
1111
"""Execute the main bit of the application.
1212
1313
This handles the creation of an instance of :class:`Application`, runs it,
@@ -16,18 +16,11 @@ def main(argv: Optional[List[str]] = None) -> None:
1616
1717
Parameters
1818
----------
19-
argv: List[str], optional
19+
argv: list[str] | None
2020
The arguments to be passed to the application for parsing.
2121
"""
22-
# TODO: remove compat after flake8>3.8.0 release
23-
# tested with flake8-nightly, in test__main__.py,
24-
# but not picked up by by coverage
25-
if FLAKE8_VERSION_TUPLE > (3, 7, 9): # pragma: no cover
26-
if argv is None:
27-
argv = sys.argv[1:]
28-
else:
29-
argv = argv[1:]
30-
22+
if FLAKE8_VERSION_TUPLE > (3, 7, 9):
23+
argv = sys.argv[1:] if argv is None else argv[1:]
3124
app = Flake8NbApplication()
3225
app.run(argv)
3326
app.exit()

flake8_nb/flake8_integration/cli.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
``*.ipynb`` files and injecting the parsed files, during the loading
66
of the CLI argv and config of ``flake8``.
77
"""
8+
from __future__ import annotations
89

910
import logging
1011
import os
1112
import sys
1213
from typing import Any
1314
from typing import Callable
14-
from typing import List
15-
from typing import Optional
16-
from typing import Tuple
1715

1816
from flake8 import __version__ as flake_version
1917
from flake8 import defaults
@@ -33,35 +31,35 @@
3331

3432

3533
def get_notebooks_from_args(
36-
args: List[str], exclude: List[str] = ["*.tox/*", "*.ipynb_checkpoints*"]
37-
) -> Tuple[List[str], List[str]]:
34+
args: list[str], exclude: list[str] = ["*.tox/*", "*.ipynb_checkpoints*"]
35+
) -> tuple[list[str], list[str]]:
3836
"""Extract the absolute paths to notebooks.
3937
4038
The paths are relative to the current directory or
4139
to the CLI passes files/folder and returned as list.
4240
4341
Parameters
4442
----------
45-
args : List[str]
43+
args : list[str]
4644
The left over arguments that were not parsed by :attr:`option_manager`
47-
exclude : List[str], optional
45+
exclude : list[str]
4846
File-/Folderpatterns that should be excluded,
4947
by default ["*.tox/*", "*.ipynb_checkpoints*"]
5048
5149
Returns
5250
-------
53-
Tuple[List[str],List[str]]
51+
tuple[list[str], list[str]]
5452
List of found notebooks absolute paths.
5553
"""
5654

57-
def is_notebook(file_path: str, nb_list: List[str], root: str = ".") -> bool:
55+
def is_notebook(file_path: str, nb_list: list[str], root: str = ".") -> bool:
5856
"""Check if a file is a notebook and appends it to nb_list if it is.
5957
6058
Parameters
6159
----------
6260
file_path : str
6361
File to check if it is a notebook
64-
nb_list : List[str]
62+
nb_list : list[str]
6563
List of notebooks
6664
root : str
6765
Root directory, by default "."
@@ -77,7 +75,7 @@ def is_notebook(file_path: str, nb_list: List[str], root: str = ".") -> bool:
7775
return True
7876
return False
7977

80-
nb_list: List[str] = []
78+
nb_list: list[str] = []
8179
if not args:
8280
args = [os.curdir]
8381
for index, arg in list(enumerate(args))[::-1]:
@@ -102,12 +100,12 @@ def hack_option_manager_generate_versions(
102100
103101
Parameters
104102
----------
105-
generate_versions : Callable
103+
generate_versions : Callable[..., str]
106104
option_manager.generate_versions of flake8.options.manager.OptionManager
107105
108106
Returns
109107
-------
110-
Callable
108+
Callable[..., str]
111109
hacked_generate_versions
112110
"""
113111

@@ -128,12 +126,11 @@ def hacked_generate_versions(*args: Any, **kwargs: Any) -> str:
128126
"""
129127
original_output = generate_versions(*args, **kwargs)
130128
format_str = "%(name)s: %(version)s"
131-
join_on = ", "
132129
additional_output = format_str % {
133130
"name": "flake8",
134131
"version": flake_version,
135132
}
136-
return f"{additional_output}{join_on}{original_output}"
133+
return f"{additional_output}, {original_output}"
137134

138135
return hacked_generate_versions
139136

@@ -257,7 +254,7 @@ def hack_options(self) -> None:
257254
)
258255

259256
@staticmethod
260-
def hack_args(args: List[str], exclude: List[str]) -> List[str]:
257+
def hack_args(args: list[str], exclude: list[str]) -> list[str]:
261258
r"""Update args with ``*.ipynb`` files.
262259
263260
Checks the passed args if ``*.ipynb`` can be found and
@@ -266,14 +263,14 @@ def hack_args(args: List[str], exclude: List[str]) -> List[str]:
266263
267264
Parameters
268265
----------
269-
args : List[str]
266+
args : list[str]
270267
List of commandline arguments provided to ``flake8_nb``
271-
exclude : List[str]
268+
exclude : list[str]
272269
File-/Folderpatterns that should be excluded
273270
274271
Returns
275272
-------
276-
List[str]
273+
list[str]
277274
The original args + intermediate parsed ``*.ipynb`` files.
278275
"""
279276
args, nb_list = get_notebooks_from_args(args, exclude=exclude)
@@ -282,7 +279,7 @@ def hack_args(args: List[str], exclude: List[str]) -> List[str]:
282279

283280
def parse_configuration_and_cli_legacy(
284281
self,
285-
argv: Optional[List[str]] = None,
282+
argv: list[str] | None = None,
286283
) -> None:
287284
"""Compat version of self.parse_configuration_and_cli to work with flake8 >=3.7.0,<= 3.7.9 .
288285
@@ -292,7 +289,7 @@ def parse_configuration_and_cli_legacy(
292289
293290
Parameters
294291
----------
295-
argv: List[str]
292+
argv: list[str] | None
296293
Command-line arguments passed in directly.
297294
"""
298295
if self.options is None and self.args is None: # type: ignore # pragma: no branch
@@ -315,15 +312,15 @@ def parse_configuration_and_cli_legacy(
315312
self.formatting_plugins.provide_options(self.option_manager, self.options, self.args)
316313

317314
def parse_configuration_and_cli(
318-
self, config_finder: config.ConfigFileFinder, argv: List[str]
315+
self, config_finder: config.ConfigFileFinder, argv: list[str]
319316
) -> None:
320317
"""Parse configuration files and the CLI options.
321318
322319
Parameters
323320
----------
324321
config_finder: config.ConfigFileFinder
325322
The finder for finding and reading configuration files.
326-
argv: List[str]
323+
argv: list[str]
327324
Command-line arguments passed in directly.
328325
"""
329326
self.options, self.args = aggregator.aggregate_options(

flake8_nb/flake8_integration/formatter.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
original notebook and the cell the code in.
55
"""
66

7+
from __future__ import annotations
8+
79
import os
8-
from typing import Tuple
9-
from typing import Union
10+
from typing import cast
1011

1112
from flake8.formatting.default import Default
1213
from flake8.style_guide import Violation
@@ -15,7 +16,7 @@
1516
from flake8_nb.parsers.notebook_parsers import map_intermediate_to_input
1617

1718

18-
def map_notebook_error(violation: Violation, format_str: str) -> Union[Tuple[str, int], None]:
19+
def map_notebook_error(violation: Violation, format_str: str) -> tuple[str, int] | None:
1920
"""Map the violation caused in an intermediate file back to its cause.
2021
2122
The cause is resolved as the notebook, the input cell and
@@ -30,7 +31,7 @@ def map_notebook_error(violation: Violation, format_str: str) -> Union[Tuple[str
3031
3132
Returns
3233
-------
33-
Tuple[str, int]
34+
tuple[str, int] | None
3435
(filename, input_cell_line_number)
3536
``filename`` being the name of the original notebook and
3637
the input cell were the violation was reported.
@@ -69,7 +70,7 @@ def after_init(self) -> None:
6970
if self.options.format.lower() != "default_notebook":
7071
self.error_format = self.options.format
7172

72-
def format(self, violation: Violation) -> Union[str, None]:
73+
def format(self, violation: Violation) -> str | None:
7374
r"""Format the error detected by a flake8 checker.
7475
7576
Depending on if the violation was caused by a ``*.py`` file
@@ -82,7 +83,7 @@ def format(self, violation: Violation) -> Union[str, None]:
8283
8384
Returns
8485
-------
85-
str
86+
str | None
8687
Formatted error message, which will be displayed
8788
in the terminal.
8889
"""
@@ -91,13 +92,15 @@ def format(self, violation: Violation) -> Union[str, None]:
9192
map_result = map_notebook_error(violation, self.options.notebook_cell_format)
9293
if map_result:
9394
filename, line_number = map_result
94-
notebook_error: str = self.error_format % {
95-
"code": violation.code,
96-
"text": violation.text,
97-
"path": filename,
98-
"row": line_number,
99-
"col": violation.column_number,
100-
}
101-
return notebook_error
102-
default_error: Union[str, None] = super().format(violation)
103-
return default_error
95+
return cast(
96+
str,
97+
self.error_format
98+
% {
99+
"code": violation.code,
100+
"text": violation.text,
101+
"path": filename,
102+
"row": line_number,
103+
"col": violation.column_number,
104+
},
105+
)
106+
return cast(str, super().format(violation))

0 commit comments

Comments
 (0)