9 ms·
TypedDicts are better than you think
- benmmurphy 2y ago<Option<Option<subscription>>
- jjtheblunt 2y ago"thank you think" in a title is overtly condescending...you know your audience and their educational deficiencies? "than i thought" would be compelling to read.
- crazygringo 2y agoYup, using "you" in headlines is a pattern that needs to die. I get that it's attention-grabbing, but it's because it's rude. You don't know anything about me. You don't know what I think or what I already know or what I won't believe. I know it's not a big deal in the grand scheme of things, but it's just one of those little aggravating things that makes life just a little bit worse each time you come across them.
- robswc 2y agoThe one I hate the most is: "X thing did Y, here's why Z" Where X and Y can range from meaningless to national security but Z always remains an opinion (usually uninformed too). Just search "here's why that's bad/good" on google news and there's so much "slop."
- jjtheblunt 2y ago"you" in headlines is better said, a better criterion for skippable articles, agreed! good point
- lopatin 2y agoIt's a qualifier. In a sense, they do know their audience fairly well because someone who clicks on that link is intrigued and feel they have more to learn about it. Anyone who is pretty knowledgeable about the subject will go "pff .. yeah right" and not even click on it. That said, it does annoy me too. Also what annoys me is that I constantly try to play devils advocate for things like this even if I don't always agree with the conclusion of the advocate.
- tptacek 2y agoThis is the kind of copyediting advice ChatGPT gives. I think everyone gets that the author doesn't know what you, in particular, think about TypedDicts. Read things more charitably; this is not a good use of time to discuss.
- CyberDildonics 2y agoOr people could post what they think, since that's the point of an internet forum.
- tptacek 2y agoPlease don't pick the most provocative thing in an article or post to complain about in the thread. Find something interesting to respond to instead. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- selcuka 2y agoIronically, you seem to have found the most provocative comment to respond to.
- CyberDildonics 2y agoSeems a little ironic and self righteous since stories are upvoted based on the most provocative titles.
- the_gorilla 2y ago[flagged]
- zo1 2y agoHonestly, right now it looks like you are the one derailing the discussion. And when it didn't go your way, you started quoting the "guidelines" as if to smack us over our fingers for misbehaving. This entire sub-thread at time of writing this is like 20% of the entire discussion; and that's sad.
- 2y ago
- sqeaky 2y ago100%! If people are dumb enough to think I am dumb, then they generally aren't saying anything worth listening to.
- code_runner 2y agoThe irony of a typo in this meaningless and overly sensitive reply is hilarious
- jjtheblunt 2y agoi agree (!), and it was my typo (amid Figaro the rescue kitten helping with the keyboard) and attempt to remark that such titles probably don't even get clicked.
- miohtama 2y agoReasons to use dataclass(slots=True) instead of TypedDict - Faster attribute access: your code is faster - Slotted classes take less RAM, less L1 cache pressure, your code is faster - Wrist friendly to with .foobar instead of ["foobar"] - Runtime error if you misspell an attribute name
- fovc 2y agoIt’s Python. Does L1 matter at all? I assume anything you’re accessing is behind a few pointers and __dict__ accesses anyway. For me it’s mostly about .attribute being more in line with the rest of the language. Kwargs aside, I find overuse of dicts to clunky in Python
- gshulegaard 2y agoNope, __slots__ exist explicitly as an alternative to __dict__: https://wiki.python.org/moin/UsingSlots https://wiki.python.org/moin/UsingSlots Whether or not the performance matters...well that's somewhat subjective since Python has a fairly high performance floor which makes performance concerns a bit of a, "Why are you doing it in Python?" question rather than a, "How do I do this faster in Python?" most of the time. That said it _is_ more memory efficient and faster on attribute lookup. https://medium.com/@stephenjayakar/a-quick-dive-into-pythons-slots-72cdc2d334e https://medium.com/@stephenjayakar/a-quick-dive-into-pythons... Anecdotally, I have used Slotted Objects to buy performance headroom before to delay/postpone a component rewrite.
- fovc 2y agoYes I know the slotted attribute is not in a __dict__, which definitely helps memory usage. But my point is that if the parent structure is itself in a dict, that access will swamp the L1 cache miss in terms of latency. Even the interpretation overhead (and likely cache thrashing) will eliminate L1 cache speedups. And yes __slots__ improve perf, but it’s about avoiding the __dict__ access, which hits really generic hashing code and then memory probing more than it is about L1 cache Where __slots__ are most useful (and IIRC what they were designed for) is when you have a lot of tiny objects and memory usage can shrink significantly as a result. That could be the difference between having to spill to disk or keeping the workload in memory. E.g., Openpyxl does with a spreadsheet model, where there could be tons of cell references floating around
- fat_cantor 2y ago[flagged]
- flysand7 2y ago> Here we have a problem, for subscription does None mean don't change or remove subscription I read this like 10 times and I still dont understand this mess of grammar. Am I having a fucking stroke rn?
- roywiggins 2y agofor "subscription", does `None` mean "don't change", or "remove subscription"?
- Ericson2314 2y agoQuoting, important when (meta-)programming and in natural language!
- dan-robertson 2y agoI’m going to send you some json to update your object – an HTTP PATCH request. There are three possible updates I might want to make to the subscription field: - change it to a new value - set it to None - leave it as-is In the json the first two options are specified by sending an object with a "subscription" field, either set to a string or to null. The third case is expressed by omitting the field. The OP asks how all three cases could be represented in python, and points out that one could not use subscription=None to represent both case 2 and 3 above.
- RHSeeger 2y agoIf you need to be able to represent what looks like a tree of data set - no value - new value do_no_set (sorry about the horrible tree) then it seems like you should represent the data like that. Pass in action (set/ignore) and value (value for set, no value for ignore). Or even (set/unset/ignore), which allows you to have some sanity checking that, if the action is set, an actual value is provided (and vice versa for unset).
- syockit 2y agoBetter punctuated: > Here we have a problem: for subscription, does None mean don't change or remove subscription?
- calibas 2y agoI feel like this needs to be mentioned more: If you don't have some kind of system to enforce types, then TypedDict does nothing at all. You can store a float in an attribute annotated as a string, and default Python will not stop you, or display any kind of warning. The typing is purely for development, and does nothing once compiled. If you want typing to actually be enforced, you need to use something like Pydantic.
- MrJohz 2y agoCounter argument: this is actually a myth that gets repeated far too often, and misses the point of what static types do. If your input data is the correct type (big if #1), and if your types are sounds and consistent with each other (big if #2), then you don't need something like Pydantic, because the types guarantee that there will never be a float stored somewhere that's annotated as a string. It cannot happen, because as soon as you try to store a float in a string attribute, your type checker (Pyright, Mypy, etc) will complain. And if you're consistently running that type checker over your code, e.g. in a CI job that runs over your codebase for every push, then you can never have checked-in code where the types are incorrect. There are the two big caveats above, but these turn out not to be such big deals. Caveat #1 is that you can't rely on external data to match the types you've described internally. This is an ideal use case for Pydantic, like you say: you check external types once, at the boundaries of your program, and then internally you can be confident that the types will always be correct. Caveat #2 is that you don't break the type checker's limits. This generally means avoiding things like `Any`, and ensuring that your typed code always calls other typed code, and never untyped, un-annotated code. The easiest way to do this is to start by typing the leaf files in your program (the ones that only get imported, and never import anything in turn). These can't import untyped code, so if they're fully typed and the type checker passes, then you can be confident that their types are correct. Now sure, a bad caller could try and call one of these functions with invalid input, but as they say, Python is for adults, and you're free to call a function with the wrong arguments, and you're free to deal with the results. Now, you can add types to code that only imports typed code, and because you've checked the imported code you know it's correct, so if you also check the importer and it's also correct, then you can be confident at that level too. You can keep going until you've added types to the entire codebase, and now you can be confident that the entire codebase always passes the correct types, and therefore that no runtime enforcement is necessary. There is a third caveat, which is that this relies on having a powerful enough type system to model all the things that you were originally doing in Python's dynamic type system. This is hard, because you can do some pretty wild things in Python, and my own experience with Python type checkers has been a bit disappointing - they handle more boilerplate-heavy code fairly well, but seem to struggle on more idiomatic code that uses Python's dynamic features. However, that seems to be improving all the time, and Typescript demonstrates that it's certainly possible to model a dynamic language with static types to a very reasonable accuracy.
- binary132 2y agoI feel like the dynamic typing crowd are slowly coming around to the fact that having a typesystem is just better. While we’re on the topic, a better typesystem is better than a worse typesystem. Thanks for coming to my TED talk.
- btilly 2y agoNo. It's that when people who prefer type systems go to use a popular dynamic language, they try to drag in features that they like from other languages. Very honestly, optional type systems tend to be the worst of all worlds. Because people who don't care, don't need to use it, you don't get safety. But people who enjoy ceremony can inflict verbosity on others. While missing the most important reason to do it.
- paulddraper 2y agoOptional types are the best.* I can prototype, script, move fast. Then I can solidify, stabilize, collaborate. * To get top performance, of course you'll need compiler, types, static dispatch.
- binary132 2y agoI find that good types help me model the problem and solution with clarity, and the procedures and functions take a back seat. After all, most procedures are just transformations of structured data, so putting the datastructure first makes good sense and keeps the code clean and lean. I used to believe in the “moving fast with no types” thing but in practice I find myself modeling the problem either way, it’s just clumsier without strong types.
- paulddraper 2y agoIt depends how large the problem is.
- josephg 2y agoYep. And even when prototyping, I make a lot more dumb mistakes without a type checker watching my back. It’s amazing how quickly a few interfaces / typedefs repay the time you spent typing them in.
- BiteCode_dev 2y agoTypedDict features are nice, but the syntax to declare it is aweful
- psd1 2y agoThe article doesn't mention my train for loving TypedDict. I worked on a python app built by a man with a stronger will to succeed than ability to code. Data was passed as dicts. Many methods (in the god object, natch) destructured, unwrapped or mutated before passing the new dict to some other method. TypedDict allowed me to annotate these shitty dicts. It became possible to reason about the code without spending two hours tracing code paths. The real solution is to be better at code. But, given an app written by a dict fetishist, it's a pretty good solution.
- sevensor 2y agoI’ve been in the same boat. The amount of pain you can reduce by pushing type annotations through a codebase like that is spectacular. And although I love dataclasses, the non-totality that you get from typeddicts is key for modeling the kind of mess you describe.
- psd1 2y agoTypedDict is a drop-in solution. Dataclasses is a big scary refactor. I'd use dataclasses in a new app, but it was not really an option in that app.
- mistahchris 2y agothe link is 404ing, but i found the post in the author's github repo: https://github.com/Jamie-Chang/Jamie-Blog/blob/18c0aaf2266de9887ca08d3954839ea08389ce80/content/typed-dicts-are-better-than-you-think.md https://github.com/Jamie-Chang/Jamie-Blog/blob/18c0aaf2266de...