Skip to content

Commit dd7a38c

Browse files
authored
MAINT: run pyupgrade for py39 (#135)
* MAINT: running pyupgrade for 3.9 * MAINT: bumping python version in pre-commit * MAINT: style cleanup * MAINT: remove old python
1 parent 457f67a commit dd7a38c

File tree

11 files changed

+68
-66
lines changed

11 files changed

+68
-66
lines changed

Diff for: .pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repos:
2323
rev: v3.19.1
2424
hooks:
2525
- id: pyupgrade
26-
args: [--py37-plus]
26+
args: [--py39-plus]
2727

2828
- repo: https://github.com/pycqa/isort
2929
rev: 6.0.0

Diff for: jupyter_cache/base.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"""
66

77
from abc import ABC, abstractmethod
8+
from collections.abc import Iterable, Mapping
89
import io
910
from pathlib import Path
10-
from typing import Iterable, List, Mapping, Optional, Tuple, Union
11+
from typing import Optional, Union
1112

1213
import attr
1314
from attr.validators import instance_of, optional
@@ -57,7 +58,7 @@ class ProjectNb:
5758
repr=lambda nb: f"Notebook(cells={len(nb.cells)})",
5859
metadata={"help": "the notebook"},
5960
)
60-
assets: List[Path] = attr.ib(
61+
assets: list[Path] = attr.ib(
6162
factory=list,
6263
metadata={"help": "File paths required to run the notebook"},
6364
)
@@ -68,11 +69,11 @@ class NbArtifactsAbstract(ABC):
6869

6970
@property
7071
@abstractmethod
71-
def relative_paths(self) -> List[Path]:
72+
def relative_paths(self) -> list[Path]:
7273
"""Return the list of paths (relative to the notebook folder)."""
7374

7475
@abstractmethod
75-
def __iter__(self) -> Iterable[Tuple[Path, io.BufferedReader]]:
76+
def __iter__(self) -> Iterable[tuple[Path, io.BufferedReader]]:
7677
"""Yield the relative path and open files (in bytes mode)"""
7778

7879
def __repr__(self):
@@ -165,7 +166,7 @@ def cache_notebook_file(
165166
self,
166167
path: str,
167168
uri: Optional[str] = None,
168-
artifacts: List[str] = (),
169+
artifacts: list[str] = (),
169170
data: Optional[dict] = None,
170171
check_validity: bool = True,
171172
overwrite: bool = False,
@@ -186,7 +187,7 @@ def cache_notebook_file(
186187
"""
187188

188189
@abstractmethod
189-
def list_cache_records(self) -> List[NbCacheRecord]:
190+
def list_cache_records(self) -> list[NbCacheRecord]:
190191
"""Return a list of cached notebook records."""
191192

192193
@abstractmethod
@@ -229,7 +230,7 @@ def merge_match_into_notebook(
229230
nb: nbf.NotebookNode,
230231
nb_meta=("kernelspec", "language_info", "widgets"),
231232
cell_meta=None,
232-
) -> Tuple[int, nbf.NotebookNode]:
233+
) -> tuple[int, nbf.NotebookNode]:
233234
"""Match to an executed notebook and return a merged version
234235
235236
:param nb: The input notebook
@@ -244,7 +245,7 @@ def merge_match_into_file(
244245
path: str,
245246
nb_meta=("kernelspec", "language_info", "widgets"),
246247
cell_meta=None,
247-
) -> Tuple[int, nbf.NotebookNode]:
248+
) -> tuple[int, nbf.NotebookNode]:
248249
"""Match to an executed notebook and return a merged version
249250
250251
:param path: The input notebook path
@@ -281,7 +282,7 @@ def add_nb_to_project(
281282
uri: str,
282283
*,
283284
read_data: Mapping = DEFAULT_READ_DATA,
284-
assets: List[str] = (),
285+
assets: list[str] = (),
285286
) -> NbProjectRecord:
286287
"""Add a single notebook to the project.
287288
@@ -298,9 +299,9 @@ def remove_nb_from_project(self, uri_or_pk: Union[int, str]):
298299
@abstractmethod
299300
def list_project_records(
300301
self,
301-
filter_uris: Optional[List[str]] = None,
302-
filter_pks: Optional[List[int]] = None,
303-
) -> List[NbProjectRecord]:
302+
filter_uris: Optional[list[str]] = None,
303+
filter_pks: Optional[list[int]] = None,
304+
) -> list[NbProjectRecord]:
304305
"""Return a list of all notebook records in the project."""
305306

306307
@abstractmethod
@@ -326,7 +327,7 @@ def get_cached_project_nb(
326327
@abstractmethod
327328
def list_unexecuted(
328329
self,
329-
filter_uris: Optional[List[str]] = None,
330-
filter_pks: Optional[List[int]] = None,
331-
) -> List[NbProjectRecord]:
330+
filter_uris: Optional[list[str]] = None,
331+
filter_pks: Optional[list[int]] = None,
332+
) -> list[NbProjectRecord]:
332333
"""List notebooks in the project, whose hash is not present in the cache."""

Diff for: jupyter_cache/cache/db.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import datetime
33
import os
44
from pathlib import Path
5-
from typing import Any, Dict, List, Optional, Union
5+
from typing import Any, Optional, Union
66

77
from sqlalchemy import JSON, Column, DateTime, Integer, String, Text
88
from sqlalchemy.engine import Engine, create_engine
@@ -203,7 +203,7 @@ def validate_assets(paths, uri=None):
203203
def create_record(
204204
uri: str,
205205
db: Engine,
206-
read_data: Dict[str, Any],
206+
read_data: dict[str, Any],
207207
raise_on_exists=True,
208208
*,
209209
assets=(),
@@ -222,14 +222,14 @@ def create_record(
222222
session.expunge(record)
223223
return record
224224

225-
def remove_pks(pks: List[int], db: Engine):
225+
def remove_pks(pks: list[int], db: Engine):
226226
with session_context(db) as session: # type: Session
227227
session.query(NbProjectRecord).filter(NbProjectRecord.pk.in_(pks)).delete(
228228
synchronize_session=False
229229
)
230230
session.commit()
231231

232-
def remove_uris(uris: List[str], db: Engine):
232+
def remove_uris(uris: list[str], db: Engine):
233233
with session_context(db) as session: # type: Session
234234
session.query(NbProjectRecord).filter(NbProjectRecord.uri.in_(uris)).delete(
235235
synchronize_session=False
@@ -339,7 +339,7 @@ def remove_record(pk: int, db: Engine):
339339
session.delete(record)
340340
session.commit()
341341

342-
def remove_records(pks: List[int], db: Engine):
342+
def remove_records(pks: list[int], db: Engine):
343343
with session_context(db) as session: # type: Session
344344
session.query(NbCacheRecord).filter(NbCacheRecord.pk.in_(pks)).delete(
345345
synchronize_session=False
@@ -400,7 +400,7 @@ def records_all(db: Engine) -> "NbCacheRecord":
400400
session.expunge_all()
401401
return results
402402

403-
def records_to_delete(keep: int, db: Engine) -> List[int]:
403+
def records_to_delete(keep: int, db: Engine) -> list[int]:
404404
"""Return pks of the oldest records, where keep is number to keep."""
405405
with session_context(db) as session: # type: Session
406406
pks_to_keep = [

Diff for: jupyter_cache/cache/main.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from collections.abc import Iterable, Mapping
12
from contextlib import contextmanager
23
import copy
34
import hashlib
45
import io
56
from pathlib import Path
67
import shutil
7-
from typing import Iterable, List, Mapping, Optional, Tuple, Union
8+
from typing import Optional, Union
89

910
import nbformat as nbf
1011

@@ -31,7 +32,7 @@
3132
class NbArtifacts(NbArtifactsAbstract):
3233
"""Container for artefacts of a notebook execution."""
3334

34-
def __init__(self, paths: List[str], in_folder, check_existence=True):
35+
def __init__(self, paths: list[str], in_folder, check_existence=True):
3536
"""Initiate NbArtifacts
3637
3738
:param paths: list of paths
@@ -44,11 +45,11 @@ def __init__(self, paths: List[str], in_folder, check_existence=True):
4445
to_relative_paths(self.paths, self.in_folder, check_existence=check_existence)
4546

