Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

New Course: Mypy Primer #10

Open
tusharsadhwani opened this issue Oct 21, 2021 · 25 comments
Open

New Course: Mypy Primer #10

tusharsadhwani opened this issue Oct 21, 2021 · 25 comments

Comments

@tusharsadhwani
Copy link
Contributor

tusharsadhwani commented Oct 21, 2021

About the course

Mypy is Python's official type checking system, which integrates seamlessly into Python. It has been slowly maturing over the past few years, and at this point it has become a solid Python tool that everyone should be using to create durable, error-free Python projects. This course is meant to be a comprehensive introduction to mypy and Python's builtin typing module, making you proficient with mypy.

Title
Mypy Primer

Subtitle
Learn how to implement static type checking in Python.

Description

It should be no surprise that Python is a dynamically typed language. And indeed, having dynamic types in your code is one of the best features of Python. But dynamic types has its own problems as well, as it takes away a great feature that statically typed languages like C++ and Java provide: You can't accidentally pass the wrong types in them. If you pass an integer where a string is expected in Java, your code won't compile. But in Python, your code will start just fine, until it crashes with a TypeError while running. Wouldn't it be great, if you could optionally write types in Python, to get the same type checking guarantees that statically typed languages have? Mypy lets you do exactly that!

Mypy is Python's official type checker. It's a really valuable and mature tool that interacts seamlessly with Python, to provide really powerful features like type safety, null safety, static duck typing, and generics, allowing you to have your Python code checked with a solid type system.

Mypy Primer will take you from knowing nothing about mypy, to being able to skillfully implement static type checking in your projects.

Course Outline

1. Introduction to mypy

  • What is mypy?
  • Introduction to Python's type annotation syntax
  • Using mypy to type-check your code
  • Installing mypy in your code editor

2. Mypy errors, and how to debug them

We'll see what kind of errors mypy will throw at you, how to configure mypy to make the best use of it, and try to figure out what its errors mean.

  • Why doesn't mypy show an error here?
  • Mypy configuration
  • Simple type checking examples
  • Reading mypy error messages
  • Using reveal_type() to debug types

3. Mypy types, and the typing module

Modern Python has a LOT of type checking features baked into it. In this module we will see a glimpse of what the typing module has to offer.

  • Lists, Dictionaries, and other collections
  • Union and Optional types
  • Type checking your classes
  • Typing *args and **kwargs
  • The "Any" type

4. Diving deep into the typing module

The typing module is built into Python, and it provides various functions and base classes, that provide the basis of a lot of concepts of the type system. In this module we will take a look into a lot of these functionalities.

  • Static duck typing
  • The NamedTuple and TypedDict classes
  • Function overloading

5. Some advanced type checking concepts

In this module we will learn some concepts, tips and tricks on how to properly type-check code using some of the more complicated Python concepts.

  • Typing decorators
  • Typing generators
  • Typing context managers
  • The Type type
  • Typing exercises

6. Conclusion

In this module, we will wrap up the series, and understand where type checking sits in the ecosystem of Python, and your software development process.

  • Type checking, or writing tests? Why not both!
  • Integrating mypy into your software development lifecycle

References

Some of this course will be referencing from one of my blog posts: The Comprehensive Guide to mypy.

@anandology
Copy link
Contributor

@tusharsadhwani Thanks for proposing this course. One of the thing that is not very clear, even for experienced Python developers is how to set up mypy in your editor and how to run it as part of tests. Could you please include that as well?

@tusharsadhwani
Copy link
Contributor Author

tusharsadhwani commented Oct 22, 2021

@anandology oh yes, definitely. I don't know if it should be its own chapter? But I'll have those in the course.

@anandology
Copy link
Contributor

@kracekumar could you please review this course?

@anandology
Copy link
Contributor

One of the things that would be good to have as part of this course is examples to show how to use typing and also exercises that students can try online. What do you think @tusharsadhwani?

@tusharsadhwani
Copy link
Contributor Author

Yes @anandology, the idea I had was to have interactive examples in each lesson, with some exercises as well

@kracekumar
Copy link
Collaborator

@anandology Should I review the outline in the issue or when the actual content is in a PR?

@tusharsadhwani
Copy link
Contributor Author

@kracekumar only the outline exists as of now.

@anandology
Copy link
Contributor

@tusharsadhwani I think the description of the course requires a relook. Imagine student who don't have too much experience with Python and never heard of mypy. It would be too hard to understand what is this course even about. I think it would be good to explain the context in more detail.

