Move Vector support from main section parser to types section parser

This commit is contained in:
Etan Kissling 2024-01-09 15:09:38 +01:00
parent c41c128c76
commit 9ebf456e5a
No known key found for this signature in database
GPG Key ID: B21DA824C5A3D03D
2 changed files with 9 additions and 14 deletions

View File

@ -22,8 +22,11 @@ def collect_prev_forks(fork: str) -> list[str]:
forks.append(fork)
def is_byte_vector(value: str) -> bool:
return value.startswith(('ByteVector'))
def requires_mypy_type_ignore(value: str) -> bool:
return (
value.startswith(('ByteVector'))
or (value.startswith(('Vector')) and 'floorlog2' in value)
)
def make_function_abstract(protocol_def: ProtocolDefinition, key: str):
@ -41,7 +44,8 @@ def objects_to_spec(preset_name: str,
new_type_definitions = (
'\n\n'.join(
[
f"class {key}({value}):\n pass\n" if not is_byte_vector(value) else f"class {key}({value}): # type: ignore\n pass\n"
f"class {key}({value}):\n pass\n" if not requires_mypy_type_ignore(value)
else f"class {key}({value}): # type: ignore\n pass\n"
for key, value in spec_object.custom_types.items()
]
)

View File

@ -218,19 +218,10 @@ def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], pr
class_name, parent_class = _get_class_info_from_source(source)
# check consistency with spec
assert class_name == current_name
ignore_type = False
if parent_class:
if parent_class == "Container":
pass
elif parent_class == "Vector":
ignore_type = True # Avoid Mypy error: Invalid base class "Vector"
else:
raise Exception("unrecognized parent class: " + source)
assert parent_class == "Container"
# NOTE: trim whitespace from spec
lines = [line.rstrip() for line in source.splitlines()]
if ignore_type:
lines[0] += " # type: ignore"
ssz_objects[current_name] = "\n".join(lines)
ssz_objects[current_name] = "\n".join(line.rstrip() for line in source.splitlines())
else:
raise Exception("unrecognized python code element: " + source)
elif isinstance(child, Table):