Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an anti-pitch for typing #1477

Merged
merged 11 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/guides.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Type System Guides
libraries
writing_stubs
unreachable
typing_anti_pitch
80 changes: 80 additions & 0 deletions docs/source/typing_anti_pitch.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
.. _typing-anti-pitch:

Reasons to avoid static type checking
=====================================

In the words of :pep:`484`::

It should also be emphasized that Python will remain a dynamically typed language, and the
authors have no desire to ever make type hints mandatory, even by convention.

The idea that dynamism in Python is a strength of the language is reflected in the fact that
Python's type system is gradual. See :pep:`483` for details, but the long and short of this is
that you can add static types to your codebase only to the extent that you want to, and static
type checkers are designed to be able to put up with this.
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

It's also worth noting that "static type checking" encompasses a spectrum of possible degrees of
strictness. On the one hand, you can set yourself up so that your type checker does almost nothing.
On the other -- well, I love type checking, but I would quit Python if I had to enable all
possible strictness checks that type checkers offer.

Anway, with all that said, here's a list of possible reasons to not use static type checking
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
in Python::

* You simply don't want to. Python is a tool that is meant to serve you. Python is a big tent,
multi-paradigm language that generally allows you to do things in the way that best suits your
needs, as best determined by you.

* The cost-benefit ratio isn't good enough. Pleasing static type checkers requires a non-zero amount
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
of busy work. If this isn't worth the extra confidence you get, you shouldn't add static type
checking.

* Your codebase fits in your developers' heads. Opinions vary, but people tend to agree that at
some number of developers and lines of code, static type checking confers significantly more
benefit. You don't feel like you're there yet.

* If you maintain high test coverage, that might provide sufficient quality assurance for your
needs (acknowledging that static type checking and tests enforce different things; static type
checking usually cannot validate logic, tests can often not prove invariants of your code to
hold).

* Your codebase is old, large and has been working fine without static type checking for years.
While Python's type system is designed to
`allow gradual adoption of static type checking <https://mypy.readthedocs.io/en/stable/existing_code.html>`_,
the total cost of adding type annotations to a large extant codebase can be prohibitive.

* Your library does enough dynamic things that type checking would be unlikely to help your users.
Either you would have to spend a lot of effort redesigning your library in ways that static type
checkers could better understand, or you'd have to invest a lot of effort into figuring out clever
type annotations to twist the arms of type checkers.

* Your codebase has suffered at the hands of `Hyrum's Law <https://www.hyrumslaw.com/>`_
and all possible observable behaviour is depended on. In order to avoid false positives for your
users, all your types end up being either a) complicated ``Protocol``s that are hard to maintain,
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
or b) ``Any`` in which case there's not much point. (On the other hand, static type checking could
be a good solution for communicating to users what behaviour they should be allowed to rely on)

* You're not opposed to type checking in theory, but you dislike Python type checkers in practice.
Maybe they don't understand enough of the idioms you use, maybe you'd like them to infer more
instead of relying on explicit annotations, maybe they're too slow, maybe they don't integrate
well with your editor, maybe they're too hard to configure. Whatever the reason -- it just doesn't
work for your project.

* Type checking in Python isn't actually strict enough, powerful enough or expressive enough for
you. Python type checkers end up making various decisions out of pragmatism, or due to limited
resources, and these decisions might not be the ones for you.
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

Advice for maintainers of untyped libraries
*******************************************
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

You've made the decision that adding static types isn't the right choice for your library. But
perhaps you'd still like to help your users who do use static type checking -- and maybe you have
some enthusiastic would-be contributors willing to help with this.

One option is encourage such contributors to publish a :pep:`561` stub-only package that is
maintained separately from your main project. You could also contribute these stubs to the
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
`typeshed <https://github.com/python/typeshed>`_ project.

If more users pester you about adding static types, feel free to link them to this document. And if
you ever change your mind, make sure to check out some of the other guides in this documentation,
and ask any questions you have over at `Python's typing discussions <https://github.com/python/typing/discussions>`_.
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved