22import json
33import re
44import string
5- from functools import lru_cache
5+ from collections .abc import Iterator , Mapping
6+ from functools import cache
67from pathlib import Path
7- from typing import cast , Dict , Iterator , Mapping , Optional , Tuple
8+ from typing import cast
89
910from marko .block import BlankLine , Document , FencedCode , Heading , HTMLBlock
1011from marko .element import Element
@@ -31,7 +32,7 @@ def __init__(
3132 self .preset_name = preset_name
3233
3334 self .document_iterator : Iterator [Element ] = self ._parse_document (file_name )
34- self .all_custom_types : Dict [str , str ] = {}
35+ self .all_custom_types : dict [str , str ] = {}
3536 self .current_heading_name : str | None = None
3637
3738 # Use a single dict to hold all SpecObject fields
@@ -59,7 +60,7 @@ def run(self) -> SpecObject:
5960 self ._finalize_types ()
6061 return self ._build_spec_object ()
6162
62- def _get_next_element (self ) -> Optional [ Element ] :
63+ def _get_next_element (self ) -> Element | None :
6364 """
6465 Returns the next non-blank element in the document.
6566 """
@@ -240,7 +241,7 @@ def _process_table(self, table: Table) -> None:
240241 self .spec ["constant_vars" ][name ] = value_def
241242
242243 @staticmethod
243- def _get_table_row_fields (row : TableRow ) -> tuple [str , str , Optional [ str ] ]:
244+ def _get_table_row_fields (row : TableRow ) -> tuple [str , str , str | None ]:
244245 """
245246 Extracts the name, value, and description fields from a table row element.
246247 """
@@ -294,9 +295,9 @@ def _process_list_of_records_table(self, table: Table, list_of_records_name: str
294295 # For mainnet, check that the spec config & file config are the same
295296 # For minimal, we expect this to be different; just use the file config
296297 if self .preset_name == "mainnet" :
297- assert (
298- list_of_records_spec == list_of_records_config_file
299- ), f"list of records mismatch: { list_of_records_spec } vs { list_of_records_config_file } "
298+ assert list_of_records_spec == list_of_records_config_file , (
299+ f"list of records mismatch: { list_of_records_spec } vs { list_of_records_config_file } "
300+ )
300301
301302 # Set the config variable
302303 self .spec ["config_vars" ][list_of_records_name ] = list_of_records_config_file
@@ -435,21 +436,21 @@ def _build_spec_object(self) -> SpecObject:
435436 )
436437
437438
438- @lru_cache ( maxsize = None )
439- def _get_name_from_heading (heading : Heading ) -> Optional [ str ] :
439+ @cache
440+ def _get_name_from_heading (heading : Heading ) -> str | None :
440441 last_child = heading .children [- 1 ]
441442 if isinstance (last_child , CodeSpan ):
442443 return last_child .children
443444 return None
444445
445446
446- @lru_cache ( maxsize = None )
447+ @cache
447448def _get_source_from_code_block (block : FencedCode ) -> str :
448449 return block .children [0 ].children .strip ()
449450
450451
451- @lru_cache ( maxsize = None )
452- def _get_self_type_from_source (fn : ast .FunctionDef ) -> Optional [ str ] :
452+ @cache
453+ def _get_self_type_from_source (fn : ast .FunctionDef ) -> str | None :
453454 args = fn .args .args
454455 if len (args ) == 0 :
455456 return None
@@ -460,8 +461,8 @@ def _get_self_type_from_source(fn: ast.FunctionDef) -> Optional[str]:
460461 return args [0 ].annotation .id
461462
462463
463- @lru_cache ( maxsize = None )
464- def _get_class_info_from_ast (cls : ast .ClassDef ) -> Tuple [str , Optional [ str ] ]:
464+ @cache
465+ def _get_class_info_from_ast (cls : ast .ClassDef ) -> tuple [str , str | None ]:
465466 base = cls .bases [0 ]
466467 if isinstance (base , ast .Name ):
467468 parent_class = base .id
@@ -475,7 +476,7 @@ def _get_class_info_from_ast(cls: ast.ClassDef) -> Tuple[str, Optional[str]]:
475476 return cls .name , parent_class
476477
477478
478- @lru_cache ( maxsize = None )
479+ @cache
479480def _is_constant_id (name : str ) -> bool :
480481 """
481482 Checks if the given name follows the convention for constant identifiers.
@@ -485,16 +486,16 @@ def _is_constant_id(name: str) -> bool:
485486 return all (map (lambda c : c in string .ascii_uppercase + "_" + string .digits , name [1 :]))
486487
487488
488- @lru_cache ( maxsize = None )
489- def _load_kzg_trusted_setups (preset_name : str ) -> Tuple [list [str ], list [str ], list [str ]]:
489+ @cache
490+ def _load_kzg_trusted_setups (preset_name : str ) -> tuple [list [str ], list [str ], list [str ]]:
490491 trusted_setups_file_path = (
491492 str (Path (__file__ ).parent .parent )
492493 + "/presets/"
493494 + preset_name
494495 + "/trusted_setups/trusted_setup_4096.json"
495496 )
496497
497- with open (trusted_setups_file_path , "r" ) as f :
498+ with open (trusted_setups_file_path ) as f :
498499 json_data = json .load (f )
499500 trusted_setup_G1_monomial = json_data ["g1_monomial" ]
500501 trusted_setup_G1_lagrange = json_data ["g1_lagrange" ]
@@ -503,8 +504,8 @@ def _load_kzg_trusted_setups(preset_name: str) -> Tuple[list[str], list[str], li
503504 return trusted_setup_G1_monomial , trusted_setup_G1_lagrange , trusted_setup_G2_monomial
504505
505506
506- @lru_cache ( maxsize = None )
507- def _load_curdleproofs_crs (preset_name : str ) -> Dict [str , list [str ]]:
507+ @cache
508+ def _load_curdleproofs_crs (preset_name : str ) -> dict [str , list [str ]]:
508509 """
509510 NOTE: File generated from https://github.com/asn-d6/curdleproofs/blob/8e8bf6d4191fb6a844002f75666fb7009716319b/tests/crs.rs#L53-L67
510511 """
@@ -515,7 +516,7 @@ def _load_curdleproofs_crs(preset_name: str) -> Dict[str, list[str]]:
515516 + "/trusted_setups/curdleproofs_crs.json"
516517 )
517518
518- with open (file_path , "r" ) as f :
519+ with open (file_path ) as f :
519520 json_data = json .load (f )
520521
521522 return json_data
@@ -532,10 +533,8 @@ def _load_curdleproofs_crs(preset_name: str) -> Dict[str, list[str]]:
532533}
533534
534535
535- @lru_cache (maxsize = None )
536- def _parse_value (
537- name : str , typed_value : str , type_hint : Optional [str ] = None
538- ) -> VariableDefinition :
536+ @cache
537+ def _parse_value (name : str , typed_value : str , type_hint : str | None = None ) -> VariableDefinition :
539538 comment = None
540539 if name in ("ROOT_OF_UNITY_EXTENDED" , "ROOTS_OF_UNITY_EXTENDED" , "ROOTS_OF_UNITY_REDUCED" ):
541540 comment = "noqa: E501"
@@ -585,7 +584,7 @@ def _update_constant_vars_with_curdleproofs_crs(
585584 )
586585
587586
588- @lru_cache ( maxsize = None )
587+ @cache
589588def parse_markdown (content : str ) -> Document :
590589 return gfm .parse (content )
591590
@@ -613,9 +612,9 @@ def check_yaml_matches_spec(
613612 else :
614613 raise ValueError (f"Variable { var } should be a string in the yaml file." )
615614 try :
616- assert yaml [var_name ] == repr (
617- eval (updated_value )
618- ), f"mismatch for { var_name } : { yaml [ var_name ] } vs { eval ( updated_value ) } "
615+ assert yaml [var_name ] == repr (eval ( updated_value )), (
616+ f"mismatch for { var_name } : { yaml [ var_name ] } vs { eval (updated_value )} "
617+ )
619618 except NameError :
620619 # Okay it's probably something more serious, let's ignore
621620 pass
0 commit comments