4647
@property
47-
def relative_paths(self) -> List[Path]:
48+
def relative_paths(self) -> list[Path]:
4849
"""Return the list of paths (relative to the notebook folder)."""
4950
return to_relative_paths(self.paths, self.in_folder)
5051

51-
def __iter__(self) -> Iterable[Tuple[Path, io.BufferedReader]]:
52+
def __iter__(self) -> Iterable[tuple[Path, io.BufferedReader]]:
5253
"""Yield the relative path and open files (in bytes mode)"""
5354
for path in self.paths:
5455
with path.open("rb") as handle:
@@ -123,7 +124,7 @@ def create_hashed_notebook(
123124
nb: nbf.NotebookNode,
124125
nb_metadata: Optional[Iterable[str]] = ("kernelspec",),
125126
cell_metadata: Optional[Iterable[str]] = None,
126-
) -> Tuple[nbf.NotebookNode, str]:
127+
) -> tuple[nbf.NotebookNode, str]:
127128
"""Convert a notebook to a standard format and hash.
128129
129130
Note: we always hash notebooks as version 4.4,
@@ -254,7 +255,7 @@ def cache_notebook_file(
254255
self,
255256
path: str,
256257
uri: Optional[str] = None,
257-
artifacts: List[str] = (),
258+
artifacts: list[str] = (),
258259
data: Optional[dict] = None,
259260
check_validity: bool = True,
260261
overwrite: bool = False,
@@ -285,7 +286,7 @@ def cache_notebook_file(
285286
overwrite=overwrite,
286287
)
287288

288-
def list_cache_records(self) -> List[NbCacheRecord]:
289+
def list_cache_records(self) -> list[NbCacheRecord]:
289290
return NbCacheRecord.records_all(self.db)
290291

291292
def get_cache_record(self, pk: int) -> NbCacheRecord:
@@ -343,7 +344,7 @@ def merge_match_into_notebook(
343344
nb: nbf.NotebookNode,
344345
nb_meta: Optional[Iterable[str]] = ("kernelspec", "language_info", "widgets"),
345346
cell_meta: Optional[Iterable[str]] = None,
346-
) -> Tuple[int, nbf.NotebookNode]:
347+
) -> tuple[int, nbf.NotebookNode]:
347348
"""Match to an executed notebook and return a merged version
348349
349350
:param nb: The input notebook
@@ -413,7 +414,7 @@ def add_nb_to_project(
413414
path: str,
414415
*,
415416
read_data: Mapping = DEFAULT_READ_DATA,
416-
assets: List[str] = (),
417+
assets: list[str] = (),
417418
) -> NbProjectRecord:
418419
# check the reader can be loaded
419420
read_data = dict(read_data)
@@ -431,9 +432,9 @@ def add_nb_to_project(
431432

432433
def list_project_records(
433434
self,
434-
filter_uris: Optional[List[str]] = None,
435-
filter_pks: Optional[List[int]] = None,
436-
) -> List[NbProjectRecord]:
435+
filter_uris: Optional[list[str]] = None,
436+
filter_pks: Optional[list[int]] = None,
437+
) -> list[NbProjectRecord]:
437438
records = NbProjectRecord.records_all(self.db)
438439
if filter_uris is not None:
439440
records = [r for r in records if r.uri in filter_uris]
@@ -487,9 +488,9 @@ def get_cached_project_nb(
487488

488489
def list_unexecuted(
489490
self,
490-
filter_uris: Optional[List[str]] = None,
491-
filter_pks: Optional[List[int]] = None,
492-
) -> List[NbProjectRecord]:
491+
filter_uris: Optional[list[str]] = None,
492+
filter_pks: Optional[list[int]] = None,
493+
) -> list[NbProjectRecord]:
493494
records = []
494495
for record in self.list_project_records(filter_uris, filter_pks):
495496
nb = self.get_project_notebook(record.uri).nb

