Does Python type hint (annotations) cause some run-time effects? [duplicate]
Asked Answered
H

2

41

Can Python function annotations and type hints (PEP 3107 and PEP 484) cause some run-time effects?

Could it made the code faster? Or shrink the usage of memory? Or otherwise it would make code more slow?

Hyrax answered 17/1, 2017 at 8:42 Comment(4)
I think that type hints and annotations are merely syntactic sugar to be passed by 3rd party tools, implying they should not effect the code performance significantly in the same way that comments don'tJeanett
Quote from the linked duplicate: "(C)Python currently (and for the foreseeable future) just discards any hints you offer and continues executing dynamically as it always does."Questionless
I think my question is a sameHyrax
Who marked this as a duplicate? The other question is only tangentially related and the replies talk about something totally different.Lexicologist
J
44

Type hints and annotations do provide attributes (see typing.get_type_hints) that can be passed by 3rd party tools but native CPython will not type check these at runtime, so this should not affect the code performance significantly in the same way that comments don't. I ran some tests with timeit and removing type hints had a negligible effect (not distinguishable from the background noise) on the run time, so any concerns about performance would certainly be a severe case of premature optimization.

From PEP 484:

While the proposed typing module will contain some building blocks for runtime type checking -- in particular the get_type_hints() function -- third party packages would have to be developed to implement specific runtime type checking functionality, for example using decorators or metaclasses. Using type hints for performance optimizations is left as an exercise for the reader.

Jeanett answered 17/1, 2017 at 9:5 Comment(3)
Python 3.7 introduced changes that improved start up performance for code using type hints - doesn't that indicate that there is a performance impact, at least prior to 3.7 in python start up times?Dresser
There is a performance hit when loading the modules, but not later on. The changes in python3.7 are breaking changes disabled by default. They turn all type hints into strings instead of using actual classes or things from the typingmodule.Lexicologist
There are even performance penalties for some annotations - like creating instance of bare generic class (class X(generic[int]) is 3-5x times slower due to overriden __new__() method (fixed in pyhon3.9 by removing the method)..Stowers
S
8

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.

Snooperscope answered 17/1, 2017 at 8:53 Comment(2)
That is incorrect. They need to be resolved when loading the code and they are kept in memory. After the loading there shouldn't be slowdowns.Lexicologist
I added type hints to a program that processes 100,000 records of a CSV file and the program then took 10 or 20 times as long to load and run.Meliorate

© 2022 - 2024 — McMap. All rights reserved.