5 Reasons Why You Should Use Type Hints In Python

Поделиться
HTML-код
  • Опубликовано: 2 июн 2024
  • Even though you don't see type hints all that often in Python code, and they'll probably never become obligatory, I still think you should use them. Here are 5 reasons why.
    💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
    💻 ArjanCodes Blog: www.arjancodes.com/blog
    🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes
    👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
    💬 Discord: discord.arjan.codes
    🐦Twitter: / arjancodes
    🌍LinkedIn: / arjancodes
    🕵Facebook: / arjancodes
    👀 Code reviewers:
    - Yoriz
    - Ryan Laursen
    - James Dooley
    - Dale Hagglund
    🎥 Video edited by Mark Bacskai: / bacskaimark
    🔖 Chapters:
    0:00 Intro
    1:27 What are type hints?
    3:35 Criticisms of type hints
    4:36 #1 Type hints help write shorter documentation
    5:50 #2 Type hints improve the editing experience
    8:25 #3 Type hints make coupling more explicit
    9:08 #4 Type hints force being explicit about data structures
    9:50 Sidestep: Type-driven development
    11:31 #5 Type hints simplify your code
    12:02 Type hints vs unit tests
    12:43 Final thoughts
    #arjancodes #softwaredesign #python
    DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Комментарии • 251

  • @ArjanCodes
    @ArjanCodes  Год назад +291

    LEGO people *were* hurt in this video.

    • @aflous
      @aflous Год назад +12

      Reported to RUclips 👮 🚨

    • @Simon-yf7fo
      @Simon-yf7fo Год назад +6

      If you don’t use type hints you fucking deserve it lmao

    • @AndreaDalseno
      @AndreaDalseno Год назад +2

      I liked the joke. Actually, RUclips subtitles translated your sentence as "be an Arian" and it sounds a little bit scary, doesn't it? 😵‍💫​

    • @cdhit
      @cdhit Год назад

      Hi Arian, love your videos definitely.
      Could you make an episode about how to use Python package and to structure Python code to make project nice and clear ? @Arian

    • @RBBlackstone
      @RBBlackstone Год назад +1

      My condolences for Bernard ( AKA Wilhelm).

  • @HexenzirkelZuluhed
    @HexenzirkelZuluhed Год назад +42

    "I don't want to push you in any specific direction" he said, while dismembering Person B.

  • @Aldraz
    @Aldraz Год назад +10

    My personal strongest arguments for using or not using type hints are these:
    Pros - Using type hints will actually make your code much faster one day, because libraries like mypy, etc will use it to compile to much faster code.
    Cons - It clutters the screen and the more type hints you have the less immidiate information you have in some sense, so maybe using it for every function isn't the best idea.

    • @Ludwighaffen1
      @Ludwighaffen1 6 месяцев назад

      Another "con" is that, at least in data science, many bugs come from the wrong dimensionality of a data structure, or the wrong formatting of a specific table column, or a column not existing. These bugs can only be properly handled by managing exceptions inside functions and type hints is not really helping.

  • @cemsity
    @cemsity Год назад +51

    My use case for type hints is mostly for the IDE Intellicode. If i have a larger application then using type hints with mypy keeps me sane and prevents a lot of dumb bugs. Also the fact that type hints aren't required enable the developer to omit type hints where they aren't need or obvious.

    • @simmonslucas
      @simmonslucas Год назад +1

      I guess that this benefit should have been apparent to me, but I didn't realize. A great time saver to find errors before you debug.

    • @rban123
      @rban123 Год назад +1

      yeah especially on a big project I'll typically have a CI job which runs mypy automatically on every PR and fails the PR if there are any type errors

  • @wesselbindt
    @wesselbindt Год назад +66

    My main gripe with type hints is that they have a tendency to give a false sense of security. I've lost count of the times where I and my colleagues thought some piece of code was type safe only to later find out that something was inferred to be of type Any because of a poorly typed 3rd party library or some forgotten subscripts on a generic (the idea usually being that surely mypy should be able to infer this). Sure, many of these instances could've been avoided by configuring mypy to be as strict as possible, but in projects which started out untyped and are in the process of migrating to using types, this is simply not realistic.
    Still, in spite of this, it is absolutely beyond me that someone would choose to write a large production system in completely untyped code. The horror

    • @andyblue953
      @andyblue953 Год назад +7

      I'm always wondering why developers would switch at all to Python for larger productions? It's not like there aren't good alternatives.

    • @tkerkvliet
      @tkerkvliet Год назад +9

      isn't that just the perfect description of development reality: fixing shit of people that made choices that are absolutely beyond us

    • @alex952
      @alex952 Год назад +7

      @@andyblue953 because of its extremely fast turn around for development and easy of use/learn. It’s also got many libraries that help you deliver code. As a company you can enforce documentation, testing, etc. which helps overcome the drawbacks of the language.

    • @newperspective5918
      @newperspective5918 Год назад

      ​@@alex952 Tbh I don't really see easy of use/learn to be a mayor point. For me it is all about libraries and the ability of the language to solve the problem. Python is a bad choice if type safety or high performance is key, so just pick something else, e.g. C++. I think Python, C++, HTML, CSS and JS is all the languages you need to know in most cases. Then add som SQL, bash and UNIX knowledge on that and you can basically solve any problem.

    • @stephens3909
      @stephens3909 Год назад +1

      Where third party libraries are involved, this is where type casting comes in, especially if you are taking advantage of abstract interfaces and some OO patterns when using some third party libraries.

  • @derGerlach
    @derGerlach Год назад +64

    When I started with Python, TypeHints were not available. Then I had to work with Springboot wich uses Java. I really loved the statical typed style. It just forced me to write better code. Nowadays I use type hinting in every piece of Python code and i love it. It helps so much, especially with bigger projects. Great video, keep it up 👍👍

    • @roelmathys
      @roelmathys Год назад +1

      But it's a game of hints in Python. No statistical analysis is done, in Java it would not compile and would not run. And it's getting better, but it's making code much more verbose.

    • @redcrafterlppa303
      @redcrafterlppa303 Год назад

      @@roelmathys a good rule of thumb is when your types get out of hand and look verbose and cluttered, rethink your design decisions.

    • @roelmathys
      @roelmathys Год назад +1

      @@redcrafterlppa303 Python existed for two decades without this. Python doesn't do anything to improve type safety based on type hints. You still can run the code if the type hints indicate it should not run (mypy).
      It's bolted on and feels more like documentation than something else.

    • @redcrafterlppa303
      @redcrafterlppa303 Год назад +1

      @@roelmathys only because it runs doesn't guarantee its good code. The level of control a strongly typed language provides python can never achieve. Overall in my opinion python is a really ugly language with to much dynamic and "it works somehow" in it. I don't get what to even use python for.

    • @roelmathys
      @roelmathys Год назад +3

      @@redcrafterlppa303 statically compiled languages give type guarantees but do not guarantee correctness or good code. But I do not understand what you are commenting about. If you don't want Python, what are your remarks adding?

  • @xor2003
    @xor2003 Год назад +2

    * Type hints in Python are optional
    * They can aid with debugging and avoiding outdated documentation
    * They also assist with autocomplete options in IDEs and make code easier to read and write
    * Python 3.10 allows for lowercase type hints for certain data types
    * Using type hints improves code quality and catches errors early on
    * It promotes explicit data structure design and simplifies code-writing
    * Type-driven development clarifies how data will interact and detects issues with complex functions
    * Pydantic and type hints improve data understanding, focus unit tests, and offer faster and accurate accuracy checking.

  • @TomTrval
    @TomTrval Год назад +19

    Agreed👍. Type hints are especially helpful for me in 2 cases>
    -when I return to my code after long time or study code from someone else🤯
    -early detect problems with code architecture. I learned when I am thinking to my self "Hmm typing this code is hard" it means there is something wrong with architecture (usually I missed abstraction to decouple modules)

    • @roelmathys
      @roelmathys Год назад

      Type hints are just hints, if somebody puts them in, and the next person does not respect the "contract", the application still runs.

  • @SophieJMore
    @SophieJMore Год назад +13

    3:06 minor correction, before 3.9
    I remember I had to change all my type hints in one of my projects to make it compatible with python 3.8 so that it could run on windows 7

    • @lunarmagpie4305
      @lunarmagpie4305 Год назад +16

      3.9+ style type hints work in 3.8 if you do `from __future__ import annotations` at the top of the file

    • @SophieJMore
      @SophieJMore Год назад

      @@lunarmagpie4305 Interesting. Thank you, I didn't know that

  • @CornishMiner
    @CornishMiner Год назад +3

    I've picked up the habit, and I like it. It helps me a lot with keeping track of what I'm doing, it reduces documentation and it helps make the most of the ide.

  • @wylde780
    @wylde780 Год назад

    This video is prefectly timed as i recently came across some type hints in some airflow code and didn't understand it. Thanks

  • @mamazu1995
    @mamazu1995 Год назад +5

    It's a great video and I completely agree that types are a great way to ensure correctness and proper scope of functions. One thing that I would also like to add to point 4 is that, if your program is sufficiently typed then you can do "type assisted refactoring". For example you want to replace a property of an object with a method call or something. Just remove the property from the object and then run a static analyzer like mypy and see where the code fails. Then you know which places to fix. This is much easier than trying out all possible paths that could crash the program.

  • @NStripleseven
    @NStripleseven Год назад +3

    Another nice ide-related thing that type hints improve is autocomplete. If your ide knows that a variable is supposed to be an int, it can give you suggestions that make sense for an int value.

    • @tronicit
      @tronicit 6 месяцев назад

      I think that's probably the best reason to use them. That and the methods which work as soon as you apply the hints. It's often easier to pick a method or related function from the list than trying to remember the exact syntax which doesn't work if you haven't got it correct.

  • @joningram
    @joningram Год назад +3

    You briefly mention 'duck typing' in the beginning, and I think this is the issue I have with the mass importation of type hints into Python - they miss that what I want to specify in a function header is not the type I require of an input variable, but instead what capabilities that variable should have.
    For example, if I am writing a 'max' function, then I don't want to restrict it to a list, or a tuple, or any inbuilt type - I want to accept anything which can be iterated through.
    There are various guises of this idea - interfaces, templates, contracts - in other languages and I am hoping that this is the direction that typing will go in Python.
    The other issue I have with typing is that it can make quite simple ideas (like passing a function into another function) complicated purely through awkward syntax. I have been able to explain and demonstrate ideas such as callback functions very easily in Python which I would have struggled to write in a language which required types.

    • @DajesOfficial
      @DajesOfficial Год назад +2

      You have types such as Iterable and Callable for that specific purposes

  • @robertbrummayer4908
    @robertbrummayer4908 Год назад +1

    Great video Arjan. I also use type hints a lot in my code. I recommend mypy as type checher. It can easily be integrated into VS code.

  • @ErnestGWilsonII
    @ErnestGWilsonII 4 месяца назад

    I just want to say thank you very much for making these videos and sharing them with all of us. I only recently found your channel and I am now subscribed with notifications enabled and thumbs up!
    Thank you,
    Ernie in Pennsylvania

    • @ArjanCodes
      @ArjanCodes  4 месяца назад

      Thank you for the kind comment, Ernie!

  • @Pirake123
    @Pirake123 Год назад +33

    Type hints actually make python more enjoyable. Putting them in the language means we don't get a Tython language down the track (like Typescript for JavaScript)

    • @roelmathys
      @roelmathys Год назад +2

      I don't know, it's a lot of extra typing, a lot of extra verbosity. And it's not that it's statically typed at that moment, it will just run fine, wel :)
      Statically typed languages result in not being able to execute the application. Type hinting is just that: hints. It - currently - does not make code magically safer, it depends on running mypy ... , or makes the generated application faster..

  • @arnold9103
    @arnold9103 Год назад +1

    We started using those at work!
    Improved readability and quality somehow !

  • @Shivnaren
    @Shivnaren Год назад +19

    Thanks a lot, Arjan-learned all about typing and types from you.
    One downside is I can't stand to read untyped python anymore...unfortunately there's a lot of it about :D

  • @anyyyel
    @anyyyel 11 месяцев назад +1

    Dude, your channel is really good. Congratulations!
    Simple, objective.

    • @ArjanCodes
      @ArjanCodes  11 месяцев назад +1

      Glad you like the videos!

  • @liquidpebbles
    @liquidpebbles Год назад +9

    I recently moved from a C# job to Python... Man, I can't stand dynamically typed languages. The development experience with a typed language is far superior in my opinion as it opens up so many possibilities for the IDE.

    • @Michal_Peterka
      @Michal_Peterka Год назад +3

      I prefer the way F# works. You don't have to specify the type at most of time but it is still strongly typed. And there is VS Code extension Ionide that displays the types annotation for you.

    •  Год назад +3

      @@Michal_Peterka type inference of ML-like languages are awesome, F# is underrated

  • @wyst69
    @wyst69 Год назад

    Your recorded the whole video in one single shot ! That's clean.

  • @user-hf5be1zu7s
    @user-hf5be1zu7s Год назад +1

    A team for the win!! Once again, great video. notes are taken!

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thank you, glad you liked it!

  • @dougalscott4896
    @dougalscott4896 Год назад +1

    I teach people coding (i.e. programming, not just python) and I find type hints very useful. It gets the student to think about the code that they are writing before they start bashing away at the keyboard, and if they need to change the types it probably means that they need to change the code.

  • @tugrulpinar16
    @tugrulpinar16 Год назад +1

    Thanks a lot for expanding on this

  • @andyblue953
    @andyblue953 Год назад +7

    I use Python for data science purposes and am perfectly happy with dynamic types for rapid prototyping. I use type hints only very rarely and prefer default values instead.
    But it seems that more and more serious programmers move to Python as well, to write larger projects with a stronger need for type control. Since there's plenty of excellent alternatives with static types, I always wonder about their motivation to switch... ?

    • @ArjanCodes
      @ArjanCodes  Год назад +10

      What I often see in the companies I’m working with is that they start with a simple data science project in Python, but then things become more complex.
      They want to keep using all the useful libraries in the Python ecosystem, but they want to add an API layer to the data processing system. Or what about doing complex computations in parallel? Or how about running all those things in the cloud? Or what if you want to make the code generic so you can use it for multiple clients? Or add authentication to control access to the data? Etc etc.
      It’s at this stage where you can really benefit from a more strict approach to writing Python code to keep things manageable.

    • @Witpaardt
      @Witpaardt Год назад +2

      ​@@ArjanCodes that is more a cultural problem and by definition not generalizable. I think you make good points in your video, but @Andreas Groth does as well. Also microservices. In many companies those data science scripts might turn into a small service where typing can be useful, but not necessary. There your final point about short scripts remains valid. Furthermore, naming things clearly can also help with documentation (as you can see in your video). The thing about Python is, the language is flexible and that is a good feature imho.

  • @aashayamballi
    @aashayamballi Год назад +1

    Just wanna say.. thank you, Arjan! Have learned a lot from you on Python 🙏

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      You’re welcome - glad to hear that the content is helpful to you!

  • @lowkeygaming4716
    @lowkeygaming4716 Год назад

    Nice vid. Very insightful.

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thank you, glad you liked the video!

  • @ea3gug
    @ea3gug Год назад +1

    Hey!, excellent video as always, very educational. Thanks!. I am oriented into app security, and I have found a paralelism about this technique and a tool called 42Crunch that does unit testing against an API definition (Swagger, OpenAPI), doing unit tests against the type definition to check the specification against the real input/outuput, messing with parameters, so it is the same concept, to check that development meets specification.

  • @bluesdog88
    @bluesdog88 Год назад

    Arjan you are a strange man, that's mostly why I subscribed....keep up the good work and code!

  • @victormungai
    @victormungai Год назад

    In one of the latest updates(at the time of writing this comment) to the python extension on vscode, one can instruct vscode to activate the type checker. I have mine set to strict and I'm glad I did.

  • @fexofenadinaGenerica
    @fexofenadinaGenerica Год назад

    I agree. Type hints help to better understand the code.

  • @colletantoine8885
    @colletantoine8885 Год назад

    Thanks for this great video ! How to you manage type hints with bumpy in Vscode ? I always end up with a lot of warnings although I use bumpy.typing ...NDArray etc.

  • @sillytechy
    @sillytechy Год назад +1

    Hi could you recommend a book on software design. Like how to design a large scale software with multiple elements like database, cloud elements(AWS/AZURE), break into smaller components, make Class relationship diagrams to use OOPs properly. Any book that talks on this.

  • @kayakMike1000
    @kayakMike1000 Год назад +19

    Type hints are sorta like stronger typing which is pretty good... Wasn't available when I started with python, but I can see great value.

    • @roelmathys
      @roelmathys Год назад

      Python was already strongly typed - has always been told. You do not get a statically type checked language with these type hints. It's more for documentation purposes.

  • @quaxBK
    @quaxBK Год назад +4

    The strongest argument for type hints are for the LSP imo, you get much better diagnostics and completions that way.

    • @nargileh1
      @nargileh1 Год назад

      yeah code completion is the best thing I get out of type hints; with CTRL-click to jump to definition code in second place

    • @sorvex9
      @sorvex9 6 месяцев назад

      Literally the primary reason I use them lol

  • @nisheetpatel2654
    @nisheetpatel2654 Год назад

    I often forget to hit the like button until the time you mention it, but this time, with the lego people in the first 20 seconds, I knew this video was legendary!

  • @rachinini
    @rachinini Год назад +1

    Python as a simple scripting language that is used for writing small to medium-sized programs is beautiful. Adding type hints, redundant features (how many string interpolation methods do you need?), and other excess features has turned Python into an ugly, unstable mess.

  • @kosmonautofficial296
    @kosmonautofficial296 Год назад

    Interesting video thanks

  • @willemvdk4886
    @willemvdk4886 Год назад +1

    The Wilhelmscream had me, haha. And the fact that you used Uncle Bob's book lol

  • @Ziggity
    @Ziggity Год назад +1

    I started a job at a young startup last year, backend was written in python and was completely untyped. I was losing my mind. These days after pushing hard in that direction everything is typed and I am a happy dev

  • @user-xc5cx7lh4l
    @user-xc5cx7lh4l 8 месяцев назад

    For complex tasks where we need types there are Java or C#... Python and JS does not need types, they are for rapid development. Type hints are ok until you need to use multiple generics with restrictions. And then you need more time to write type definitions than the actual code.

  • @riptorforever2
    @riptorforever2 Год назад

    Psico introduction with Dexter's serial killer flavor.. loved!

  • @distant6606
    @distant6606 Год назад

    I also recommend using mypy to check for these type hints.if u type hint a variable to str and then assign to it an integer mypy will throw you an error.

  • @bluzter
    @bluzter Год назад +1

    Type hints are kinda satisfying which is why I love them.

  • @thomassmail6896
    @thomassmail6896 Год назад

    This is really cool, I wasn’t even aware of them. I feel they are something I might add at the end of writing a function when I go back and comment my code.

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thanks so much Thomas, glad it was helpful!

    • @NicholasShanks
      @NicholasShanks Год назад +1

      Try adding them right at the start when you don't even know how the function is going to work yet.

  • @ivhea
    @ivhea Год назад +1

    Density of jokes per minute is augmenting with each new video. At some point this will become a comedy channel. Just a fact, not a judgement. :)

  • @JeffreyChadwell
    @JeffreyChadwell Год назад +1

    Nice use of the Wilhelm Scream!

  • @sync9827
    @sync9827 Год назад +1

    The PyCharm IDE checks if the types of the arguments and of the return value match with the types specified in the docsring. So while you're writing the code the IDE checks if all the types are correct even though you aren't using any type hints. So you can do type-driven development without type hints.
    But since type hints are pretty elegant and less work than writing docstrings, I might also start using type hints...

    • @hansdietrich1496
      @hansdietrich1496 7 месяцев назад

      Also, PyCharm hints become much better, when you're generous with adding type hints to your code.

  • @Ludwighaffen1
    @Ludwighaffen1 6 месяцев назад

    Thanks for this great video.
    In your neat example on the luhn_checksum function (I liked your lunatic joke btw 🙂), I'd argue that "number" is a pretty bad variable or argument name in the first place. But we all Bernards got your point. ;-)

  • @blutella4162
    @blutella4162 Год назад +3

    Great video.

  • @TheSzybas
    @TheSzybas Год назад

    That "luhnatic" joke was perfect!

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thank you Tomasz, glad you liked the video!

  • @cdhit
    @cdhit Год назад +3

    Hi Arian, love your videos definitely.
    Could you make an episode about how to use Python package and to structure Python code to make project nice and clear ? @Arian

  • @handle_and_gretel
    @handle_and_gretel Год назад

    use type hints and then vscode extension AutoDocString to create the small snippet for documentation

  • @zackadam2598
    @zackadam2598 Год назад

    I liked this video explicitly for the LUNA dig. Spicy! 🌶

  • @josewilhelm2717
    @josewilhelm2717 Год назад +2

    Great video! Can we have a discussion video about Object Oriented Programming and why it is being criticized so much lately?

    • @HelloWorlds__JTS
      @HelloWorlds__JTS Год назад +1

      @ArjanCodes already has a handful of videos discussing tradeoffs between functional and object oriented programming. Just search for object oriented in the search bar on his main page, and the first 5-6 videos are probably exactly what you're looking for.

    • @josewilhelm2717
      @josewilhelm2717 Год назад

      @@HelloWorlds__JTS oh thanks, I didn't know. I will search it out!

  • @cn-ml
    @cn-ml Год назад +2

    I just cannot grasp why people dislike using type annotations. The amount of coding errors type hints prevent is just insane. Even with statically typed languages people usually code bugs, but without type checking even it's almost impossible to write code that works the first try. I absoltely hate python without types.

  • @TechieGuy82
    @TechieGuy82 Год назад

    Love you videos.. Actually "your bad joke" made me laugh :D
    Thumbs up

  • @JuampyRabino
    @JuampyRabino Год назад

    Love that you used Wilhelm's scream at the beginning 🤣🤣🤣🤣

    • @ArjanCodes
      @ArjanCodes  Год назад

      It's my favorite sound effect :)

  • @FabioKasper
    @FabioKasper Год назад +1

    Ohh, "type hint" is how it is called then. All I know is that I started using them after seeing it in your videos. Thanks again. 😂

  • @postmodernist1848
    @postmodernist1848 Год назад

    After learning a lot of Python I tried C and it really changed my perspective. At first, I thought that static typed languages are inferior (are dumb??) and then I realised that it's much, much easier to understand and maintain typed code. Static typing is a great feature, not a disadvantage. When I came back to Python, I knew I needed to use (optional) typing

  • @LewisCowles
    @LewisCowles Год назад

    I Liked this. Another reason to use them (I wrote you an email about not liking type-hints) is that it can make it easier for compliant tools to assist a coder in refactoring in certain code-bases. I Still don't type-hint everything, but have found myself using type-hints more and more.
    Do you think Guido comments have proven to be unhelpful in use of this language feature, or perhaps are misinterpreted?

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thanks Lewis! Your email was one of the reasons I created the video 😊.
      I can’t speak for Guido, but programming languages are always changing, leading to new insights into how to make things better which in turn changes our opinions about things.
      I’m in favor of a stricter Python, while at the same time not losing the flexibility and simplicity of the language in its current form. But let’s see, that might also change in the future 😁.

  • @p.t.8312
    @p.t.8312 Год назад

    Do I need a special VS Code extension to highlight incorrect types in, for example, function arguments? Looks like it doesn't work by default.

  • @davethorn9423
    @davethorn9423 Год назад

    If I had a function expecting a string , surely don't call the argument number , call it num_str or something ? Or am I missing something . Just clear naming seems to be a solution ?

  • @sandie_jr
    @sandie_jr 3 месяца назад

    Finally, I'm convinced!

    • @ArjanCodes
      @ArjanCodes  3 месяца назад +1

      Glad the video was useful then! Ahah

  • @kurkabane702
    @kurkabane702 Год назад

    Can someone explain why the function argument 'number' is reffered further as 'nr'? Is it some recent feature?

  • @johnjsal
    @johnjsal 8 месяцев назад

    Is that VS Code you were using? What is the name of that syntax theme?

  • @samsung40_media87
    @samsung40_media87 Год назад

    I do it automatically, it doesn't feel like extra work and follows on from explicit > implicit and defining the return type as a way to guide others so as not to use mixed return types

  • @Celestialzzz
    @Celestialzzz Год назад

    in vs.code for me type hinting does not flag a type mismatch error as you display at 7:55 in your video. Is there an extension that you have to download first to have this work?

  • @miguelvasquez9849
    @miguelvasquez9849 Год назад

    How should i type when a function receive an object that is not imported in my module? Should i import it only for the hinting?

  • @Han-ve8uh
    @Han-ve8uh Год назад

    7:44 we see the red underline error because the value is hardcoded, and is a simple primitive type right? In real programs we likely read information from a file or env, does that mean the typechecker will not be able to know whether the contents are right, since we haven't even provided the file or setup the env yet during development. I'm trying to get a practical sensing of whether this benefit is really that big a benefit.
    10:51 is the concept in the popup here that "input type hints can point out mistakes in output type hints"? Which means if we never wrote the input hints but only the output hint, there won't be warnings?

    • @totalermist
      @totalermist Год назад

      > we see the red underline error because the value is hardcoded, and is a simple primitive type right?
      No. The IDE will show the same error if you pass a variable of the wrong type (the IDE can infer the type of the variable even without type hints, since it has to be assigned to at some point). This of course only works reliably if the functions or libraries you use also have type hints.

  • @andrewglick6279
    @andrewglick6279 Год назад +5

    I love type hints in Python and I always try to use them. For people wanting safe validation from type hints, checkout the Beartype library (it is a decorator you can add to functions that verifies your arguments with your type hints).
    I do think type hints have ways to go, and part of that may be Python's refusal to enforce them even as part of convention. For example, there is something so much more elegant about `foo?: int` in TypeScript than `foo: Optional[int]` (I hate having to import Optional). The lack of type hinting enforcement in libraries also makes it difficult for IDEs to work at their full potential. And don't even get me started on how hideous the `Callable` type hint is. Though generally speaking, I think Python is headed on the right track for improving type hints.

  • @snippletrap
    @snippletrap Год назад

    Arjan you have the best programming content and the high production values are impressive. But if you're going to show your arms, you ought to tone them up!

  • @sanketower
    @sanketower Год назад +1

    Doing OOP without type annotations is practically against the basic design patterns that define that paradigm. Typing in Python may not be the default behavior and is technically just sugar coating, but adding it to your already structured project can help you discover a lot of vulnerabilities and bad practices hidden behind your Python trickery.
    Like they always say; make it work, then make it right.

  • @HaiderGill-th5bp
    @HaiderGill-th5bp Год назад

    I was always told to prefix my variable names with int_, flo_, str_, boo_ in C so I know what type it is, I expanded it for C++ obj for object why do it because datetime could be an datetime obj or a string holding a datetime...

  • @maksimon519
    @maksimon519 Год назад

    in short, type hints make development much faster. it also help editor to auto-complete the name of attributes, which makes writing codes much faster and satisfying.

  • @matrixtoogood5601
    @matrixtoogood5601 Год назад +2

    How to use type hints in frameworks like Django?

  • @Pawl0solidus
    @Pawl0solidus Год назад

    After working for years with Typescript I’m learning Python for some months and I can see extreme value in these type hints, they are really useful for the reasons mentioned in this video.

  • @airatvaliullin8420
    @airatvaliullin8420 Год назад

    I don't want to push you in any particular direction
    *beheads a lego man who is against typehinting*

  • @Maroonight
    @Maroonight Год назад

    I have a struggle to type hint my company's code. We have a python wrapper on C++ that defines a class inside a function. In that way, the type of the variable would be . I certainly cannot change how they write the wrapper, so how can I circumvent this problem?

    • @pasiasachilleas4739
      @pasiasachilleas4739 Год назад +1

      the problem is that is long? If yes you could probably define a short name for this.

  • @wilkyarny3012
    @wilkyarny3012 Год назад

    In case the next step of python update is to become strongly type language... Guido is certainly working on it in Microsoft. Like Microsoft provided typescript for Javascript devs. We can reasonably corroborate the same kind of path for python and a python runtime environment (like nodejs.) it's my opinion but nothing is sure for now

    • @ArjanCodes
      @ArjanCodes  Год назад

      If that happens, I would be very, very interested in that development.

    • @dennisvlahos
      @dennisvlahos Год назад

      Typescript is statically typed though, which is something Python never will be

    • @tkerkvliet
      @tkerkvliet Год назад +1

      Python is already considered a strongly typed language: we need explicit conversions between types and operations inappropriate to a type simply can not be performed. Strongly typed is something different than statically typed.

    • @nargileh1
      @nargileh1 Год назад +2

      What would be the point of strongly typed Python?? You can make your argument checking as stringent as you want without forcing the whole language to do it.

  • @DanielStoehr203
    @DanielStoehr203 Год назад

    Do you have a favorite Type checker? Pyright? Mypy? And why? Are there differences?

  • @mesaber86
    @mesaber86 Год назад

    Awesome intro :D

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thanks MeSaber, happy you’re enjoying the content!

  • @JorgeEscobarMX
    @JorgeEscobarMX Год назад +2

    I have to agree that removing type checks on unit tests is removing a lot of boring repeating work. As long as we are all consenting adults, I'm removing type checks from my own unit tests.

  • @hudabdulwahab2499
    @hudabdulwahab2499 Год назад

    Is it possible to make type hint videos for cases like doubly-linked lists etc? e.g. it's a bit annoying when the node is a class object and we can't define their types as Class | None

    • @ianakotey
      @ianakotey Год назад

      Use Optional from the typing module

  • @bitswapper
    @bitswapper 10 месяцев назад

    VSCode doesn't show the type mismatch in python when I use type hints

  • @TheKubaski
    @TheKubaski Год назад

    I noticed that you use a tab to debug the python code. Does anyone know how to set that up?

  • @Betacak3
    @Betacak3 Год назад +6

    Source code is meant to be read by humans. You only ever write a piece of code once, but you will read it many times. So, whenever you ask yourself "Is it worth it to spend this extra time to write this?", think of the time it could save when you have to deal with that bit of code 6 months or more later.
    That's my response to the "It's more work" argument and - in my experience - a good way to approach code in general. Don't write code for your computer. Write it for yourself and your collleagues.

    • @banatibor83
      @banatibor83 Год назад +1

      Very very good point. The most important attribute of code that it should be easily readable. That's why I like to use small functions with self explanatory names, and spend time on naming my variables. When things are well named it is easy to read and understand the code.
      Type hints start to get extremely helpful when multiple scrum teams are working on the same software. Unavoidable that these teams will develop a common library for certain tasks for that software, type hints in that common library can save many hours.

  • @CynicalOldDwarf
    @CynicalOldDwarf Год назад

    I like the concept, hate the syntax though.
    I wish they'd have kept a c-style typing syntax (as in type first, e.g. int a_number) instead of using typescript's syntax

  • @ranjansingh9972
    @ranjansingh9972 Год назад +1

    Type Hints add to why I find Python to be a frustrating language. It’s a language that seems to support every paradigm of programming, and because of that it’s hard to know ‘the right way’. It’s just endless learning of new language features and tweaks - and there’s no escaping it because the libraries/frameworks you use pull in language features from across the spectrum. Probably been trying to learn/code in Python for 6 years and still feel like a complete novice. That same amount of time in an older language, like C, was enough for relative mastery (vastly different eras though, I concede).

  • @majolakrem77
    @majolakrem77 2 месяца назад

    Say you have a class that has as an object another class, which in turn contains a reference to the parent class. Using type hints is quite useful as it allows you to navigate the structure of your class(es) easier while coding. However, it throws an error during execution as python detects circular class definitions.
    Currently, I code using type hints for such class dependencies but later erase the imports again. That's unfortunate...

    • @cholobok
      @cholobok 8 дней назад

      Use “from __future__ import annotations”

    • @cholobok
      @cholobok 8 дней назад

      It’s pretty cool to define recursive types that way too, like
      type Operator = Literal[“+”, -“]
      type Operand = int | float
      type Expression = Operand | tuple[Expression, Operator, Expression]

  • @presstv
    @presstv Год назад

    Did you kill Person A along with Bernard The Headless? that's not fair🤣😂. great video Arjan :)

  • @hshhsjhahsvs7728
    @hshhsjhahsvs7728 Год назад

    Am rooting for type hints included.

  • @willemvdk4886
    @willemvdk4886 Год назад

    I definitely like "Type Driven Development" way more than I like "Test Driven Development", honestly ;)

  • @parisqs
    @parisqs Год назад

    Beware: If you don't write type hint, you LEGO body will be torn apart!

  • @davidedigrande3204
    @davidedigrande3204 Год назад

    My internal OCD programmer COMMENDS that I add type hints in all of the code. Sometimes I do it in variables declarations too, when the type of the object is not obvious.

  • @k98killer
    @k98killer Год назад

    Might be preaching to the choir with this one, but I imagine some people haven't experienced the difference between coding with type hints and coding without them.

  • @Andrumen01
    @Andrumen01 Год назад

    Think about typehints as seat belts for code. In principle, they aren't needed, but during a crash they can save you!

  • @hudabdulwahab2499
    @hudabdulwahab2499 Год назад

    0:55 I kinda get the hint there

  • @appuser
    @appuser Год назад

    It's hard not to like them when you discover bugs right away when you start using them and from that point forward (that would otherwise escape your notice).