Type-Checking Python Programs With Type Hints and mypy

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ค. 2024
  • dbader.org/python-tricks ► Avoid the bugs that Python type hinting will catch in the first place and see how to write clean and Pythonic code
    Get quick intro to Python type hinting using the mypy type checker, in order to catch bugs and improve the quality of your existing Python code.
    In this video I'll walk you through a simple example of what Python's new type hints allow you to do, then I'll show you how to install and use the mypy type checker as a static analysis tool from the command line. At the end you'll get an outlook of the future of type checking and the type hinting syntax in Python.
    FREE COURSE - "5 Thoughts on Mastering Python" dbader.org/python-mastery
    SUBSCRIBE TO THIS CHANNEL: dbader.org/youtube
    * * *
    ► Python MUGS, T-SHIRTS & MORE: nerdlettering.com
    FREE Python Tutorials & News:
    » Python Tutorials: dbader.org
    » Python News on Twitter: / @dbader_org
    » Weekly Tips for Pythonistas: dbader.org/newsletter
    » Subscribe to this channel: dbader.org/youtube

ความคิดเห็น • 25

  • @FRIENDSofCAP
    @FRIENDSofCAP 5 ปีที่แล้ว +25

    FYI:: mypy-lang is not longer supported according to the terminal response... And as of pyton v3.7.4 (which I am running) suggests installing 'mypy' (i.e., pip install mypy'

    • @eduardoruizrios6140
      @eduardoruizrios6140 ปีที่แล้ว

      Maybe few years later, but now as indicated in the official documentation you can install mypy by running the following command in your command-line tool: python3 -m pip install -U mypy. In addition, is not mandatory to run the function for mypy to warning you about type errors. Actually you could just run mypy file_name.py and it will display on the command line all the lines where a type error was found!

  • @RameenFallschirmjager
    @RameenFallschirmjager 4 ปีที่แล้ว +9

    From the chaos and the menace that rage out there, I take shelter in serenity and stability and reliability of programming!

  • @hehehe289
    @hehehe289 3 ปีที่แล้ว +6

    two things:
    1, you should run pip install mypy now since the package names have been updated
    2, you don't have to run the function in your code for it to be checked by mypy

  • @rikless
    @rikless 7 ปีที่แล้ว +6

    Is it possible to type hint objects ? For example, to force a specific class type on an argument.

  • @MrNagios
    @MrNagios 7 ปีที่แล้ว

    Thanks for explanation.

  • @EmadMokhtar
    @EmadMokhtar 7 ปีที่แล้ว +8

    Is there a way to integrate MyPy to test suite?

  • @lindhe
    @lindhe 5 ปีที่แล้ว

    Cool. Does type hints work with keyword arguments also?

  • @OthmanAlikhan
    @OthmanAlikhan 4 ปีที่แล้ว +1

    Thanks for the video =)

  • @aoeu256
    @aoeu256 5 ปีที่แล้ว

    IMO: doctests with extended syntax can be used to derive types, and they are a better form of documentation than types.
    Which is more useful:
    join(Sequence[Sequence]) -> Sequence
    or
    % join(["cold", "bath"]) -> "coldbath"
    % intadd(!1, !2) -> !3
    So it will look at the arguments in your test, and annotate the function but any list or str will generalize into a Sequence and ints and floats will turn to Number unless you write ! which means don't try to "generalize" the types. It can run at about the same time that the type system works on.

  • @sinekonata
    @sinekonata 3 ปีที่แล้ว +3

    So 4 years later, is type-hinting and type checking the confirmed future of python?

  • @dhaval1489
    @dhaval1489 5 ปีที่แล้ว +2

    Does it help decrease processing time?, Does the interpreter check hints and not spend time on guessing type on run time? That would be huge incentive other I am good with dynamic style.

    • @user-nf8zb4qp6j
      @user-nf8zb4qp6j 3 ปีที่แล้ว

      AFAIK, no. This is not really affect your program in a runtime. It's just helps to check that you are using your function with a right types. You still can call function with arguments of any types.

  • @tcarney57
    @tcarney57 6 ปีที่แล้ว

    Two questions and a comment: How would mypy react if you left out the annotations on the arguments but included the one on the return value i.e., *-> int:* ? Can mypy read annotations in a same-line comment instead of embedded in the code?
    My comment is that embedded hints add some visual noise for the human reader. *add_them(a, b):* is quite a bit easier for the eye to follow than *add_them(a: int, b: int) ->:* . A compromise might be:
    *add_them(a, b) # a: int, b: int, -> int*
    In the good-old qbasic days, you used a single character ($, %, etc.) appended to the variable name to force a type: ex. *name$* or *age%* . This was not as visually disconcerting as the PEP484 annotations. I guess as long as one reads annotated python code in a color-coded editor, the visual noise will be reduced somewhat since the annotations will be immediately apparent.

  • @vincenzofiorentini8002
    @vincenzofiorentini8002 6 ปีที่แล้ว

    for me, this works in 3.5.4 by having an 'import mypy' in the code. with it, the type check works, without it has no effect. 'mypy-lang' seems seriously deprecated.

  • @aharongina5226
    @aharongina5226 2 ปีที่แล้ว

    we have inspect functionality in pycharm

  • @vijayabhaskar-j
    @vijayabhaskar-j 6 ปีที่แล้ว

    What if I need the add_this function to handle both int and float?

    • @Berl0ga
      @Berl0ga 6 ปีที่แล้ว

      Vijayabhaskar J Union[int, float] or Number (from typing)

  • @krishna_o15
    @krishna_o15 4 ปีที่แล้ว

    does this needs to be done for every overridden method also.

  • @soldadopreciso
    @soldadopreciso 4 ปีที่แล้ว

    thanks pal.

  • @ORagnar
    @ORagnar 4 ปีที่แล้ว +2

    I just realized, you sound like the Yankee slugger Alex Rodriguez.

  • @jonathanmoore5619
    @jonathanmoore5619 5 ปีที่แล้ว

    Python is starting to eat its own tail... Programming is about a means to an end... Not an end in itself.

    • @sgt391
      @sgt391 4 ปีที่แล้ว

      it is an end for people developing the language

  • @RameenFallschirmjager
    @RameenFallschirmjager 4 ปีที่แล้ว

    So vanilla python is not that much great after all!