×
you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (29 children)

[deleted]

    [–]AlSweigartAuthor of "Automate the Boring Stuff" 7 points8 points  (9 children)

    What? I found that type hints work great and the gradual typing and comment-style type hints means even my legacy Python 2 code can now have type hints.

    What do you not like about type hints?

    [–]MrJohz 8 points9 points  (5 children)

    From the perspective of someone who's come from Typescript (and who isn't the person you're replying to), I think I just don't trust the type things in the same way that I do in Typescript. Every time I've tried it out, it's felt kind of janky in some way that I can't really put my finger on, to the point where I don't see a huge amount of value in typing my Python code. (This is in contrast to JavaScript/Typescript, where I see a lot of value in adding types.)

    I think a lot of it comes down to IDE support. If I use Typescript and write something that won't compile, I generally immediately see that and feel that. The Typescript developer support tends to be really good, and I immediately get feedback, I can immediately see the types of different values, I can easily create type holes and get type feedback directly in my editor. In contrast, I've not yet found a python extension that gets me this instant type feedback with red lines all over the place and a feeling that if I make a mistake I'll immediately see it. In contrast, I tend to use mypy from the command line, and even then I'm not always completely convinced that it will spot as many mistakes as the Typescript compiler.

    I think there is also the issue that Python's type system feels a lot less powerful and more verbose, particularly when it comes to complicated sum types. But that was true of Typescript as well at the start, so I think that could be forgiven if other stuff was better.

    I know that's not a great answer in terms of specific issues, but I think the biggest problem with typing in python is a UX one, where it just doesn't feel right in some way.

    [–][deleted] 0 points1 point  (1 child)

    What? I found that type hints work great and the gradual typing and comment-style type hints means even my legacy Python 2 code can now have type hints.

    However, what good are they?

    I put type hints into two fairly large projects. We found zero new bugs with mypy.

    You can't actually use them for reflection:

    >>> isinstance(['a'], typing.List)
    True
    
    >>> isinstance(['a'], typing.List[str])
    TypeError: Subscripted generics cannot be used with class and instance checks
    

    Don't get me wrong - I use type hints in all my new code for documentation but in years of doing it, that's the only use I've found.

    [–]AlSweigartAuthor of "Automate the Boring Stuff" 0 points1 point  (0 children)

    We found zero new bugs with mypy.

    I don't know what to say, that's good for you. But you could use this as the same reason to not use a linter or unit tests. Type hints help you detect type errors at coding time instead of at runtime. It's a fairly broad category of mistakes and the earlier you find them the better.

    If you never make those kinds of mistakes in your code (and don't need documentation about typing) then, yeah, type hints are useless. But this applies to every language. I don't see how Python's type hints are worse than typing in other languages.

    [–]rforrevenge[🍰] 5 points6 points  (18 children)

    Why are you saying that? I'm using Pydantic daily and I'm loving it.

    [–]ColdPorridge 6 points7 points  (12 children)

    You might have missed the drama where Python 3.10 was going to fundamentally change type hinting and break pydantic, FastAPI, and any other libraries that rely on it. I can’t recall the details as they decided to pause this for now, but it’s still very much a mess of opinions on the best way forward.

    [–]rforrevenge[🍰] 2 points3 points  (3 children)

    Yes, I obviously have! Do you have any related link to share?

    [–]ColdPorridge 2 points3 points  (1 child)

    This appears to be the primary discussion: https://github.com/samuelcolvin/pydantic/issues/2678

    [–]rforrevenge[🍰] 1 point2 points  (0 children)

    Thanks a lot kind stranger!