Skip to content

Commit

Permalink
feat(typing): Enable type checking on tools/
Browse files Browse the repository at this point in the history
Also fixes some warnings only present for `mypy`, but not `pyright`.
  • Loading branch information
dangotbanned committed Sep 7, 2024
1 parent 2efe2ea commit d922e4b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ installer = "uv"

[tool.hatch.envs.default.scripts]
generate-schema-wrapper = [
"mypy tools/schemapi/schemapi.py",
"mypy tools",
"python tools/generate_schema_wrapper.py",
"test"
]
Expand Down Expand Up @@ -439,7 +439,7 @@ module = [
"ibis.*",
# This refers to schemapi in the tools folder which is imported
# by the tools scripts such as generate_schema_wrapper.py
"schemapi.*"
# "schemapi.*"
]
ignore_missing_imports = true

Expand Down
2 changes: 1 addition & 1 deletion tools/generate_api_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def toplevel_charts() -> list[str]:


def encoding_wrappers() -> list[str]:
return sorted(iter_objects(alt.channels, restrict_to_subclass=alt.SchemaBase))
return sorted(iter_objects(alt.channels, restrict_to_subclass=alt.SchemaBase)) # type: ignore[attr-defined]


def api_functions() -> list[str]:
Expand Down
4 changes: 2 additions & 2 deletions tools/generate_schema_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def load_schema_with_shorthand_properties(fp: Path, /) -> dict[str, Any]:
for propschema in encoding.properties.values():
def_dict = get_field_datum_value_defs(propschema, schema)
if field_ref := def_dict.get("field", None):
defschema = {"$ref": field_ref}
defschema: dict[str, Any] = {"$ref": field_ref}
defschema = copy.deepcopy(resolve_references(defschema, schema))
# For Encoding field definitions, we patch the schema by adding the
# shorthand property.
Expand Down Expand Up @@ -702,7 +702,7 @@ def generate_vegalite_channel_wrappers(

gen: SchemaGenerator
defschema = {"$ref": definition}
kwds = {
kwds: dict[str, Any] = {
"basename": basename,
"schema": defschema,
"rootschema": schema,
Expand Down
23 changes: 14 additions & 9 deletions tools/schemapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
Iterable,
Iterator,
Literal,
Mapping,
MutableSequence,
Sequence,
overload,
)
Expand All @@ -27,9 +29,9 @@
from tools.schemapi.schemapi import _resolve_references as resolve_references

if TYPE_CHECKING:
from _collections_abc import dict_keys
from _collections_abc import KeysView
from pathlib import Path
from typing_extensions import LiteralString, TypeAlias
from typing_extensions import LiteralString, Never, TypeAlias

from mistune import BlockState

Expand Down Expand Up @@ -139,7 +141,7 @@ def add_literal(
self._update_literals(alias, tp)
if replace:
tp = alias
elif (alias := self._literals_invert.get(tp)) and replace:
elif (alias := self._literals_invert.get(tp, "")) and replace:
tp = alias
elif replace and info.is_union_literal():
# Handles one very specific edge case `WindowFieldDef`
Expand Down Expand Up @@ -201,10 +203,13 @@ def write_module(
extra
`tools.generate_schema_wrapper.TYPING_EXTRA`.
"""
ruff_format = ["ruff", "format", fp]
ruff_format: MutableSequence[str | Path] = ["ruff", "format", fp]
if self._cmd_format:
ruff_format.extend(self._cmd_format)
commands = (["ruff", "check", fp, *self._cmd_check], ruff_format)
commands: tuple[Sequence[str | Path], ...] = (
["ruff", "check", fp, *self._cmd_check],
ruff_format,
)
static = (header, "\n", *self._imports, "\n\n")
self.update_aliases(*sorted(self._literals.items(), key=itemgetter(0)))
all_ = [*iter(self._aliases), *extra_all]
Expand Down Expand Up @@ -438,11 +443,11 @@ def to_type_repr( # noqa: C901
tp_str = TypeAliasTracer.add_literal(self, spell_literal(it), replace=True)
tps.add(tp_str)
elif self.is_anyOf():
it = (
it_nest = (
s.to_type_repr(target=target, as_str=False, use_concrete=use_concrete)
for s in self.anyOf
)
tps.update(maybe_rewrap_literal(chain.from_iterable(it)))
tps.update(maybe_rewrap_literal(chain.from_iterable(it_nest)))
elif isinstance(self.type, list):
options = []
subschema = SchemaInfo(dict(**self.schema))
Expand Down Expand Up @@ -1015,7 +1020,7 @@ def ruff_format_py(fp: Path, /, *extra_args: str) -> None:
ruff format --diff --check .
```
"""
cmd = ["ruff", "format", fp]
cmd: MutableSequence[str | Path] = ["ruff", "format", fp]
if extra_args:
cmd.extend(extra_args)
r = subprocess.run(cmd, check=True)
Expand All @@ -1036,7 +1041,7 @@ def ruff_write_lint_format_str(
- Encoding set as default
- `I001/2` are `isort` rules, to sort imports.
"""
commands = (
commands: Iterable[Sequence[str | Path]] = (
["ruff", "check", fp, "--fix"],
["ruff", "check", fp, "--fix", "--select", "I001", "--select", "I002"],
)
Expand Down

0 comments on commit d922e4b

Please sign in to comment.