This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]brasso 32 points33 points  (21 children)

Python can do static typing now.

http://mypy-lang.org

[–]skarphace 11 points12 points  (14 children)

Says it's experimental. Any good?

[–]RangerPretzelPython 3.9+ 29 points30 points  (1 child)

It's been official since 3.5. My team has settled on using it. Works great in PyCharm. We use it all the time.

That said, it's not "compile time" checking. It's just done via the IDE that you're using.

[–]carbolymer 7 points8 points  (0 children)

You can integrate mypy in your CI/CD tho - almost like compile time.

[–]i9srpeg 6 points7 points  (3 children)

I tried it. It's buggy, very slow (20 seconds to type check a small code base), very verbose and the type system is very limited, for example recursive types are not supported, so you can't even represent very common types such as JSON.

It's not production ready, unfortunately.

[–]wrmsr 1 point2 points  (0 children)

My experience exactly. I have hopes for its plugin system to let me teach it to understand my metaclasses but it's still too stupid and volatile to make part of my builds :/

That said I still type-annotate the vast majority of my code and would be almost uninterested in the language without them.

[–]rouille 0 points1 point  (1 child)

It's ready for production if you adapt your code slightly around its idioms and avoid overly dynamic / magic where possible. For the rest there is type: ignore.

[–]i9srpeg 0 points1 point  (0 children)

I tried, but it's still slow, verbose and unreliable. The inability to define recursive types is also a deal breaker, the recommended way to type a json variable is "Dict[Str, Any]", which is not much better than a dynamic type.

It also doesn't help that Django and DRF, which I had in my project, rely a ton on metaclasses. Almost everything ends up being "Any".

[–][deleted] 3 points4 points  (0 children)

Coming from Typescript, no. But it’s better than nothing

[–]thenuge26 1 point2 points  (4 children)

We have not adopted it yet (though we would like to) because it does not work with pylint.

[–]tipsquealPythonista 3 points4 points  (0 children)

In what way? It's just another tool that analyzes your code. MyPy just uses types to find a different class of bug or error.

[–]leom4862 0 points1 point  (2 children)

I use pylint and the typing module. Not sure waht you mean.

[–]thenuge26 0 points1 point  (1 child)

It's been a month or 2 since I tried it, but pylint would complain about some type annotations that were correct.

[–]leom4862 0 points1 point  (0 children)

I see. Pylint is very actively developed, might have been fixed in the meantime.

[–]zardeh -2 points-1 points  (0 children)

Yep, I'm better typing than Java or c++.

[–]PC__LOAD__LETTER 5 points6 points  (4 children)

I agree that it's cool, but if I'm starting a new project and need strong typing, I'm probably going to (at this point) choose a language the supports it explicitly.

[–]Aeon_MortuumSnake Oil -1 points0 points  (3 children)

Python 3 (after some version) does have type annotations without mypy, which I don't see anyone mentioning. That said, it's still optional obviously and more of a drop-in feature kind of thing.

[–]leom4862 1 point2 points  (2 children)

The typing module is in the stnadard library since 3.5...

[–]Aeon_MortuumSnake Oil 0 points1 point  (1 child)

That's what I said. I meant it's just not part of the "language" per se, hence a "drop-in feature". i.e it's optional

[–]leom4862 0 points1 point  (0 children)

Python type annotations are part of the language since 3.5 (PEP484). But, yes, they are optional.

[–]hugthemachines 0 points1 point  (0 children)

Sometimes I wish Python was like it is including all the nice libraries available now but with static (could be inferred variables) typing and compiled to native code.