Type Hints for Dynamic Class Creation

Posted on Sun, 15 Mar 2020 in Python • Tagged with python, type hinting

Type hints in Python are cool. Projects of any size can benefit from them. Actually for projects bigger than 50-60 modules or with more than 5 developers involved type hints become crucial to keep its code quality at a reasonable level. And it looks like it is my favorite topic to write about.

But sometimes, its realization in Python is counterintuitive or even confusing. You have to write a code that has no sense but typing, for example, in case of dynamic type creation and necessity of using cast function. Well, technically all type hints in Python have no sense but typing. However, in the vast majority of cases, type annotation adds clarity to the code, not confusion.

Continue reading

Want to add type annotation to a variable? Do refactoring!

Posted on Thu, 06 Oct 2016 in Python • Tagged with python, type hinting

There is the PEP 526 implemented in Python 3.6 that adds optional type annotations for variables. It's replacement for type comments. There is one pitfall: adding type annotations for local variables can hide problems with code.

Continue reading

How to use mypy on Python 2.7 project

Posted on Fri, 29 Jul 2016 in Python • Tagged with python, mypy, type hinting

If you work on a big Python project, type hinting can help you to find many design and function usage problems. Of course, you have to use external tools for static analysis, such as mypy. This is a great tool, but supports Python 3 mainly. Support for 2.7 branch isn't so straightforward. There are a couple of tricks that allows you to use it for 2.7.

Continue reading