Typeguard
typeguard.readthedocs.io
typeguard.readthedocs.io
import Data.Foldable (for_)
main = do
let words = ["Hello", "World"]
for_ words (\word -> print word)
vs from typing import Iterable
def main() -> None:
words: Iterable[str] = ["Hello", "World"]
for word in words:
print(word)
if __name__ == '__main__':
main() def main() -> None:
words = ["Hello", "World"]
for word in words:
print(word)
if __name__ == '__main__':
main() def main() -> None:
words = []
for word in (words + ["Hello"]):
print(word)
if __name__ == '__main__':
main()
MyPy rejects to infer the type of the iterable. Here's an equivalent Haskell version that does infer the type without manual annotations: import Data.Foldable (for_)
main = do
let words = []
for_ (words ++ ["Hello"]) (\word -> print word) function main() {
let words = [];
for (const word of [...words, 'Hello']) {
console.log(word);
}
}I think comparing hello world is a bit unfair. A script may have something like file manipulation, regex, http requests.
Using standard type annotations in python and adding some @typechecked annotations can be much easier than adopting mypy in an organisation. It helps adopting typechecking incrementally for people who would never decide to adopt it otherwise, and it brings benefits immediately.
One caveat is I've stopped adding type annotations for anything but the most latest Python versions. Mainly because I got tired of having to deal with importing modules for things like unions, or whatever container types... all of that should be in the core language, and in more recent python that is happening.
Why not trying to catch type issues earlier than in Prod?
On the other side, this runtime error can be triggered by the tests in a ci/cd pipeline. While not ideal, in practice this can end up being much earlier than in production.
https://typeguard.readthedocs.io/en/latest/userguide.html#su...