44import warnings
55from itertools import chain
66from pathlib import Path
7- from typing import Any
7+ from typing import Any , Dict , List , Optional
88
99from gitingest .exceptions import InvalidNotebookError
1010
@@ -32,12 +32,13 @@ def process_notebook(file: Path, include_output: bool = True) -> str:
3232 """
3333 try :
3434 with file .open (encoding = "utf-8" ) as f :
35- notebook : dict [str , Any ] = json .load (f )
35+ notebook : Dict [str , Any ] = json .load (f )
3636 except json .JSONDecodeError as e :
3737 raise InvalidNotebookError (f"Invalid JSON in notebook: { file } " ) from e
3838
3939 # Check if the notebook contains worksheets
40- if worksheets := notebook .get ("worksheets" ):
40+ worksheets = notebook .get ("worksheets" )
41+ if worksheets :
4142 warnings .warn (
4243 "Worksheets are deprecated as of IPEP-17. Consider updating the notebook. "
4344 "(See: https://github.com/jupyter/nbformat and "
@@ -57,26 +58,27 @@ def process_notebook(file: Path, include_output: bool = True) -> str:
5758 result = ["# Jupyter notebook converted to Python script." ]
5859
5960 for cell in cells :
60- if cell_str := _process_cell (cell , include_output = include_output ):
61+ cell_str = _process_cell (cell , include_output = include_output )
62+ if cell_str :
6163 result .append (cell_str )
6264
6365 return "\n \n " .join (result ) + "\n "
6466
6567
66- def _process_cell (cell : dict [str , Any ], include_output : bool ) -> str | None :
68+ def _process_cell (cell : Dict [str , Any ], include_output : bool ) -> Optional [ str ] :
6769 """
6870 Process a Jupyter notebook cell and return the cell content as a string.
6971
7072 Parameters
7173 ----------
72- cell : dict [str, Any]
74+ cell : Dict [str, Any]
7375 The cell dictionary from a Jupyter notebook.
7476 include_output : bool
7577 Whether to include cell outputs in the generated script
7678
7779 Returns
7880 -------
79- str | None
81+ str, optional
8082 The cell content as a string, or None if the cell is empty.
8183
8284 Raises
@@ -101,7 +103,8 @@ def _process_cell(cell: dict[str, Any], include_output: bool) -> str | None:
101103 return f'"""\n { cell_str } \n """'
102104
103105 # Add cell output as comments
104- if include_output and (outputs := cell .get ("outputs" )):
106+ outputs = cell .get ("outputs" )
107+ if include_output and outputs :
105108
106109 # Include cell outputs as comments
107110 output_lines = []
@@ -118,18 +121,18 @@ def _process_cell(cell: dict[str, Any], include_output: bool) -> str | None:
118121 return cell_str
119122
120123
121- def _extract_output (output : dict [str , Any ]) -> list [str ]:
124+ def _extract_output (output : Dict [str , Any ]) -> List [str ]:
122125 """
123126 Extract the output from a Jupyter notebook cell.
124127
125128 Parameters
126129 ----------
127- output : dict [str, Any]
130+ output : Dict [str, Any]
128131 The output dictionary from a Jupyter notebook cell.
129132
130133 Returns
131134 -------
132- list [str]
135+ List [str]
133136 The output as a list of strings.
134137
135138 Raises
@@ -139,15 +142,13 @@ def _extract_output(output: dict[str, Any]) -> list[str]:
139142 """
140143 output_type = output ["output_type" ]
141144
142- match output_type :
143- case "stream" :
144- return output ["text" ]
145+ if output_type == "stream" :
146+ return output ["text" ]
145147
146- case "execute_result" | "display_data" :
147- return output ["data" ]["text/plain" ]
148+ if output_type in ( "execute_result" , "display_data" ) :
149+ return output ["data" ]["text/plain" ]
148150
149- case "error" :
150- return [f"Error: { output ['ename' ]} : { output ['evalue' ]} " ]
151+ if output_type == "error" :
152+ return [f"Error: { output ['ename' ]} : { output ['evalue' ]} " ]
151153
152- case _:
153- raise ValueError (f"Unknown output type: { output_type } " )
154+ raise ValueError (f"Unknown output type: { output_type } " )
0 commit comments