|
|
Subscribe / Log in / New account

Dynamic typing

Dynamic typing

Posted Aug 9, 2019 14:22 UTC (Fri) by rahulsundaram (subscriber, #21946)
In reply to: Dynamic typing by jem
Parent article: Knoll: Technical vision for Qt 6

"Dynamic typing in (for example) Python, means some rarely called code can blow up at run time years after being put in production because a function is called with an object the function didn't expect"

Use Type Hints (c.f. https://docs.python.org/3/library/typing.html) and enforce it at commit time.


to post comments

Dynamic typing

Posted Aug 10, 2019 9:48 UTC (Sat) by swilmet (subscriber, #98424) [Link] (4 responses)

Python type hints is only for function parameters and return values, not for other variables. For other variables, you need to look elsewhere in the code to know the type, by examining the right-hand side of the assignments (if it's a function call, look at that function definition). It's more work for the developer when trying to understand the code.

Also, when there is a typo in a variable or function name, the error can be discovered only at runtime AFAIK. When doing big refactorings in a sizeable codebase, having a good compiler is of great help.

Dynamic typing

Posted Aug 10, 2019 12:11 UTC (Sat) by rahulsundaram (subscriber, #21946) [Link] (3 responses)

$vi quicktest.py

#!/usr/bin/python3

is_this_true: bool = True

is_this_true: bool = 'explain_this'

$mypy quicktest.py
quicktest.py:5: error: Incompatible types in assignment (expression has type "str", variable has type "bool")

Dynamic typing

Posted Aug 10, 2019 12:23 UTC (Sat) by rahulsundaram (subscriber, #21946) [Link]

> Also, when there is a typo in a variable or function name, the error can be discovered only at runtime AFAIK

Use something like https://github.com/nvie/vim-flake8 and have some tests built-in. Not perfect but certainly not too hard to stop the easy mistakes

Dynamic typing

Posted Aug 14, 2019 13:47 UTC (Wed) by swilmet (subscriber, #98424) [Link] (1 responses)

Oh, so type hints can be applied to local variables too, good to know!

Unfortunately there are lots of Python codebases out there without type hints.

Dynamic typing

Posted Aug 14, 2019 14:02 UTC (Wed) by rahulsundaram (subscriber, #21946) [Link]

> Oh, so type hints can be applied to local variables too, good to know!

Indeed, keep in mind this is fairly recent: Python 3.6

> Unfortunately there are lots of Python codebases out there without type hints.

This is slowly changing, if you care about a codebase either submit a PR or have stub files in parallel that adds such support if upstream is not convinced of them yet along the lines of https://github.com/python/typeshed


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds