PNL Learn

Type Hints and Clean Code

Write Python other developers are glad to read and maintain.

Write code the way a reviewer wants to read it. You will annotate functions and variables with type hints (collections, unions and None, Callable, Iterable, TypedDict, Literal, Protocol, generics with type variables and typed dataclasses), choose between Any and object, and read a type checker's report. Then you will follow PEP 8, pick names that explain themselves, and let formatters and linters handle the mechanical rules; refactor with tests holding behavior steady (extract functions, guard clauses, named constants, no flag arguments); write comments that explain why, remove duplicated knowledge without building the wrong abstraction, and give each function one responsibility. The course ends by untangling a messy script into typed, documented, tested functions.

IntermediatePython7 lessons82 exercisesAbout 6.5 h
Create a free account

What you will learn

  • Type hints and unions
  • TypedDict, Protocol and Literal
  • Generics and typed dataclasses
  • Reading mypy and ruff output
  • PEP 8 and naming
  • Refactoring under tests
  • Guard clauses
  • DRY and single responsibility
  • Google-style docstrings

Lessons

  1. 1

    Type Hints

    Annotate parameters, return values and variables, describe collections, unions and None, learn what hints do and do not do at runtime, and read a type checker's report.

    14 exercises

  2. 2

    Richer Types

    Accept any iterable, hint functions you pass around, name long hints, and describe dict records, fixed choices and duck-typed objects.

    14 exercises

  3. 3

    Any, object, Generics and Typed Dataclasses

    Choose between Any and object, narrow a union with isinstance, write generic functions with TypeVar, and build typed dataclasses whose annotations are the fields.

    12 exercises

  4. 4

    PEP 8 and Naming

    Follow the conventions every Python team expects, never shadow a built-in, choose names that explain themselves, and let formatters and linters handle the mechanical rules.

    14 exercises

  5. 5

    Refactoring

    Improve the structure of working code without changing what it does: extract functions, flatten nesting with guard clauses, name numbers and remove flag arguments, with tests holding behavior steady.

    12 exercises

  6. 6

    Comments, DRY and Responsibility

    Write comments that explain why, replace what-comments with names, remove duplicated knowledge without building the wrong abstraction, and give each function one reason to change.

    10 exercises

  7. 7

    Putting It Together

    Document functions in Google style, keep printing at the edge, then turn tangled code into typed, named, tested functions.

    6 exercises

How you practice

You practice in the browser and every exercise gives you feedback right away. This course uses these formats:

  • Code exercise: 29
  • Fix the bug: 14
  • Predict the output: 10
  • Fill in the blank: 9
  • Multiple choice: 7
  • Type the answer: 6
  • Match the pairs: 2
  • Spot the bug: 2
  • Select all that apply: 2
  • Reorder lines: 1

Aligned to

CS2023ACM / IEEE-CS / AAAISource
  • Software Development Fundamentals
    • SDF-PracticesSoftware Development Practices

Proto Node Labs is not affiliated with or endorsed by ACM / IEEE-CS / AAAI. Exam names are trademarks of their owners.