@tusharsadhwani
Copy link
Contributor Author

Yeah you're right. I'll try to expand on that. Any specific points that you'd like me to mention @anandology ?

@kracekumar
Copy link
Collaborator

kracekumar commented Oct 23, 2021

@anandology @tusharsadhwani The content and other parts can be added, enhanced, and expanded.

Another approach is to use existing Python or Python Primer course material to build upon for the mypy course which will actually help the audience. In the future, it can become a good natural fit to introduce mypy as part of a python course similar to creating a python package, etc. That will also send a message mypy complements the existing Python course.

There may be cases for adding new content to focus on adding annotation for advanced topics which can be added in the course. I'll let @tusharsadhwani decide.

From the content point of view, mypy gets harder with generics, classes, custom inheritance chains, and annotating third-party code. @tusharsadhwani do you plan to include them or avoid them?

At a high level, is this course going to be 101 or 201 or both?

@tusharsadhwani
Copy link
Contributor Author

I want it to be a 101, at least in the beginning.

I'd like to structure the course and lessons in such a way that I'd be able to continue appending advanced concepts to lessons as needed. For eg. the section on generics can have more chapters added in the future that cover things like covariance. Another example would be to add a whole section about protocols later on.

@tusharsadhwani
Copy link
Contributor Author

tusharsadhwani commented Oct 23, 2021

There's 4-5 bullet points in each section, but many of those could take 2-3 videos and exercises to really understand. I'm estimating 45-50 videos even in the 101 course.

@anandology
Copy link
Contributor

At a high level, is this course going to be 101 or 201 or both?

That is very good question. I would recommend dropping async, context managers and decorators etc. if you are planning for a 101 course.

@tusharsadhwani
Copy link
Contributor Author

tusharsadhwani commented Oct 23, 2021

The high level idea that I have with the content, is to provide enough context to mypy such that the person taking the course is able to confidently write annotations for all the Python code that they have written, or have an idea of what else they can read up on. Does that qualify as 101? I'd say it does, if the content is geared towards teaching someone completely new to it.

It would involve covering things like decorators imo. It doesn't need to go into depth about decorators themselves, but more so how you'd reason about adding type hints to it. So the idea of decorators can be introduced as simply as possible, and then focus on the type checking aspect.

It would also involve briefly touching on protocols for eg., just so they know it exists and the places where they might want to use it.

I'd be fine with dropping async for now though, it's very new and most beginners won't be writing async Python code.

@anandology
Copy link
Contributor

anandology commented Oct 23, 2021 via email

@tusharsadhwani
Copy link
Contributor Author

Yup, that sounds good.

I'll be updating the course outline with a more detailed version tomorrow.

@kracekumar
Copy link
Collaborator

Just knowing protocols exist is of much use. You could put efforts to make
them understand why using protocols is a good idea, but that is quite a lot
of work and I'm not sure that will fit well with the rest of the course.

Protocol is part of nominal typing and will be advanced topic, skipping will be better for 101 course.

Good decision to drop async too.

@anandology
Copy link
Contributor

Sounds great. @tusharsadhwani can you send a PR for the course with a course.yml file? Please see the other courses for reference.

@tusharsadhwani
Copy link
Contributor Author

Yup, will do.

@tusharsadhwani
Copy link
Contributor Author

tusharsadhwani commented Oct 25, 2021

@anandology can you re-read the description, see if it's easier to understand?

@anandology
Copy link
Contributor

Looks good. Can you focus on refining the lesson titles now.

I think Introduction to Python's type annotation syntax should be Introduction to function annotations.

I guess you can start with a PR to create a course.yml file with the outline. We can discuss the further refinements there.

@anandology
Copy link
Contributor

@tusharsadhwani, the course is published on mon.school with the outline. Here are two next steps.

  1. Create a teaser video about the course
  2. Create the content for the first chapter

@anandology
Copy link
Contributor

This course is mostly ready and we'll go live soon.

Here is the preview:
https://monschool.netlify.app/mypy-primer/introduction/what-is-mypy/

@kracekumar it would be great if you can review the course.

@kracekumar
Copy link
Collaborator

@anandology Sure thing! I'll do it over coming weeks. I'll go over all the contents and complete by 21st Mar?

@kracekumar
Copy link
Collaborator

Feedback

Tech

Non-tech

  • Overall content is looks good, but writing mixes both active and passive voice. It'll be better to use active voice.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants