1
+ from collections .abc import Iterable , Mapping
1
2
from contextlib import contextmanager
2
3
import copy
3
4
import hashlib
4
5
import io
5
6
from pathlib import Path
6
7
import shutil
7
- from typing import Iterable , List , Mapping , Optional , Tuple , Union
8
+ from typing import Optional , Union
8
9
9
10
import nbformat as nbf
10
11
31
32
class NbArtifacts (NbArtifactsAbstract ):
32
33
"""Container for artefacts of a notebook execution."""
33
34
34
- def __init__ (self , paths : List [str ], in_folder , check_existence = True ):
35
+ def __init__ (self , paths : list [str ], in_folder , check_existence = True ):
35
36
"""Initiate NbArtifacts
36
37
37
38
:param paths: list of paths
@@ -44,11 +45,11 @@ def __init__(self, paths: List[str], in_folder, check_existence=True):
44
45
to_relative_paths (self .paths , self .in_folder , check_existence = check_existence )
45
46
46
47
@property
47
- def relative_paths (self ) -> List [Path ]:
48
+ def relative_paths (self ) -> list [Path ]:
48
49
"""Return the list of paths (relative to the notebook folder)."""
49
50
return to_relative_paths (self .paths , self .in_folder )
50
51
51
- def __iter__ (self ) -> Iterable [Tuple [Path , io .BufferedReader ]]:
52
+ def __iter__ (self ) -> Iterable [tuple [Path , io .BufferedReader ]]:
52
53
"""Yield the relative path and open files (in bytes mode)"""
53
54
for path in self .paths :
54
55
with path .open ("rb" ) as handle :
@@ -123,7 +124,7 @@ def create_hashed_notebook(
123
124
nb : nbf .NotebookNode ,
124
125
nb_metadata : Optional [Iterable [str ]] = ("kernelspec" ,),
125
126
cell_metadata : Optional [Iterable [str ]] = None ,
126
- ) -> Tuple [nbf .NotebookNode , str ]:
127
+ ) -> tuple [nbf .NotebookNode , str ]:
127
128
"""Convert a notebook to a standard format and hash.
128
129
129
130
Note: we always hash notebooks as version 4.4,
@@ -254,7 +255,7 @@ def cache_notebook_file(
254
255
self ,
255
256
path : str ,
256
257
uri : Optional [str ] = None ,
257
- artifacts : List [str ] = (),
258
+ artifacts : list [str ] = (),
258
259
data : Optional [dict ] = None ,
259
260
check_validity : bool = True ,
260
261
overwrite : bool = False ,
@@ -285,7 +286,7 @@ def cache_notebook_file(
285
286
overwrite = overwrite ,
286
287
)
287
288
288
- def list_cache_records (self ) -> List [NbCacheRecord ]:
289
+ def list_cache_records (self ) -> list [NbCacheRecord ]:
289
290
return NbCacheRecord .records_all (self .db )
290
291
291
292
def get_cache_record (self , pk : int ) -> NbCacheRecord :
@@ -343,7 +344,7 @@ def merge_match_into_notebook(
343
344
nb : nbf .NotebookNode ,
344
345
nb_meta : Optional [Iterable [str ]] = ("kernelspec" , "language_info" , "widgets" ),
345
346
cell_meta : Optional [Iterable [str ]] = None ,
346
- ) -> Tuple [int , nbf .NotebookNode ]:
347
+ ) -> tuple [int , nbf .NotebookNode ]:
347
348
"""Match to an executed notebook and return a merged version
348
349
349
350
:param nb: The input notebook
@@ -413,7 +414,7 @@ def add_nb_to_project(
413
414
path : str ,
414
415
* ,
415
416
read_data : Mapping = DEFAULT_READ_DATA ,
416
- assets : List [str ] = (),
417
+ assets : list [str ] = (),
417
418
) -> NbProjectRecord :
418
419
# check the reader can be loaded
419
420
read_data = dict (read_data )
@@ -431,9 +432,9 @@ def add_nb_to_project(
431
432
432
433
def list_project_records (
433
434
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 ]:
437
438
records = NbProjectRecord .records_all (self .db )
438
439
if filter_uris is not None :
439
440
records = [r for r in records if r .uri in filter_uris ]
@@ -487,9 +488,9 @@ def get_cached_project_nb(
487
488
488
489
def list_unexecuted (
489
490
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 ]:
493
494
records = []
494
495
for record in self .list_project_records (filter_uris , filter_pks ):
495
496
nb = self .get_project_notebook (record .uri ).nb
0 commit comments