Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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

Coming from typescript it's been very lackluster.



I agree that mypy can be a bit lacking, but to address a couple of your mypy woes:

- TypedDict is an option but if you can, dataclasses work much better

- mypy does refine Optional[T] to T with an if check. Maybe you need to futz with the compiler options?

- Does Literal["foo"] not work? I've been using it with pydantic for de-serializing a tagged union of dicts and also for function args.

e.g.,

    def call_to_service(api_type: Literal["users", "teams", "orgs") -> None: ...


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.


That is false.

On point 1: def use_int(n: int): return n * 2

  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:

  Accepted = Literal["foo", "bar", 3]
  
  def accept_values(v: Accepted):
      print(v)
  
  accept_values("foo")
  accept_values("bar")
  accept_values(3)
  accept_values("wrong")  # MYPY complains
  accept_values(5)  # MYPY complains


> "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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: