4 Types and docs

just setup

To learn what count takes and gives, a reader had to read its body. Now its signature says so, with types, and a docstring says in one line what it does. What count does is the same.

uv run python -c "import tally; help(tally.count)" | head -6
uv run python -m unittest discover -s tests -q

help prints the signature and the docstring, and the tests pass as before.

Ask: who reads a type hint, the person, the editor, or Python?

What changed

  • src/tally/__init__.py: the types in the signature of count, and its docstring. 2 lines added, 1 removed. The file.
    • look at def count(text: str) -> dict[str, int]:
The diff of src/tally/__init__.py
@@ -3,7 +3,8 @@
 import sys
 
 
-def count(text):
+def count(text: str) -> dict[str, int]:
+    "How many times each word of `text` appears, ignoring case."
     counts = {}
     for word in text.lower().split():
         counts[word] = counts.get(word, 0) + 1

This change on GitHub. The code after it.