leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add type annotations for e.g. mypy

vmiklos opened this issue · comments

Hi,

We use yattag in osm-gimmisn, and it works great, thanks for your project. We also use mypy there, and at the moment we blacklist type checking for yattag as type annotations are not available in yattag.

Would you be interested in python2-compatible type annotations for yattag in a pull request? I'm happy to work on that, just want to make sure they would not be rejected as-is. :-)

So far I only worked with type annotations where the baseline was python 3.5, but I believe it's possible to do this in a way so that even python 2.6 keeps working.

Thanks,

Miklos

Hello Miklos,

thanks for your message and sorry for the very late reply. I lacked the time to determine if I thought that would be a good idea or not.
Personally, I use a lot of duck typing in my own codebase, mainly to produce documents in multiple languages (so sometimes, my strings aren't real strings etc...). So I was a bit hesitant. However, after further reflexion:
- type annotations wouldn't prevent my code from working (at worst, I just wouldn't use them with yattag myself)
- in my codebase, I actually subclass the yattag.Doc class to achieve the translation features, so I could probably just adjust the annotations in the subclass
- type annotations would be useful to most people
- they help to understand the code
So, if you want to do it, I'd be happy to review your code and probably include it in yattag.

Yes it would be important that it's still compatible with Python 2.6. I know some yattag users still use that version of Python.

If you decide to do this, keep in mind that attribute values (created with the tag, stag, attr... functions, ie doc.attr(myattr=value)) can be strings, ints or floats (but nothing else).

Have a nice day.

OK, one thing I'm not sure about is 2.6; type hints require the typing module, and I know that works with 2.7, but not sure about 2.6. the str/int/float union type is not a problem at all. Let me annotate just indentation.py as a start, then we'll see.

The changes from your work on this have been incorporated in version 1.13.0 of yattag.
You can pip --install --upgrade yattag to obtain the new version.
Thanks for your contribution!

Ugh, sorry, turns out I forgot about an empty py.typed stamp file as documented here:

https://mypy.readthedocs.io/en/latest/installed_packages.html#making-pep-561-compatible-packages

Without that, mypy does not consider type hints in code (vs in stub files) for external modules. I'll submit a separate PR for that. Too bad I only notice this now, after you nicely already tagged a release. :-)

Your new pull request was merged and is now part of yattag version 1.13.1 on pypi.

Thanks! FYI, vmiklos/osm-gimmisn@123e5c2 is an example on how client code benefits from this work.