Diff for: jupyter_cache/entry_points.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Module for dealing with entry points."""
22

3-
from typing import Optional, Set
3+
from typing import Optional
44

55
# TODO importlib.metadata was introduced into the standard library in python 3.8
66
# so we can change this when we drop support for 3.7
@@ -14,7 +14,7 @@
1414
ENTRY_POINT_GROUP_EXEC = "jcache.executors"
1515

1616

17-
def list_group_names(group: str) -> Set[str]:
17+
def list_group_names(group: str) -> set[str]:
1818
"""Return the entry points within a group."""
1919
all_eps = eps()
2020
try:

Diff for: jupyter_cache/executors/base.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
import logging
3-
from typing import Any, Dict, List, Optional, Set
3+
from typing import Any, Optional
44

55
import attr
66

@@ -24,17 +24,17 @@ class ExecutorRunResult:
2424
"""A container for the execution result."""
2525

2626
# URIs of notebooks which where successfully executed
27-
succeeded: List[str] = attr.ib(factory=list)
27+
succeeded: list[str] = attr.ib(factory=list)
2828
# URIs of notebooks which excepted during execution
29-
excepted: List[str] = attr.ib(factory=list)
29+
excepted: list[str] = attr.ib(factory=list)
3030
# URIs of notebooks which errored before execution
31-
errored: List[str] = attr.ib(factory=list)
31+
errored: list[str] = attr.ib(factory=list)
3232

33-
def all(self) -> List[str]:
33+
def all(self) -> list[str]:
3434
"""Return all notebooks."""
3535
return self.succeeded + self.excepted + self.errored
3636

37-
def as_json(self) -> Dict[str, Any]:
37+
def as_json(self) -> dict[str, Any]:
3838
"""Return the result as a JSON serializable dict."""
3939
return {
4040
"succeeded": self.succeeded,
@@ -63,11 +63,11 @@ def logger(self):
6363

6464
def get_records(
6565
self,
66-
filter_uris: Optional[List[str]] = None,
67-
filter_pks: Optional[List[int]] = None,
66+
filter_uris: Optional[list[str]] = None,
67+
filter_pks: Optional[list[int]] = None,
6868
clear_tracebacks: bool = True,
6969
force: bool = False,
70-
) -> List[NbProjectRecord]:
70+
) -> list[NbProjectRecord]:
7171
"""Return records to execute.
7272
7373
:param clear_tracebacks: Remove any tracebacks from previous executions
@@ -86,8 +86,8 @@ def get_records(
8686
def run_and_cache(
8787
self,
8888
*,
89-
filter_uris: Optional[List[str]] = None,
90-
filter_pks: Optional[List[int]] = None,
89+
filter_uris: Optional[list[str]] = None,
90+
filter_pks: Optional[list[int]] = None,
9191
timeout: Optional[int] = 30,
9292
allow_errors: bool = False,
9393
force: bool = False,
@@ -105,7 +105,7 @@ def run_and_cache(
105105
"""
106106

107107

108-
def list_executors() -> Set[str]:
108+
def list_executors() -> set[str]:
109109
return list_group_names(ENTRY_POINT_GROUP_EXEC)
110110

111111

Diff for: jupyter_cache/executors/basic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
from pathlib import Path
55
import tempfile
6-
from typing import NamedTuple, Tuple
6+
from typing import NamedTuple
77

88
from jupyter_cache.base import JupyterCacheAbstract, ProjectNb
99
from jupyter_cache.cache.db import NbProjectRecord
@@ -45,7 +45,7 @@ def log_info(self, msg: str):
4545
def execute(self, project_nb: ProjectNb, data: ProcessData) -> ExecutionResult:
4646
raise NotImplementedError
4747

48-
def __call__(self, data: ProcessData) -> Tuple[int, str]:
48+
def __call__(self, data: ProcessData) -> tuple[int, str]:
4949
try:
5050
project_nb = data.cache.get_project_notebook(data.pk)
5151
except Exception:

0 commit comments

Comments
 (0)