From 297efac4f7880184ffaec555929730dffc6ca36b Mon Sep 17 00:00:00 2001 From: Dan Lindholm Date: Thu, 11 Jul 2024 00:20:33 +0200 Subject: [PATCH 1/5] add stubs for public interface --- mjml/__init__.pyi | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 mjml/__init__.pyi diff --git a/mjml/__init__.pyi b/mjml/__init__.pyi new file mode 100644 index 0000000..b40723d --- /dev/null +++ b/mjml/__init__.pyi @@ -0,0 +1,21 @@ +import pathlib +import typing as t + +if t.TYPE_CHECKING: + from _typeshed import SupportsRead + + from mjml.core.api import Component + + class _Output(t.NamedTuple): + html: str + errors: t.Sequence[str] + + FpOrJson = t.Union[t.Dict[str, t.Any], str, bytes, SupportsRead[str], SupportsRead[bytes]] + StrOrPath = t.Union[str, pathlib.PurePath] + +def mjml_to_html( + xml_fp_or_json: "FpOrJson", + skeleton: t.Optional[str] = None, + template_dir: t.Optional["StrOrPath"] = None, + custom_components: t.Optional[t.List[t.Type["Component"]]] = None, +) -> "_Output": ... From b400fb6771febbd87419e3ddf483274e6476514c Mon Sep 17 00:00:00 2001 From: Dan Lindholm Date: Thu, 11 Jul 2024 00:20:54 +0200 Subject: [PATCH 2/5] add py.typed marker --- mjml/py.typed | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 mjml/py.typed diff --git a/mjml/py.typed b/mjml/py.typed new file mode 100644 index 0000000..e69de29 From 3d853ef10c967e4e552d881ee913a5047bda25b1 Mon Sep 17 00:00:00 2001 From: Dan Lindholm Date: Thu, 11 Jul 2024 12:57:54 +0200 Subject: [PATCH 3/5] add subscript and get type hints for output type --- mjml/__init__.pyi | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mjml/__init__.pyi b/mjml/__init__.pyi index b40723d..ea7503b 100644 --- a/mjml/__init__.pyi +++ b/mjml/__init__.pyi @@ -2,6 +2,7 @@ import pathlib import typing as t if t.TYPE_CHECKING: + import typing_extensions as te from _typeshed import SupportsRead from mjml.core.api import Component @@ -10,6 +11,19 @@ if t.TYPE_CHECKING: html: str errors: t.Sequence[str] + @te.overload + def __getitem__(self, _: t.Literal["html"]) -> str: ... + @te.overload + def __getitem__(self, _: t.Literal["errors"]) -> t.Sequence[str]: ... + @te.overload + def get(self, key: t.Literal["html"], /) -> t.Optional[str]: ... + @te.overload + def get(self, key: t.Literal["html"], default: str, /) -> str: ... + @te.overload + def get(self, key: t.Literal["errors"], /) -> t.Optional[t.Sequence[str]]: ... + @te.overload + def get(self, key: t.Literal["errors"], default: t.Sequence[str], /) -> t.Sequence[str]: ... + FpOrJson = t.Union[t.Dict[str, t.Any], str, bytes, SupportsRead[str], SupportsRead[bytes]] StrOrPath = t.Union[str, pathlib.PurePath] From 5a3cb5527dd5a0f1010d8e53e1a100de44c25bb9 Mon Sep 17 00:00:00 2001 From: Dan Lindholm Date: Wed, 14 Aug 2024 22:02:14 +0200 Subject: [PATCH 4/5] use StrPath from typeshed --- mjml/__init__.pyi | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mjml/__init__.pyi b/mjml/__init__.pyi index ea7503b..e110c0a 100644 --- a/mjml/__init__.pyi +++ b/mjml/__init__.pyi @@ -1,9 +1,8 @@ -import pathlib import typing as t if t.TYPE_CHECKING: import typing_extensions as te - from _typeshed import SupportsRead + from _typeshed import StrPath, SupportsRead from mjml.core.api import Component @@ -25,11 +24,10 @@ if t.TYPE_CHECKING: def get(self, key: t.Literal["errors"], default: t.Sequence[str], /) -> t.Sequence[str]: ... FpOrJson = t.Union[t.Dict[str, t.Any], str, bytes, SupportsRead[str], SupportsRead[bytes]] - StrOrPath = t.Union[str, pathlib.PurePath] def mjml_to_html( xml_fp_or_json: "FpOrJson", skeleton: t.Optional[str] = None, - template_dir: t.Optional["StrOrPath"] = None, + template_dir: t.Optional["StrPath"] = None, custom_components: t.Optional[t.List[t.Type["Component"]]] = None, ) -> "_Output": ... From 1b7f8a4658538592506debb17d30882a951337cf Mon Sep 17 00:00:00 2001 From: Dan Lindholm Date: Wed, 14 Aug 2024 22:44:59 +0200 Subject: [PATCH 5/5] use mapping instead of dict --- mjml/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mjml/__init__.pyi b/mjml/__init__.pyi index e110c0a..4b5ca95 100644 --- a/mjml/__init__.pyi +++ b/mjml/__init__.pyi @@ -23,7 +23,7 @@ if t.TYPE_CHECKING: @te.overload def get(self, key: t.Literal["errors"], default: t.Sequence[str], /) -> t.Sequence[str]: ... - FpOrJson = t.Union[t.Dict[str, t.Any], str, bytes, SupportsRead[str], SupportsRead[bytes]] + FpOrJson = t.Union[t.Mapping[str, t.Any], str, bytes, SupportsRead[str], SupportsRead[bytes]] def mjml_to_html( xml_fp_or_json: "FpOrJson",