hynek / pem

PEM file parsing in Python.
https://pem.readthedocs.io/
MIT License
159 stars 37 forks source link

Add typing information #31

Closed hynek closed 6 years ago

hynek commented 6 years ago

So um @ethanhs does this look overall sensible to you? I consider it a finger exercise before moving to more complex projects including attrs.

Also ping @ambv for good measure.

Is this an approach we could take on attrs? One thing I’ve noticed is that we definitely lose cast().

ethanhs commented 6 years ago

So um @ethanhs does this look overall sensible to you?

I mean it will work here, and seems to make sense. You aren't doing anything too complicated.

Is this an approach we could take on attrs?

Probably not, as you need some runtime typing things Generic, overload, etc. in attrs.

hynek commented 6 years ago

What about the TYPE_CHECKING thingie I’ve seen thrown around? I know that there’s one in the typing module but I saw people saying that even if I go the X=False; if X: it has to be called the same way?

ethanhs commented 6 years ago

TYPE_CHECKING might help here, but as I said in the attrs PR, you will likely end up duplicating things (eg overload, Generic) in the else branch.

ilevkivskyi commented 6 years ago

Also don't forget to add the new cool badge to your README :-)

Checked with mypy

hynek commented 6 years ago

Wait what do you mean by “duplicating things”? I’m just talking about how to literally write down the currently if False: branch and if it makes semantically any sense to introduce an TYPE_CHECKING variable (instead of bare False or your suggested MYPY).

ethanhs commented 6 years ago

It is more idiomatic to use MYPY, TYPE_CHECKING was added to typing to avoid import cycles and such. Mypy itself uses if MYPY: to work around some missing features in 3.5.1. But it is up to personal preference in the end.

So here is an example:

MYPY=False
if MYPY:
    from typing import Generic
else:
    # define a class that pretends to be Generic

class A(Generic):
    pass

Also I'm not sure I understand what you mean by

I saw people saying that even if I go the X=False; if X: it has to be called the same way?

What is "it" here and what do you mean by it being called?