Was battling it today. Latest stable release of python mind you 3.7.3. We have to move structured data around from a legacy system and integrate it with our new website. Perfect opertunity for types. This also includes trying to use mypy.
* it doesn't track the state of the variable. If I get a value thats Optional[int] there is no way to say 'if a is not None:' it will still complain that the variable could be none
* even with the mypy experimental TypedDict they're basically unusable IMO
* there is no 'keyof'
* there is no way to constrain to specific strings (or other primitives). I don't understand what literals are supposed to be but they aren't useful for us
The problem I had with Literal["foo"] is that at least according to mypy "foo" is a string and it was expecting Literal["foo"].
I think you're right, something must be mixed up. But then again this is the latest version of python and mypy, on intellij, so I'm not sure what I did wrong, it's using otherwise default options.
def foo(n: Optional[int]):
use_int(n) # MYPY complaints here because n could be None
if n is not None:
use_int(n) #MYPY allows this because it understood it cannot be None
2) TypeDict work fine, do you have an example of waht does not work
3) Of course, JS is different from Python, "keyof" doesn't make any sense in Python because Python uses class where JS uses dicts
4) Of course there is:
> "I'm familiar with tool A. Today I tried tool B and I couldn't figure out how to use it correctly, so it must be lacking. Everyone should know!"
From the points you make it seem like you haven't really figured out how to use mypy...
TS and Python/Mypy are my daily drivers. I don't find the experience working with mypy any worse than working with TS. In fact I prefer composing and consuming types via dataclasses and NamedTuples over interfaces and TS classes.
One of the very useful things Typescript has for a dynamic-language-turned-static is smart casts, union types and dependent types union types.
Things like these are possible:
And you can even do that with dict in the most recent version of mypy.
But the fact you have to import Union instead of using a pipe bothers me. It's useless boilerplate and verbose. So not o
Python. I reported it, but Guido told me it will not happen.
You are right. Let's remove the "+" operator in favor of operator.add. It's totally worth the readability.
I'm sorry but "|" is not only a very commonly known operator in the entire programming world, it is already an officially supported operator by python, vastly used by the community for doing unions: sets use it, sqlalchemy use it, pandas use it.
The absence of two PEPs that haven't shipped yet cause me great annoyance on a daily basis: 0544 [1] and 0589 [2].
On the implementation side, I've found TypeScript's type inference to be far more capable. I've encountered several mypy bugs around type narrowing. The issues I've filed have greeted by a "yeah, this is a problem, but low priority since you can restructure your code to make it more explicit." I understand staffing is an issue for community projects, and the PSF is trying to do a lot with very little, but that doesn't change the fact that it's a deficiency when compared to other systems.
Typescript feels like it's own language. Whereas python types are just optional extra information you can add (more like Flow does), and they are rather verbose and clunky compared to Typescript
You can try and take some old JS lib and annotate it in TS. It'll work. It'll be very clunky, and full of any/unknown.
There's a reason why strong static typeable code looks the way it looks (basically functional programming). You can type a monad chain, because it's a container type for arbitrary types interacting, but you can't easily type a switch nested in a for loop that uses a queue to walk a tree.
Of course the flip side is that to walk a tree functionally you need recursion schemas, and they are ugly/complex.
Both TS and mypy has strict mode and "implicit Any" mode.
In the latter they don't enforce much, you just get a JS or regular Python file. (mypy is not a transpiler like TS, mostly because it explicitly wants to be able to target legacy JS runtimes - eg IE8 ES5 - whereas mypy needs a recent Python that supports type annotations)
Python doesn't properly do static typing. Strong typing alone gives you none of the benefits of dynamic typing and none of the benefits of static typing.