# Consider adding type hints to clinic.py

**URL:** <https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320>\
**Category:** Core Development\
**Tags:** typing\
**Created:** [April 29, 2023, 8:06pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320 "2023-04-29T20:06:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [April 29, 2023, 8:06pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/1 "2023-04-29T20:06:45Z")

</div>

I’ve done a handful of PRs on the 5367 lines\[1\] long [Tools/clinic/clinic.py](https://github.com/python/cpython/blob/main/Tools/clinic/clinic.py), and every time I touched that file, I had to use considerable amounts of time to figure out all the single-letter named parameters and variable names. I often find myself thinking that clinic.py would have been so much easier to maintain if it only had had type hints. So here the crazy idea: let’s add type hints to clinic.py. Hypothesis: it will make it easier to triage clinic issues, review clinic PRs, fix clinic bugs and implement new clinic features.

Thoughts?

* * *

1. 2023-04-29, HEAD at `85c7bf5bc`

---

<div class="post-metadata">

**Author:** ![tusharsadhwani](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tusharsadhwani/32/12278_2.png) [@tusharsadhwani](https://discuss.python.org/u/tusharsadhwani)\
**Post date:** [April 29, 2023, 9:29pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/2 "2023-04-29T21:29:30Z")

</div>

From a typing perspective the code seems rather straightforward. Just need to add types to function signatures and empty list/dict/set assignments.

I can’t say anything about why it’s not typed yet, but if it ends up happening I can join the effort.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [April 30, 2023, 9:55am UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/3 "2023-04-30T09:55:57Z")

</div>

> [@tusharsadhwani](#):
>
> I can’t say anything about why it’s not typed yet, […]

Adding type hints to Python source code in the CPython code base has been a controversial issue, which is why I brought it up here. However, since Argument Clinic is part of `Tools/`, and not a part of the standard library (`Lib/`), I expect it to be less of a controversial issue. For example, [Tools/build/check\_extension\_modules.py](https://github.com/python/cpython/blob/main/Tools/build/check_extension_modules.py) is typed.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [April 30, 2023, 10:36am UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/4 "2023-04-30T10:36:25Z")

</div>

> [@erlendaasland](#):
>
> Adding type hints to Python source code in the CPython code base has been a controversial issue, which is why I brought it up here.

Prior discussion:

> [@Type annotations in the stdlib](https://discuss.python.org/t/type-annotations-in-the-stdlib/21487):
>
> I’d like us to think carefully about this. I agree that this might be nice for “simple” type annotations, where an argument can e.g. only be a str or whatever. But I’d be wary of starting a large-scale project to add type hints in lots of places. As a typeshed maintainer and a codeowner for typing.py, I’m obviously pro-typing. But I’ve also seen how “apparently simple” functions can get very complicated to add annotations for. There will be a lot of functions where we won’t be able to add typ…

As I said earlier, tooling is a less controversial topic.

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [April 30, 2023, 11:32am UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/5 "2023-04-30T11:32:24Z")

</div>

FWIW, I don’t think any of the points I made in that^ thread apply to things outside the `Lib/` directory 🙂

I don’t know much about clinic.py, so I can’t comment on how much adding type hints is likely to help or hinder development of the code! But I’m (obviously) generally pro-typing, and a believer that it generally makes code easier to read and reason about. Happy to help out if you need a hand on anything.

There are tools such as [autotyping](https://github.com/JelleZijlstra/autotyping) that might be worth trying, which can add an initial layer of type hints to the code automatically.

Would we run a type checker on clinic.py to validate that the type hints are correct, or would they just be for human readers? (I’d advocate using a type checker, as it’s very easy for type hints to go out of date otherwise. If you configure the settings correctly, you should be able to use “gradual typing” — the type checker will only emit errors on functions with type annotations, and will ignore all other functions. This allows you to incrementally add more annotations to the code.)

---

<div class="post-metadata">

**Author:** ![kj0](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@kj0](https://discuss.python.org/u/kj0)\
**Post date:** [April 30, 2023, 1:25pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/6 "2023-04-30T13:25:57Z")

</div>

I think it’s fine to add type hints to clinic. FWIW, the cases generator used in Python 3.12’s interpreter generator is almost fully type hinted [cpython/Tools/cases\_generator at main · python/cpython · GitHub](https://github.com/python/cpython/tree/main/Tools/cases_generator)

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [April 30, 2023, 2:31pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/7 "2023-04-30T14:31:25Z")

</div>

> [@AlexWaygood](#):
>
> Would we run a type checker on clinic.py to validate that the type hints are correct, or would they just be for human readers? (I’d advocate using a type checker, as it’s very easy for type hints to go out of date otherwise.

Sound like something we’d want as a non-required check in our CI.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [April 30, 2023, 2:44pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/8 "2023-04-30T14:44:24Z")

</div>

Based on the feedback so far, I’ll create an issue for this. Since I’m in the «typing-ignorant-camp» of core devs, I’d like to try to come up with a PR for this. Who knows, maybe I end up in the pro typing camp 🙂

Thanks for your input, Tushar, Alex, and Ken.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [July 29, 2023, 2:34pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/9 "2023-07-29T14:34:13Z")

</div>

… and now, 44 PRs later, Argument Clinic is fully typed. Big thanks to everyone involved! It’s been fun 😃

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [July 29, 2023, 6:37pm UTC](https://discuss.python.org/t/consider-adding-type-hints-to-clinic-py/26320/10 "2023-07-29T18:37:38Z")

</div>

> [@erlendaasland](#):
>
> Who knows, maybe I end up in the pro typing camp

Oh, for the record: I did.
