From 2797efb618eb40284165c3b1265d88a495fd1584 Mon Sep 17 00:00:00 2001 From: Dan Lindholm Date: Tue, 2 Jul 2024 00:20:41 +0200 Subject: [PATCH] typing for head elements --- mjml/elements/head/_head_base.py | 5 ++++- mjml/elements/head/mj_attributes.py | 12 +++++++++--- mjml/elements/head/mj_breakpoint.py | 11 ++++++++--- mjml/elements/head/mj_font.py | 11 ++++++++--- mjml/elements/head/mj_head.py | 9 +++++++-- mjml/elements/head/mj_html_attributes.py | 8 ++++++-- mjml/elements/head/mj_preview.py | 8 ++++++-- mjml/elements/head/mj_style.py | 11 ++++++++--- mjml/elements/head/mj_title.py | 8 ++++++-- 9 files changed, 62 insertions(+), 21 deletions(-) diff --git a/mjml/elements/head/_head_base.py b/mjml/elements/head/_head_base.py index db6f95d..c2ba81a 100644 --- a/mjml/elements/head/_head_base.py +++ b/mjml/elements/head/_head_base.py @@ -1,12 +1,15 @@ +import typing as t from mjml.core import Component, initComponent __all__ = ['HeadComponent'] + class HeadComponent(Component): + # TODO typing: figure out proper type annotations def handlerChildren(self): - def handle_children(children): + def handle_children(children: t.Dict[str, t.Any]) -> t.Optional[str]: tagName = children['tagName'] component = initComponent( name = tagName, diff --git a/mjml/elements/head/mj_attributes.py b/mjml/elements/head/mj_attributes.py index bed3d47..70c5a63 100644 --- a/mjml/elements/head/mj_attributes.py +++ b/mjml/elements/head/mj_attributes.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from mjml.helpers import omit @@ -6,12 +9,15 @@ __all__ = ['MjAttributes'] + class MjAttributes(HeadComponent): - component_name = 'mj-attributes' + component_name: t.ClassVar[str] = 'mj-attributes' - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] - _children = self.props.get("children") + if (_children := self.props.get("children")) is None: + return None for child in _children: tagName = child['tagName'] diff --git a/mjml/elements/head/mj_breakpoint.py b/mjml/elements/head/mj_breakpoint.py index 6a8d54f..d12e1d7 100644 --- a/mjml/elements/head/mj_breakpoint.py +++ b/mjml/elements/head/mj_breakpoint.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -6,14 +9,16 @@ class MjBreakpoint(HeadComponent): - component_name = 'mj-breakpoint' + component_name: t.ClassVar[str] = 'mj-breakpoint' + @te.override @classmethod - def allowed_attrs(cls): + def allowed_attrs(cls) -> t.Dict[str, str]: return { 'width': 'unit(px)', } - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] add('breakpoint', self.getAttribute('width')) diff --git a/mjml/elements/head/mj_font.py b/mjml/elements/head/mj_font.py index 8661edb..2469d7b 100644 --- a/mjml/elements/head/mj_font.py +++ b/mjml/elements/head/mj_font.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -6,15 +9,17 @@ class MjFont(HeadComponent): - component_name = 'mj-font' + component_name: t.ClassVar[str] = 'mj-font' + @te.override @classmethod - def allowed_attrs(cls): + def allowed_attrs(cls) -> t.Dict[str, str]: return { 'href' : 'string', 'name' : 'string', } - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] add('fonts', self.getAttribute('name'), self.getAttribute('href')) diff --git a/mjml/elements/head/mj_head.py b/mjml/elements/head/mj_head.py index 36c4331..3daea30 100644 --- a/mjml/elements/head/mj_head.py +++ b/mjml/elements/head/mj_head.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -5,7 +8,9 @@ __all__ = ['MjHead'] class MjHead(HeadComponent): - component_name = 'mj-head' + component_name: t.ClassVar[str] = 'mj-head' - def handler(self): + @te.override + def handler(self) -> t.Optional[str]: + # TODO typing: fix return self.handlerChildren() diff --git a/mjml/elements/head/mj_html_attributes.py b/mjml/elements/head/mj_html_attributes.py index 93d953e..82ede27 100644 --- a/mjml/elements/head/mj_html_attributes.py +++ b/mjml/elements/head/mj_html_attributes.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -5,9 +8,10 @@ __all__ = ['MjHtmlAttributes'] class MjHtmlAttributes(HeadComponent): - component_name = 'mj-html-attributes' + component_name: t.ClassVar[str] = 'mj-html-attributes' - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] _children = self.props.get("children") diff --git a/mjml/elements/head/mj_preview.py b/mjml/elements/head/mj_preview.py index 2af8ce5..1919f54 100644 --- a/mjml/elements/head/mj_preview.py +++ b/mjml/elements/head/mj_preview.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -6,8 +9,9 @@ class MjPreview(HeadComponent): - component_name = 'mj-preview' + component_name: t.ClassVar[str] = 'mj-preview' - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] add('preview', self.getContent()) diff --git a/mjml/elements/head/mj_style.py b/mjml/elements/head/mj_style.py index 1da2024..c334f3e 100644 --- a/mjml/elements/head/mj_style.py +++ b/mjml/elements/head/mj_style.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -5,15 +8,17 @@ __all__ = ['MjStyle'] class MjStyle(HeadComponent): - component_name = 'mj-style' + component_name: t.ClassVar[str] = 'mj-style' + @te.override @classmethod - def default_attrs(cls): + def default_attrs(cls) -> t.Dict[str, str]: return { 'inline': '', } - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] inline_attr = 'inlineStyle' if (self.get_attr('inline') == 'inline') else 'style' html_str = self.getContent() diff --git a/mjml/elements/head/mj_title.py b/mjml/elements/head/mj_title.py index ff89e96..355c610 100644 --- a/mjml/elements/head/mj_title.py +++ b/mjml/elements/head/mj_title.py @@ -1,3 +1,6 @@ +import typing as t + +import typing_extensions as te from ._head_base import HeadComponent @@ -5,8 +8,9 @@ __all__ = ['MjTitle'] class MjTitle(HeadComponent): - component_name = 'mj-title' + component_name: t.ClassVar[str] = 'mj-title' - def handler(self): + @te.override + def handler(self) -> None: add = self.context['add'] add('title', self.getContent())