mjml-python
mjml-python copied to clipboard
Stubs for public interface
This is a minimal PR, which (mostly) resolves #50, as discussed in #57.
For overriding components in downstream projects, the public interface of all components should be typed accordingly, but I'll leave that for the next PR (unless it's seen as pertinent to do here while we're at it).
For instance, calls like this: https://github.com/FelixSchwarz/mjml-python/blob/ed3187e2d7f22ab39848f056e7406ef0b2118aff/tests/custom_components_test.py#L28-L31
...will cause mypy to error with:
error: Call to untyped function "render" in typed context [no-untyped-call]
The same goes for e.g. default_attrs and so on, so type annotations should be added to these as soon as possible.
See mypy docs on stubs for more details.
The stubs can be deleted later, when the package is typed internally.
Also added type hints for the dictionary interface of the output (since dotmap.DotMap is also a dictionary). These can be removed if attribute access on the result should be enforced (or if treating the result as a dictionary should be discouraged).
Sorry for the long delay. I just added a few minor comments but other than that it looks good to merge.
No worries at all!
Speaking of bike-shedding, there's currently an annotation for custom_components expecting an optional list:
https://github.com/FelixSchwarz/mjml-python/blob/ed3187e2d7f22ab39848f056e7406ef0b2118aff/mjml/mjml2html.py#L22-L23
Since this list isn't actually mutated, it would be sufficient to annotate it as a typing.Sequence (or collections.abc.Sequence once Python 3.9 is used as the baseline). Should I update these while I'm at it, or leave it for later?
Sorry for letting this slip for so long. I cherry-picked your commits and pushed them in main. Release will follow later today.