TypedDict is a LIFESAVER

Sdílet
Vložit
  • čas přidán 22. 08. 2024

Komentáře • 59

  • @dafoster
    @dafoster Před rokem +6

    The alternative syntax for TypedDicts is mainly useful if you have a TypedDict with a key containing special characters like period (ex: "http.method"). The alternative syntax can also be useful on older versions of Python 3.x (

    • @Carberra
      @Carberra  Před rokem +1

      Ah okay, that makes sense. Thanks for mentioning!

  • @jumper0122
    @jumper0122 Před rokem +9

    This is wonderful, I have *so* many applications that would benefit from this

    • @Carberra
      @Carberra  Před rokem

      Glad I could be of help! It's so damn useful honestly -- I need to get round to creating loads of these as Python 3.7 compatibility becomes less of an issue

  • @quackat1110
    @quackat1110 Před rokem +1

    Just found you out yesterday and you already helped me improve my codes so much. Much appreciated!

  • @parlortricks2992
    @parlortricks2992 Před rokem +6

    Great to know that python supports better typing. One question i have is, at what point do you need to swap out typing for pydantic?

  • @ominhquanho3860
    @ominhquanho3860 Před rokem +5

    please keep creating these amazing content:)))

  • @molomono9481
    @molomono9481 Před rokem +1

    This does seem genuinely useful. But as many others have mentioned i can't really see the benefit over Pydantic. It feels like you are making a class which serves as a factory for a dict, but have to couple this waaaay to much, even the keys have to match the class attributes. Why not have a "as/to_dict" method of the class which defines the typing, then you just construct a class and represent it as a dict which is supported with even dataclasses.
    The keys of the dict are attributes of the class, so you don't have to type as much and it has the same result + lower coupling. I'm not bashing this feature though, i would be joyed to have someone tell me when to use this over pydantic/dataclasses (basically structs lol). I'm guessing it has to do with mutability and/or resources but i'm not a computer scientist so i have no idea what is "really" going on under the hood in both cases.

  • @dafoster
    @dafoster Před rokem

    Glad to hear that you're finding TypedDict useful! (I helped bring it to life.)

    • @Carberra
      @Carberra  Před rokem

      Wow, never thought I'd get a comment from any of the authors of these features! Thanks for stopping by!

  • @topcivilian
    @topcivilian Před rokem +1

    Excellent content, my friend..!!
    The google algorithm brought me here...
    You have a new subscriber

  • @ilonachan
    @ilonachan Před rokem +3

    Is it possible that when using "total=false", the default assumtion of Required vs NotRequired is simply flipped? so usually all fields are Required and you mark the ones that aren't, but now every field is NotRequired and you mark the ones that are?
    Also, is there a way with this syntax to allow adding arbitrary keys additionally to the predefined and typed ones?

    • @Carberra
      @Carberra  Před rokem

      Ya know, that's probably it. The total kwarg reverses it normally, so that makes sense -- why didn't I think of that!
      As for arbitrary keys, I'm not sure. You'd have to look that one up.

  • @aishwaryapatel7835
    @aishwaryapatel7835 Před rokem +2

    What about using pydantic?

  • @davidmurphy563
    @davidmurphy563 Před rokem +1

    Could you maybe consider upping the font size please? My old eyes struggle on a mobile screen.

  • @flyingsl0ths961
    @flyingsl0ths961 Před rokem

    this was really helpful, thanks 😀

  • @fallingintime
    @fallingintime Před rokem

    Very useful for serverless apps and creating types for events (also it supports generic overloading)

  • @Tntpker
    @Tntpker Před rokem

    Very underrated channel

  • @iyadahmed3773
    @iyadahmed3773 Před rokem

    Thanks a ton 🙏, your videos are awesome!

  • @JohnMitchellCalif
    @JohnMitchellCalif Před rokem

    super clear and useful! Subscribed.

    • @Carberra
      @Carberra  Před rokem

      Glad you liked it, and thanks for the support! (:

  • @ufukbakan4741
    @ufukbakan4741 Před rokem +1

    nice to see python getting similar to Typescript xD

  • @GeorgeNoiseless
    @GeorgeNoiseless Před rokem +2

    _Ugh,_ why don't we just start a superset called TypePython (TyPython? Typton?!) and be done with it, _jeeeeeeeeeeeez_
    Not sure if I'm even joking there... Thank You for the video!

    • @quackat1110
      @quackat1110 Před rokem +1

      London Typton, sounds nice

    • @muharief3885
      @muharief3885 Před rokem

      You don't understand the point. Scripting is scripting, don't mess with strongly type. I didn't like the idea declare type everywhere, if that the case, why not jump into strongly type language like Java or c# that are more performant than scripting. This is just tool, you use it when it needed.
      Superset js with typescript just create new mess, divergent code and make type code more than it should be. Scripting should easy to write.

    • @DMSBrian24
      @DMSBrian24 Před rokem

      @@muharief3885 I didn't like it either when I started. Now I understand it's basically a necessity for large scale projects to remain reliable and maintainable. Typescript is great compared to JS, it's not perfect but it's far from being the mess that JS itself is. Now, Python is not nearly as bad as JS and doesn't really need a TS equivalent but having an *optional* feature for mandatory type declaration/expectation, even if it's non-strict and only throws warnings, would be absolutely amazing. Even for simple data analysis usecases this would save you so much headaches of having to constantly check the type of everything.

  • @andrewglick6279
    @andrewglick6279 Před rokem +2

    I think I've been using TypedDicts wrong this whole time. I've always thought that to use them, you had to use them as a constructor, e.g., ProfileT(name="John", age=35). At that point, you might as well just use a dataclass (which I still think I prefer over TypedDicts). This makes TypedDicts actually useful, though!

    • @dafoster
      @dafoster Před rokem

      You can create TypedDicts either way. The main advantage TypedDicts have over dataclasses is that they serialize to/from JSON trivially, since a TypedDict is just a dict.

  • @schedarr
    @schedarr Před rokem +3

    My senior says there's no advantage on using TypedDict and it's much better to use dataclasses, pydantic or attrs

    • @falklumo
      @falklumo Před rokem +1

      Except, you are required to pass a dict or kwargs …

    • @schedarr
      @schedarr Před rokem

      @@falklumo yes, the source will often untyped like eg. json, but you can put it directly into typed data structure

  • @m.m3633
    @m.m3633 Před rokem +2

    Nice

  • @Bobronium
    @Bobronium Před rokem +1

    I've seen T suffix mostly used for TypeVar, and never seen it used with regular types or even type aliases. TypedDict can be used as a regular dict, so it doesn't make much sense to use T in its name from my perspective.

    • @Carberra
      @Carberra  Před rokem

      I've never given it much thought tbh, but yeah, I think you're right.

  • @timelessjc
    @timelessjc Před měsícem

    Hi there, thanks for sharing! I don't quite understand it, I tried the following code and it works fine .... :
    -----------------------
    from typing import TypedDict
    class ProfileT(TypedDict):
    name: str
    age: int
    jobs: list[str]


    profile: ProfileT = {
    "name":"Ethan",
    "age":24,
    "jobs":["se"],
    "gender":"male",
    }
    profile["age"] = '20'
    print(profile)
    ---------------------------
    nothing stopped the code from running good. it printed out : {'name': 'Ethan', 'age': '20', 'jobs': ['se'], 'gender': 'male'}
    Am I missing something here or I just didn't get it ?
    Thanks!

    • @Carberra
      @Carberra  Před měsícem +1

      The only validation is at the type-checking level, so something like Mypy would pick up errors. At runtime, they're seen as normal dicts. If you want runtime validation, you need something like Pydantic (latest video) or Msgspec (next week's video).

  • @gilkogan479
    @gilkogan479 Před rokem

    You can also mix required and non required keys by creating two classes and inheriting one from the other. From your example it would look like this;
    from typing import TypedDict
    class ProfileReqFields(TypedDict):
    name: str
    age: int
    class ProfileT(ProfileReqFields, total=False):
    jobs: list[str]
    profile: ProfileT = {
    "name": "Ethan",
    "age": 24,
    "jobs": ["Software Engineer"],
    }

  • @willemvdk4886
    @willemvdk4886 Před rokem +1

    But what is the advantage of this as opposed to just using a regular class? You could also just use a dataclass for exactly this thing, right? Why a TypedDict?

    • @Carberra
      @Carberra  Před rokem

      I believe TypedDicts are lighter weight. And if you're only using it to type dictionaries, you wouldn't need to create a dataclass for it. That's how I understand it anyways.

    • @falklumo
      @falklumo Před rokem

      Lighter weight is ridiculous in Python which is slow as hell anyway. The real reason for TypedDict I think is that you can still use it where you need dicts, **kwargs etc.
      However, a typed dictionary where not all keys, and all values are same type may be considered bad practice better replaced by a data class. From an architecture point of view. I fear TypedDict does not handle the case where a key type is mapped to value type, or does it?

    • @monishbiswas1966
      @monishbiswas1966 Před 2 měsíci

      This mirrors functionality in typescript to type is dicts.
      I guess there are a few reasons
      1. You want them to be dicts as they are used in other classes
      2. You are retrofitting this to existing code and you don’t want to rewrite a lot of code

  • @axiinyaa
    @axiinyaa Před rokem

    YOOOOOO I DIDN'T KNWO ABOUT THIS

  • @bmoore813
    @bmoore813 Před rokem

    Nice but I would just use a data class or take it one step further with pydantic

  • @franky12
    @franky12 Před rokem

    Shouldn't this also work with dataclasses from the standard library?

    • @Carberra
      @Carberra  Před rokem

      You mean making the TypedDict a dataclass? I honestly don't know if that's a thing you can do.

  • @holthuizenoemoet591
    @holthuizenoemoet591 Před rokem

    gender = Mail, which kind tho?

  • @illker.
    @illker. Před rokem

    PLEASE ZOOM IN PLEASE

  • @lightless
    @lightless Před 5 měsíci +1

    Feel like this is just extra unnecessary code. Not saying its useless but i think theres better methods.

  • @nassehk
    @nassehk Před rokem

    What is that syntax in line 10?

    • @jphvnet
      @jphvnet Před rokem

      Timestamp?

    • @nassehk
      @nassehk Před rokem

      ​@@jphvnet1:36

    • @jphvnet
      @jphvnet Před rokem +1

      @@nassehk the : in the type definition introduced by Python 3.6. Like "count : int = 3", here the type is that class (it's compatible with dictionary definition)

    • @nassehk
      @nassehk Před rokem +1

      @@jphvnet Thanks yeah I see it now. What confused me is seeing the type definition out of the function definition. I had not seen type definition being used like that.

  • @labret4250
    @labret4250 Před rokem +2

    First hehe