mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 13:03:28 +01:00
disko2: Fix all mypy type checking errors
Belongs to d382a3cccf, but I authored that
on another machine.
Making sure vscode uses mypy from the environment is very important now,
because some of these errors get triggered in different ways depending
on the version.
This commit is contained in:
1 parent
a0e8f7f587
commit
ae7891e21f
22 files changed
+190
-145
No files matched your search
@@ -3,12 +3,19 @@
|
||||
import io
|
||||
import json
|
||||
import sys
|
||||
from typing import Any, Callable, Literal, Mapping, TypeGuard
|
||||
from typing import Any, Callable, Mapping, TypeGuard, TypeVar, TypedDict, cast
|
||||
|
||||
JsonDict = dict[str, "JsonValue"]
|
||||
JsonValue = str | int | float | bool | None | list["JsonValue"] | JsonDict
|
||||
|
||||
|
||||
def is_type(
|
||||
type_field: dict[str, Any]
|
||||
) -> TypeGuard[dict[Literal["default", "description", "type"], Any]]:
|
||||
class TypeDefinition(TypedDict):
|
||||
type: str | JsonDict
|
||||
default: JsonValue
|
||||
description: str
|
||||
|
||||
|
||||
def is_type(type_field: JsonValue) -> TypeGuard[TypeDefinition]:
|
||||
if not isinstance(type_field, dict):
|
||||
return False
|
||||
return set(type_field.keys()) == {"default", "description", "type"}
|
||||
@@ -111,7 +118,7 @@ def _parse_simple_type(type_str: str) -> str:
|
||||
|
||||
|
||||
def parse_field(
|
||||
containing_class: str, field_name: str, field: dict[str, Any]
|
||||
containing_class: str, field_name: str, field: str | JsonDict
|
||||
) -> tuple[str, io.StringIO | None]:
|
||||
if isinstance(field, str):
|
||||
return _parse_simple_type(field), None
|
||||
@@ -177,16 +184,20 @@ def generate_class(name: str, fields: dict[str, Any]) -> tuple[str, io.StringIO
|
||||
return buffer.getvalue(), contained_classes_buffer
|
||||
|
||||
|
||||
def transform_dict_keys(
|
||||
d: dict[str, Any], transform_fn: Callable[[str], str]
|
||||
) -> dict[str, Any]:
|
||||
T = TypeVar("T", bound=JsonValue)
|
||||
|
||||
|
||||
def transform_dict_keys(d: T, transform_fn: Callable[[str], str]) -> T:
|
||||
if not isinstance(d, Mapping):
|
||||
return d
|
||||
|
||||
return {transform_fn(k): transform_dict_keys(v, transform_fn) for k, v in d.items()}
|
||||
return cast(
|
||||
T,
|
||||
{transform_fn(k): transform_dict_keys(v, transform_fn) for k, v in d.items()},
|
||||
)
|
||||
|
||||
|
||||
def generate_python_code(schema: dict[str, dict[str, Any]]) -> io.StringIO:
|
||||
def generate_python_code(schema: JsonDict) -> io.StringIO:
|
||||
assert isinstance(schema, dict)
|
||||
|
||||
# Convert disallowed characters in Python identifiers
|
||||
@@ -195,7 +206,11 @@ def generate_python_code(schema: dict[str, dict[str, Any]]) -> io.StringIO:
|
||||
buffer = io.StringIO()
|
||||
|
||||
buffer.write(
|
||||
"""
|
||||
"""# File generated by scripts/generate_python_types.py
|
||||
# Ignore warnings that decorators contain Any
|
||||
# mypy: disable-error-code="misc"
|
||||
# Disable auto-formatting for this file
|
||||
# fmt: off
|
||||
from typing import Any, Literal, Union
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -204,6 +219,7 @@ from pydantic import BaseModel
|
||||
)
|
||||
|
||||
for type_name, fields in schema.items():
|
||||
assert isinstance(fields, dict)
|
||||
if "__isCompositeType" in fields:
|
||||
try:
|
||||
alias_content, type_code = _parse_composite_type(type_name, "", fields)
|
||||
|
||||
Reference in new issue
Block a user