question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce type hints PEP484 and integrate mypy into CI/CD

See original GitHub issue

This issue was opened to discuss if nornir should introduce type checks and mypy.

Mypy is a static type checker for Python 2 and 3. It is quite smart and can infer a lot of types based on the code and type hinting which was introduced with PEP 484 Prior to Python 3.6 type hints were specified using comments: # type: <type>, but Python 3.6 introduced new type hinting using: var: <type> = <value> While some perceive it as ugly, it really helps (at least in my opinion):

  • easier to document types there instead of docstrings, which can easily become outdated (just to clarify, I am not saying to remove docstrings, just types from them)
  • easier to understand the code with types
  • static type checker like mypy can catch some bugs early
  • with more adoption, library maintainers will be pushed to migrate to it or provide stub files describing types so that application using the library can leverage those types

I am definitely not a mypy expert, but I was helping certbot (medium codebase, around 35k LoC) to migrate to mypy during PyCon sprints. We caught a dozen of small bugs and found places with weird code that definitely require refactoring.

The strategy was:

  1. Add mypy with weak checks
  2. Fix all found problems
  3. Add mypy to CI/CD
  4. Switch to stronger type checking settings one at a time
  5. Repeat 2-4

As a point of reference we used this document written by Zulip team who successfully migrated to mypy for even larger codebase. Good presentation to understand type hints and how to do it in real-world: Carl Meyer - Type-checked Python in the real world - PyCon 2018

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dbarrosopcommented, Jul 10, 2018

I don’t think we need to rewrite anything for asyncio, we will need to provide a new facility as you suggested though but in the end nornir may end up having a run and an async_run mode. We will have to experiment a bit with this.

Regarding futures, to provide all that functionality there are bunch of very heavy classes that don’t scale well. You are free to test but while we could scale current solution to process thousands of devices in seconds, using futures took instead minutes.

Regarding py36+ syntax, go for it. I have a PR open already to get rid of anything <py3.6. Even though it’s going to make it harder for some people to use I think it’s worth it. Just for the sake of completeness, the comparison with black is not fair as you run black in your development machine or CI but nornir will end up running in bastions and control machines which sometimes are… heavily outdated. In any case, I think we should encourage people to containarize their nornir applications so we can focus on writing clean and modern code.

0reactions
dbarrosopcommented, Jan 7, 2021

Closing this as I think it’s completed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mypy and Continuous Integration sequence part 1 - Medium
Today, we will show how a simple program can be improved by type hints. Then, I will introduce the two most basic concepts...
Read more >
PEP 484 – Type Hints - Python Enhancement Proposals
This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime...
Read more >
Type hinting in Python, and testing with GitLab - cylab.be
To check that your code uses methods correctly (or at least according to type hints), you should use mypy. Installation: python3 -m pip...
Read more >
Python's “type hints” are a bit of a disappointment to me
My team began using type hints and aggressively applied mypy to new code starting ... before PEP 484 was published, so the core...
Read more >
How to Use Data Types & the Python Type() Function
A type is assigned to a variable when it is assigned a value. Gradual typing (type annotations/type hints) was introduced in PEP 484....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found