Skip to content

Commit

Permalink
Import typing as t.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelme committed Aug 16, 2024
1 parent a50db90 commit b4baa7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
34 changes: 17 additions & 17 deletions htpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
__all__: list[str] = []

import functools
import typing as t
from collections.abc import Callable, Iterable, Iterator
from typing import Any, Protocol, TypeAlias, TypeVar, overload

from markupsafe import Markup as _Markup
from markupsafe import escape as _escape

BaseElementSelf = TypeVar("BaseElementSelf", bound="BaseElement")
ElementSelf = TypeVar("ElementSelf", bound="Element")
BaseElementSelf = t.TypeVar("BaseElementSelf", bound="BaseElement")
ElementSelf = t.TypeVar("ElementSelf", bound="Element")


def _force_escape(value: Any) -> str:
def _force_escape(value: t.Any) -> str:
return _escape(str(value))


# Inspired by https://www.npmjs.com/package/classnames
def _class_names(items: Any) -> Any:
def _class_names(items: t.Any) -> t.Any:
if isinstance(items, str):
return _force_escape(items)

Expand All @@ -33,7 +33,7 @@ def _class_names(items: Any) -> Any:
return " ".join(_force_escape(class_name) for class_name in result)


def _class_names_for_items(items: Any) -> Any:
def _class_names_for_items(items: t.Any) -> t.Any:
for item in items:
if isinstance(item, dict):
for k, v in item.items(): # pyright: ignore [reportUnknownVariableType]
Expand All @@ -44,7 +44,7 @@ def _class_names_for_items(items: Any) -> Any:
yield item


def _id_class_names_from_css_str(x: Any) -> dict[str, Attribute]:
def _id_class_names_from_css_str(x: t.Any) -> dict[str, Attribute]:
if not isinstance(x, str):
raise ValueError(f"id/class strings must be str. got {x}")

Expand Down Expand Up @@ -153,21 +153,21 @@ def __init__(
def __str__(self) -> _Markup:
return _Markup("".join(str(x) for x in self))

@overload
@t.overload
def __call__(
self: BaseElementSelf, id_class: str, attrs: dict[str, Attribute], **kwargs: Attribute
) -> BaseElementSelf: ...
@overload
@t.overload
def __call__(
self: BaseElementSelf, id_class: str = "", **kwargs: Attribute
) -> BaseElementSelf: ...
@overload
@t.overload
def __call__(
self: BaseElementSelf, attrs: dict[str, Attribute], **kwargs: Attribute
) -> BaseElementSelf: ...
@overload
@t.overload
def __call__(self: BaseElementSelf, **kwargs: Attribute) -> BaseElementSelf: ...
def __call__(self: BaseElementSelf, *args: Any, **kwargs: Any) -> BaseElementSelf:
def __call__(self: BaseElementSelf, *args: t.Any, **kwargs: t.Any) -> BaseElementSelf:
id_class = ""
attrs: dict[str, Attribute] = {}

Expand Down Expand Up @@ -234,17 +234,17 @@ def comment(text: str) -> _Markup:
return _Markup(f"<!-- {escaped_text} -->")


class _HasHtml(Protocol):
class _HasHtml(t.Protocol):
def __html__(self) -> str: ...


_ClassNamesDict: TypeAlias = dict[str, bool]
_ClassNames: TypeAlias = Iterable[str | None | bool | _ClassNamesDict] | _ClassNamesDict
Node: TypeAlias = (
_ClassNamesDict: t.TypeAlias = dict[str, bool]
_ClassNames: t.TypeAlias = Iterable[str | None | bool | _ClassNamesDict] | _ClassNamesDict
Node: t.TypeAlias = (
None | bool | str | BaseElement | _HasHtml | Iterable["Node"] | Callable[[], "Node"]
)

Attribute: TypeAlias = None | bool | str | _HasHtml | _ClassNames
Attribute: t.TypeAlias = None | bool | str | _HasHtml | _ClassNames

# https://developer.mozilla.org/en-US/docs/Glossary/Doctype
html = HTMLElement("html")
Expand Down
10 changes: 5 additions & 5 deletions htpy/html2htpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import shutil
import subprocess
import sys
import typing as t
from abc import ABC, abstractmethod
from html.parser import HTMLParser
from typing import Literal

import htpy

Expand Down Expand Up @@ -234,7 +234,7 @@ def handle_data(self, data: str) -> None:
def serialize_python(
self,
shorthand_id_class: bool = False,
import_mode: Literal["yes", "h", "no"] = "yes",
import_mode: t.Literal["yes", "h", "no"] = "yes",
formatter: Formatter | None = None,
) -> str:
o = ""
Expand Down Expand Up @@ -282,7 +282,7 @@ def _tags_from_children(parent: Tag) -> None:
def html2htpy(
html: str,
shorthand_id_class: bool = True,
import_mode: Literal["yes", "h", "no"] = "yes",
import_mode: t.Literal["yes", "h", "no"] = "yes",
formatter: Formatter | None = None,
) -> str:
parser = HTPYParser()
Expand Down Expand Up @@ -345,7 +345,7 @@ def _serialize(el: Tag | str, shorthand_id_class: bool, use_h_prefix: bool) -> s
return str(el)


def _get_formatter(format: Literal["auto", "ruff", "black", "none"]) -> Formatter | None:
def _get_formatter(format: t.Literal["auto", "ruff", "black", "none"]) -> Formatter | None:
if format == "ruff":
if _is_command_available("ruff"):
return RuffFormatter()
Expand Down Expand Up @@ -422,7 +422,7 @@ def main() -> None:
sys.exit(1)

shorthand = not args.no_shorthand
imports: Literal["yes", "h", "no"] = args.imports
imports: t.Literal["yes", "h", "no"] = args.imports

formatter = _get_formatter(args.format)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_children.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
import typing as t

import pytest
from markupsafe import Markup
from typing_extensions import assert_type

from htpy import Element, VoidElement, dd, div, dl, dt, html, img, input, li, my_custom_element, ul

if TYPE_CHECKING:
if t.TYPE_CHECKING:
from collections.abc import Callable, Generator

from htpy import Node
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_custom_element() -> None:


@pytest.mark.parametrize("ignored_value", [None, True, False])
def test_ignored(ignored_value: Any) -> None:
def test_ignored(ignored_value: t.Any) -> None:
assert str(div[ignored_value]) == "<div></div>"


Expand Down Expand Up @@ -199,6 +199,6 @@ def test_callable_in_generator() -> None:


@pytest.mark.parametrize("not_a_child", [1234, b"foo", object(), object, 1, 0])
def test_invalid_child(not_a_child: Any) -> None:
def test_invalid_child(not_a_child: t.Any) -> None:
with pytest.raises(ValueError, match="is not a valid child element"):
str(div[not_a_child])

0 comments on commit b4baa7b

Please sign in to comment.