TypedDicts are better than you think

82 pointsposted 10 hours ago
by rbanffy

36 Comments

binary132

an hour ago

I 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

35 minutes ago

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

miohtama

6 hours ago

Reasons 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

6 hours ago

It’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

6 hours ago

Nope, __slots__ exist explicitly as an alternative to __dict__:

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

Anecdotally, I have used Slotted Objects to buy performance headroom before to delay/postpone a component rewrite.

zemo

5 hours ago

> It’s Python. Does L1 matter at all?

depends on the type in question. If you are fetching and operating on a large number of records then it can matter. But otherwise the answer is more often that it does not really matter.

Spivak

5 hours ago

Better than both use Pydantic. You'll never want to use anything else ever again. It's truly transformative in how you write code. Full type hinting support as well as strong verification that your data actually conforms to the types you set. Full recursive parsing of types arbitrarily nested and can parse tagged and untagged unions.

serjester

3 hours ago

TypedDicts are enormously helpful in defining args a function takes. You can’t do that with either dataclasses / pydantic without passing instantiated objects as args - which is really cumbersome.

Spivak

an hour ago

I actually have a function for this! I use it all the time and it's super helpful.

    T = TypeVar("T")
    U = TypeVar("U")
    V = TypeVar("V")
    P = ParamSpec("P")

    def modelargs(model: Callable[P, U]):
        def _modelargs(func: Callable[[T], V]) -> Callable[P, V]:
            def __modelargs(*args: P.args, **kwargs: P.kwargs) -> V:
                return func(model(*args, **kwargs))  # type: ignore
            return __modelargs  # type: ignore
        return _modelargs

    class MyModel(BaseModel):
        foo: str
        bar: int = 4

    @modelargs(MyModel)
    def test_func(model: MyModel):
        print(model.foo, model.bar)
        return 4

    test_func(foo="Hello", bar=20)  # -> prints Hello 20
If you look in your editor you'll see that the type signature for test_func is `(*, foo: str, bar: int = 4) -> int`. It's unfortunate that you have to write the model type twice but in exchange you don't have to write the args twice.

rattray

3 minutes ago

Interesting! How does that work for nested properties?

neeleshs

an hour ago

The author touches upon why typeddicts with total=False are more ergonomic than Pydantic for the examples they have.

darkteflon

3 hours ago

Really like Pydantic - assuming you’re happy to step outside the standard library. It does have a few sharp edges, but out of the box it includes all the stuff you eventually end up needing anyway. attrs is the other big Python parsing / validation library. I know nothing about it other than it’s also popular and that it distinguishes itself philosophically from Pydantic in a number of ways.

Don’t forget to use ‘ConfigDict(frozen=True)’ absolutely everywhere!

ddejohn

30 minutes ago

Huge proponent of Pydantic.

That said, there's also msgspec [1], which I've not used yet but plan to for my next project. It is supposedly quite a bit faster than Pydantic in de/serialization.

[1] https://jcristharif.com/msgspec/

richrichie

3 hours ago

I looked up the docs as to what frozen does:

> whether or not models are faux-immutable, i.e. whether __setattr__ is allowed (default: True)

Seems everything is “faux” in Python world when it comes to typing ;)

Spivak

2 hours ago

Python very much lives up to its goal of being a language for consenting adults. It's part of the culture that you don't write code that tries to forbid users of your code from doing "wrong" things and such efforts are mostly futile anyway. You can reach right into a Python object's internal data and modify it directly, taking away __setattr__ is just a suggestion. It's the same as in C where you can modify const variables if you're determined enough.

stavros

2 hours ago

Unfortunately I can't downvote, but this comment is just flamebait.

calibas

3 hours ago

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

flysand7

5 hours 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?

dan-robertson

4 hours ago

I’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.

roywiggins

5 hours ago

for "subscription", does `None` mean "don't change", or "remove subscription"?

Ericson2314

4 hours ago

Quoting, important when (meta-)programming and in natural language!

syockit

2 hours ago

Better punctuated:

> Here we have a problem: for subscription, does None mean don't change or remove subscription?

jjtheblunt

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

tptacek

6 hours ago

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

AmericanChopper

3 hours ago

I’m not a big fan of the “my opinion is fact” or “your opinion is wrong” headlines. They can be mildly funny in the right context, but it’s been done so much that they’re just a bit boring now. I’m especially bored of seeing this convention in conference presentation titles.

CyberDildonics

5 hours ago

Or people could post what they think, since that's the point of an internet forum.

binary132

an hour ago

And here I thought the point of Internet forums was to prove who has the bigger uppey votey number!

tptacek

5 hours ago

Please 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

selcuka

4 hours ago

Ironically, you seem to have found the most provocative comment to respond to.

the_gorilla

16 minutes ago

Are you a hall monitor or do you just play one on TV? It's not worth your time to come up and tell us what you think is worth our time to discuss. You see how that works?

CyberDildonics

2 hours ago

Seems a little ironic and self righteous since stories are upvoted based on the most provocative titles.

crazygringo

7 hours ago

Yup, 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

5 hours ago

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

sqeaky

5 hours ago

100%! If people are dumb enough to think I am dumb, then they generally aren't saying anything worth listening to.

lopatin

7 hours ago

It'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.

benmmurphy

7 hours ago

  <Option<Option<subscription>>