miguelgrinberg / Flask-HTTPAuth

Simple extension that provides Basic, Digest and Token HTTP authentication for Flask routes
MIT License
1.27k stars 228 forks source link

Type annotations / type hints / stub files for mypy #112

Closed MartinThoma closed 4 years ago

MartinThoma commented 4 years ago

Are there plans to add type annotations or stub files for mypy to flask-httpauth?

As you seem to want to support Python 2, I guess stub files would be the way to go. Would you be open to a PR?

miguelgrinberg commented 4 years ago

I do not really have strong feelings about Python 2. I supported it in the past, but now I have no problem dropping it, and in fact I have dropped it on many of my other projects.

But I do feel a bit reluctant to add type hints directly into the source code. I don't think such a simple extension, which relies only on primitive types, will benefit much from them. And type hints have a way of making simple code look harder to parse visually (personal opinion, of course).

The ideal solution for me would be to have a stub file hosted externally and maintained by someone who benefits from this.

MartinThoma commented 4 years ago

The ideal solution for me would be to have a stub file hosted externally and maintained by someone who benefits from this.

I can understand your reason not to add the type annotations within the source code. Why would you prefer not to include stub files in the repository?

The big advantage to me of having type annotations or stub files directly within the project is that every user will automatically benefit from it. I've asked this question because I had a look at where a project's type coverage is not so great and Flask-HTTPAuth popped up. I hoped that somebody might have created type annotations for it already (and I'm still hoping :crossed_fingers: )

miguelgrinberg commented 4 years ago

Why would you prefer not to include stub files in the repository?

I do not have interest in maintaining the stub file.

MartinThoma commented 4 years ago

Ah, ok, I understand that. I guess then we can close this here.

Can people still comment here as a central point for this topic? I would like to try to write a mypy plugin, similar to sqlalchemy-stubs, but I haven't created a mypy plugin so far. I will need a bit of time .

miguelgrinberg commented 4 years ago

Can people still comment here as a central point for this topic?

Of course.

MartinThoma commented 4 years ago

I will develop this here: flask-httpauth-stubs. I've just added a stub generated by MonkeyType. I still need to figure out how to tell mypy to use it.

Unrelated side note: Thank you so much for writing your Mega-Tutorial! This is how I got into web development with Python :-) I love the tutorial - it's awesome! :heart:

miguelgrinberg commented 4 years ago

@MartinThoma That's awesome. Once you have this let me know and I'll reference it in the docs for those who are interested.

MartinThoma commented 4 years ago

@miguelgrinberg It seems to work now :tada: https://pypi.org/project/Flask-HTTPAuth-stubs/

miguelgrinberg commented 4 years ago

@MartinThoma you left some actual Python code in the HTTPDigestAuth class.

Also, how will people know which version of your stubs goes with a given version of the package? It seems people interested in types don't care about that level of accuracy?

MartinThoma commented 4 years ago

you left some actual Python code in the HTTPDigestAuth class.

Yes. I wasn't sure how to tell mypy that the class has attributes, so I left the constructors as they are.

Also, how will people know which version of your stubs goes with a given version of the package?

I've added Flask-HTTPAuth as a dependency and set a minimum version: https://github.com/MartinThoma/flask-httpauth-stubs/blob/main/setup.py#L37

I wasn't sure about best pracitices there. Maybe rather Flask-HTTPAuth==4.*?

miguelgrinberg commented 4 years ago

I missed the minimum version, I guess that's better.

I honestly don't know how this is supposed to work. The typeshed project does not appear to be trying to match versions either (or maybe I'm not familiar enough with it to understand how they do it).