gunicorn icon indicating copy to clipboard operation
gunicorn copied to clipboard

Start adding type annotations to gunicorn

Open MartinThoma opened this issue 3 years ago • 14 comments

See issue #2374 and issue #1393

MartinThoma avatar Jul 12 '20 16:07 MartinThoma

Hi guincorn developers! First of all: A big thank you for gunicorn 🤗 I've used it for several projects and never had to think a lot about that part :-) I'm currently annotating a code base and noticed that gunicorn does not have type annotations, sooo ... here you are :-)

As I'm not familiar with the codebase, this will require some work and help of people who know gunicorn.

In case you want to read more about type annotations: I wrote an article :-)

How to get type annotations

This is how I would like to go forward and add types to gunicorn:

  1. Make sure mypy doesn't crash anymore. Decide on stub files vs in-line annotations. (In the PR you see the in-line annotations example; let me know if you think stub files would be the better idea)
  2. Integrate mypy in CI pipeline: Let me know if you prefer not to do that; note that this would only fail if mypy discovers a new problem - when new code without annotations is added, it is not checked and thus does NOT fail. Those settings can be adjusted, of course.
  3. Increase typed parts
    • Try to get rid of "type[ignore]" / Any / untyped generics
    • Type untyped functions (currently, I mainly added annotations for the config.py)

Impossible at the moment

  • Using "real" annotations everywhere (variable annotations were introduced in Pyhton 3.6)
  • Have "nice" annotations when a class returns itself (from __future__ import annotations was introduced in Python 3.7)
  • Using TypedDict instead of Dict (introduced in Python 3.8)

MartinThoma avatar Jul 12 '20 17:07 MartinThoma

I see a lot of linting errors. I'm used to the black formatter. Would it be ok to apply it? Do you have other autoformatters?

MartinThoma avatar Jul 12 '20 17:07 MartinThoma

I see a lot of linting errors. I'm used to the black formatter. Would it be ok to apply it? Do you have other autoformatters?

No autoformatter is and will be used. (one readon is to keep some flexibility). If you see any pep8 issue, let's fix them in a separate commit :)

benoitc avatar Jul 12 '20 17:07 benoitc

I see a lot of linting errors. I'm used to the black formatter. Would it be ok to apply it? Do you have other autoformatters?

No autoformatter is and will be used. (one readon is to keep some flexibility). If you see any pep8 issue, let's fix them in a separate commit :)

I meant at the project level. I need to publish some style rules so anyone can adapt their auto-formatter in a way we keep the code readable and not increasing that much the number of lines like some formatter do... If you use one in your env, just make sure to apply the change on a separate commit :)

benoitc avatar Jul 13 '20 14:07 benoitc

I applied black to the config.py to solve the linting issues and fixed the remaining issues. Black mostly has done two types of changes:

  • Single quotes to double quotes
  • The way of line breaks for long parametrized funtions

Current:

def foo(parameter_a,
        parameter_b,
        parameter_c):
   body

Black:

def a_function(
    parameter_a,
    parameter_b,
    parameter_c
):
    body

I think yapf would support the current style, but I'm not familiar with yapf.

MartinThoma avatar Jul 13 '20 20:07 MartinThoma

@benoitc I guess you are busy with other task, but just to make it clear: I'm waiting for feedback. From my side, this PR could be merged. Once it is merged, I will try to add mypy to the CI pipeline :-)

MartinThoma avatar Jul 21 '20 16:07 MartinThoma

@benoitc Did you have the time to have a look at the PR?

MartinThoma avatar Sep 03 '20 18:09 MartinThoma

@tilgovi I've added the more specific type annotations you mentioned and undid the changes black did to config. I've only applied black to that file and that is also where most of the type annotations were added.

MartinThoma avatar Sep 08 '20 06:09 MartinThoma

@tiangolo Is there anything blocking this PR?

MartinThoma avatar Oct 06 '20 06:10 MartinThoma

@benoitc Is there anything blocking this PR?

MartinThoma avatar Oct 06 '20 06:10 MartinThoma

I still like this idea and would love to add type annotations. I would prefer to use the annotation syntax, rather than comments, now that Gunicorn is Python 3 only. I'll ping people directly to see if I can get some consensus from folks about starting to add these types.

tilgovi avatar Feb 16 '21 02:02 tilgovi

Pls don't forget the py.typed marker 🙏

Also, partial stuff (like merging this MR) is better than no stuf

Also related to https://github.com/benoitc/gunicorn/issues/2833?

stdedos avatar Oct 11 '23 10:10 stdedos

I didn't get any feedback for over 3 years. I'm not going to put more effort in this.

MartinThoma avatar Oct 12 '23 16:10 MartinThoma

That is ... very respected ❤️

stdedos avatar Oct 12 '23 19:10 stdedos