koodaamo/tnefparse

use type hints & mypy

petri opened this issue · 5 comments

petri commented

There's a lot of byte & string handling and so on that are easy to get wrong accidentally. Having some type hints and mypy checking them as part of tests would help catch errors & increase code quality.

👍 I actually tried to get this going a week or so a go. I struggled with the difference in types between py2 and py3. I think if we used the future shims consistently, we could get it to work. Unfortunately we are limited to using comment based type hints to maintain support for py2 and early py3 versions rather than the annotation style. And .pyi files cannot be used to validate the files they shadow, which prevents that option.

FYI we are using this in our local typeshed:

# tnefparse/tnef.pyi

from typing import Union

class TNEF:
    htmlbody: Union[str, bytes, None]
    def __init__(self, data: bytes, do_checksum: bool = True): ...

I have a typed version locally that was created with https://github.com/dropbox/pyannotate while running the tests. It gets us a lot of the way there. I should have some time in the next few weeks to come back to this, but if anyone else wants to try feel free. I do suggest the pyannotate approach.

@jrideout Is this still something you would like to do?

Otherwise I'd like to type annotate the project.

Either way, thanks a lot for the hint about pyannotate - works like a charm.

@jugmac00 Please go-ahead, I'm not actively working on the typing

Currently, I have not much spare time, but I am looking forward to type this project - I will learn a lot, I guess :-)