Is typing good in python?
I know that python essentially is a duck typed language and, in a way, asking for type checking is a no-brainer considering there is no plan to make cpython statically typed. Show
The point I am not understanding is, being optional and helpful in finding bug and better documentation, why is type hinting (PEP-484 and followups) not popular among tutorials and the community in general? I am not talking about tutorials aimed at complete beginners. Consider function docstrings. There comes a state when we start documenting argument types for a function (e.g. see the numpy's guide on parameters), but most of us don't use type hints which can help both in documentation + type checking (using mypy, say) and instead use plain docstrings. Is it because of the idea being slightly contrary to python's dynamic typing rise to fame? Or is it simply not known by many? EDIT: I mean type hinting (which translates to comment based hinting for Python 2) and not just type annotations for Python 3.
When I started to learn programming, I used C as the main language. Declaring the actual type of the variable you will use was mandatory, and the compiler would complain a lot if you forget. Changing to C++ and Java later didn’t change anything. I still needed to know which type I was going to use… Image creates with carbon.now.shBefore switching to software engineering, I was working on data science projects. One surprising thing is that, despite having worked on creating a Python package for actuaries, I had never heard of type hints. Then, I had the opportunity to work on large Python projects and that is when I discovered type hints. As a daily user of Python for 3–4 years, I found it confusing to use type hints when Python is known to be a dynamically typed language. After digging into the subject, I realized the power of type hints. Through this article, this is what I will try to make you discover: what is typing, what are type hints, how do you enforce it and why do you need it or not. 1. Static typing vs Dynamic typingStatic typingStatic typing consists in declaring within programs the type of variables or class attributes at the same time as their identifier. This anticipates low-level problems but also ensures a certain degree of programming security by introducing additional rigour and allowing the compiler to detect more potential problems. An example in C: int number;number = 1; The first line declares that the variable Dynamic typingDynamic typing allows greater flexibility (e.g. changing the type of a variable at runtime) but is necessarily less efficient. However, it has the advantage of being much more manageable and allows classical problems to be solved in a much more natural and elegant way. An example in Python: number = 7.77print(type(number))>>> In this example, the 2. Python’s typing moduleIntroduced in Python 3.0 and in the standard library since Python 3.5, Python’s typing module contains the main type hints. Python is a high level language with dynamic typing. In fact, it determines the type of objects during run-time. This is why it is sometimes hard for developers to find out what exactly is going on in the code. In order to reduce and mitigate this concern, the Python’s typing module has introduced type indications. Still called type annotations, they are a good way for the programmer to indicate the type of the object used (during compilation) and to help external type checkers. This makes the code more readable, more robust and better documented for other developers. Any type error is identified when an external type checker is executed. It is not intended to check the type at compile time or runtime. For example, if the object returned by Python is not the same type as the one specified, there will be no compile-time error. The most common tool for doing type checking is Mypy (Dropbox). But it is also possible to use Pytype (Google), Pyright (Microsoft) or Pyre (Facebook). ⚠️ Thus, it is important to note that typing in Python is only code documentation and help for external type checkers. It does not make Python a statically typed language. 3. Type hints — AnnotationsIntroductionIn order to implement type hints in your Python code, you will need to use what are called annotations. This notion of annotation was introduced in Python 3.0, originally without any specific purpose. In September 2014, Guido van Rossum (Python BDFL), thanks to the work Jukka Lehtosalo had done on his PhD project (Mypy), created a Python Enhancement Proposal, PEP-484 to add type hints to Python through annotations. Type annotationsType annotations is the straightforward way and is the one you’ll find mostly mentioned on the Python’s typing documentation. It uses function annotations added to language with PEP-3107 (Python 3.0+) and variable annotations with PEP-526 (Python 3.6+). These allow you to use the Function AnnotationsThus, the syntax to annotating a function (its arguments and the return value) is as follows: ⚠️ The annotation must be a valid Python expression. To better understand, let’s take as an example a function Now, the same function but with annotations: Variable AnnotationsIn the definition of
4. Mypy, a static type checkerImage from https://github.com/python/mypyWhat is Mypy ?
How to install and run Mypy ?
$ python3 -m pip install mypy
$ mypy program.py
Example with the sphere_volume() functionIf we run the example through Python and Mypy: $ mypy sphere_volume_mypy_example_1.pySuccess: no issues found in 1 source file In the terminal output, Mypy tells us that there are no issues. Now,
if you have errors, for instance if you call $ mypy sphere_volume_mypy_example_2.pysphere_volume.py:7: error: Argument 1 to “sphere_volume” has incompatible type “str”; expected “float”Found 1 error in 1 file (checked 1 source file) 5. How to use Typing on an existing projectMyPy supports the concept of gradual typing. This means that you can gradually introduce types into your code. Code without type hints will be ignored by the static type checker. Therefore, you can start adding types to critical components, and continue as long as it adds value to you. There are also type annotation generators. When you want to add type annotations to an existing code base, use them to automate the most boring part. 6. Pros and Cons of using type hints and static type checkersWhy should you use type hints and static type checkers ?Type hints:
Static type checkers:
Why should not you use type hints and static type checkers ?
7. The future of the Python type hintsType Hinting Generics In Standard CollectionsWith type annotations, since Python 3.9 and the PEP-585, it is now possible to use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing. With the following example from the Python documentation : The code becomes : This modification in the import of type hints makes a large part of the typing library obsolete. Will type hints be used to do low-level optimizations?Several ongoing projects aim to use type hints to compile Python modules to C extensions in order to generate fast code. For example :
ConclusionEven though typing
is an optional feature in Python, it was introduced to take advantage of some of the benefits of static typing. Does typing make Python faster?According to the non-goals in the PEP 484 documentation, type checking and performance optimization is dependent on third-party tools or left to the programmer. So in short: no, they will not cause any run-time effects, unless you explicitly make use of them.
What is Python typing used for?Introduced since Python 3.5, Python's typing module attempts to provide a way of hinting types to help static type checkers and linters accurately predict errors.
Is Python type safe?4.2.
Python is a semi type safe language. Python is a dynamically and strongly typed language that has a high degree of type safety control built-in. But the type checking is done only at the run time. So, we can say that Python is not a 100% type safe language.
What are reasons for using type hinting?As the code base gets larger, type hints can help to debug and prevent some dumb mistakes. If you're using an IDE like PyCharm, you'll get a warning message whenever you've used the wrong data type, provided you're using type hints.
|