From f6b49239a7b7b7fae3d560d9d7df063d21a3d57c Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Tue, 3 Sep 2024 19:12:08 +0100 Subject: [PATCH] fix(typing): Resolve/ignore typing in `tools/` --- tools/schemapi/codegen.py | 2 +- tools/schemapi/utils.py | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/tools/schemapi/codegen.py b/tools/schemapi/codegen.py index e808bc7dc..4e7da36cf 100644 --- a/tools/schemapi/codegen.py +++ b/tools/schemapi/codegen.py @@ -294,7 +294,7 @@ def get_args(self, si: SchemaInfo) -> list[str]: f"{p}: {info.to_type_repr(target='annotation', use_undefined=True)} = Undefined" for p, info in prop_infos.items() ) - elif si.type: + elif isinstance(si.type, str): py_type = jsonschema_to_python_types[si.type] if py_type == "list": # Try to get a type hint like "List[str]" which is more specific diff --git a/tools/schemapi/utils.py b/tools/schemapi/utils.py index 5ee867384..0f3c4be0b 100644 --- a/tools/schemapi/utils.py +++ b/tools/schemapi/utils.py @@ -6,7 +6,7 @@ import re import subprocess import textwrap -import urllib +import urllib.parse from html import unescape from itertools import chain from operator import itemgetter @@ -289,7 +289,7 @@ def get_valid_identifier( def is_valid_identifier(s: str, /) -> bool: """Return ``True`` if ``s`` contains a valid Python identifier.""" - return _VALID_IDENT.match(s) and not keyword.iskeyword(s) + return _VALID_IDENT.match(s) is not None and not keyword.iskeyword(s) class SchemaProperties: @@ -311,11 +311,8 @@ def __bool__(self) -> bool: def __dir__(self) -> list[str]: return list(self._properties.keys()) - def __getattr__(self, attr): - try: - return self[attr] - except KeyError: - return super().__getattr__(attr) + def __getattr__(self, attr) -> SchemaInfo: + return self[attr] def __getitem__(self, attr) -> SchemaInfo: dct = self._properties[attr] @@ -748,7 +745,7 @@ def __init__( super().__init__(renderer, block, inline, plugins) def __call__(self, s: str) -> str: - s = super().__call__(s) + s = super().__call__(s) # pyright: ignore[reportAssignmentType] return unescape(s).replace(r"\ ,", ",").replace(r"\ ", " ") @@ -1045,8 +1042,8 @@ def ruff_write_lint_format_str( def import_type_checking(*imports: str) -> str: """Write an `if TYPE_CHECKING` block.""" - imports = "\n".join(f" {s}" for s in imports) - return f"\nif TYPE_CHECKING:\n # ruff: noqa: F405\n{imports}\n" + imps = "\n".join(f" {s}" for s in imports) + return f"\nif TYPE_CHECKING:\n # ruff: noqa: F405\n{imps}\n" def import_typing_extensions(