Skip to content

← Getting Started with Tests and Type Checks step 2 of 13

Easy Primitives

Fix Your First mypy Errors

A teammate wrote two small helpers, and every visible test passes. mypy still rejects the file, and one of its errors comes from a real bug that no visible test catches.

  • initials(full_name) returns the first letter of each word, in capitals. "Ada Lovelace" gives "AL".
  • greeting(name) returns "Hello, Ada!" for the name "ada". When name is None, it should return "Hello, guest!".

What mypy reports

Submit the starter as it is, and mypy reports three errors:

L9:1 [no-untyped-def]
Function is missing a type annotation

L16:24 [union-attr]
Item "None" of "str | None" has no attribute "title"

L22:21 [no-untyped-call]
Call to untyped function "initials" in typed context

What to fix

  • Annotate initials. It takes a str and returns a str. That fixes the first error, and also the third, which comes from calling a function that has no annotations.
  • In greeting, return "Hello, guest!" when name is None, before the code calls .title(). The hidden tests pass None, so the starter fails them as well as failing mypy.

Keep solve as it is.

What the tests check

  • initials gives one capital letter per word and ignores extra spaces. An empty string gives "".
  • greeting puts name.title() between "Hello, " and "!", or returns "Hello, guest!" when name is None.