Add to Watchlist

How you can benefit from type hints

1 views

Citation of segment
Embed Code
Purchasing a DVD Cite video

Formal Metadata

Title How you can benefit from type hints
Title of Series EuroPython 2015
Part Number 139
Number of Parts 173
Author Vlasovskikh, Andrey
License CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
DOI 10.5446/20079
Publisher EuroPython
Release Date 2015
Language English
Production Place Bilbao, Euskadi, Spain

Content Metadata

Subject Area Computer Science
Abstract Andrey Vlasovskikh - How you can benefit from type hints PEP 484 introduces type hints for Python 3. Type hints can increase readability of our code for both humans and tools and lead to better and safer outcomes. And we'll prove it in this talk! We're going to take a closer look at type hints, see practical examples of where they can be used and the value they provide. We'll see that simple class types and built-in collection types are often enough for our public API's. We'll also discuss how you can benefit from type hinting stubs for third-party libraries and briefly cover more advanced scenarios like generic types.
Keywords EuroPython Conference
EP 2015
EuroPython 2015
Series
Annotations
Transcript
Loading...
so I welcome everybody and 100 muscle skin and I'm going to talk about how you can benefit from time so that I can come from the city called saying this is where it said the city in Russia and I work for a company called generation and I'm 1 of the developers of the idea called white uh also I've been contributing to the new paradigm warrant for holding typing and I'm very glad that it will be the bottle of vitamin was and I would really like to to have faith in tool the authority for this region you guys and who crash and also to the media tell for this market who actually accepted this point so what here time inspire like that any information provided by the user of all the types in their programs like types of function of functional parameters type of class attribute and so and that therefore it was just as the formal notation notation for all time so what I'm going to be talking about out here it is basically the motivation behind type in and how you can get started with them the of all so I will talk about the best practices for annotating your cold with him what is out of the scope here these overview of the type system what kind of types can you write your code and also I want to walk through the whole time module and this is out of the scope here probably some of you have visited the told by agreed on a also owned by the end of a couple of questions for you but what you will has actually listened to read all about what actions please raise your hand great almost everyone and how many of you will have actually looked into this document therefore it for alright evolved to site and having ever had a chance to actually play with that and a couple of rate so you are you came to the right tool for you and you have a hundred alright so time using Python then use to be informed so
basically uh by the problem arose would define type used inside a Dumpster uh and those definitions of users usually given in an informal language or in natural language such as English for example here a piece of documentation from the standard library of all the fields of function as you can see there are lots of words highlighted in blue that actually refer to type and that therefore
it was basically defines the for implementation and as you can see that this is the type used for the same field of function and there are a lot of the ability to dance and if you have a bit of understanding of what typing on this notation I think is much more clear and just 1 thing I want to I remind you from real is that Python will always be a dynamic language and that type of his part on optional all right so from my perspective the main benefits of type annotations is readability type you sample of altered ability for both humans and to but I mean by credibility by humans the lecture I mean documentation and augmentation with type is is a lot more easier to grasp if you are already have some limited
familiarity with typing and then collected in natural language description and by readability by tools I mean that possibility of creating type-checker essentially in terms of life and also the tools that help you write code for example called completion type information is crucial for those kinds of tools and that's what we're going to talk about it in this talk so let's start with the type what tools do support type use right now actually I know only 2 of them is that might their regional tool that uh was the main inspiration for this for for written by you collect political science and by chance and I should know that we have every community edition of by chance that is completely open source under the Apache License and all our type inference and analysis codes so that that is also open source of high chance to quantify 5 that was released earlier this year has already some support some basic support for typing but that there wasn't ready yet at the time we are really releasing by chance so but only some possible the specifications are support and by job 5 is coming this fall and it will include full support for typed in the of several of developers also called analysis tools like violent and have already announced that they're going to support the type used in their tools and followed by the idea that that uh um it will support evolved and 16 alright so as to give an example for understanding actions and as an example I've chosen the library called elementary it's a part of the standard biting library and uh I would like to ask you how many of you are familiar with elementary is almost everyone right for those of you who are not familiar with is a library for handling among data from Boston going surmountable dealing with it we then they get right this is nicer than the step don't maybe so let's start with the library and let's right some is in all of this is that it is 1 of them and just for the sake of brevity and let's call it the tree is our own modified versions of documents and images later on I will show you how to deal with the library without modifying so here are some little example of using that elementary library and if you are quite familiar but with the API library probably would not something here dust everybody but everybody anybody see the problem here was right here in this part of Florida here just the idea behind it all right that's it so basically when you're on this cold is the result would be a we will be a type because an elementary can lies binary by the way there is another area here you can specify integers as well some of 2 so yes you have to always around this program in order to find this there if you are not that familiar with the material and as we'll see right now the type checker tools will find this pair this kind of errors for you automatically with a little bit of help from taking his soul will start annotating just the construction of the elements class here we add that stir annotations for the task parameter we expect it to be as changed otherwise and we expect that attribute parameter to be in a dictionary of all integer source during his and string uh and so it's a generative alright and with the the
help of this type of energy and what we can get 4 and tools yeah is the output of 2 that with all those tools by chance will underline that problematic and with this you'll squiggly line and it basically says that it expects the type strain but it goes by and that might might be a command line tool called basically the same information but only in the textual form all right so thank you it somewhat
useful let's continue annotating the API of the century like most types you will write the practice are quite simple for example the registrants space function except just 2 strings that we're talking not mostly there is normative statement in this form so we annotated with the return flight number and the element that is element function of all that it says basically anywhere you and check if it is actually an XML and sold we specify the junction of 2 types of of the different parameters as any a son type used on a little bit more complex but for example below we are using some genetic types that is a function returns an iterator over elements and will not as this single occasion we don't have mentioned in his talk because we are in for a reference to the class itself while we are defining and that detects function returns just an iterative cost alright so with the help of of we have have annotated all the guys and what could we get for from that
using that here's a little piece of gold by chance of let's find out what this program that's a that's a program that creates an XML elements of this page 110 soul is kind the HTML that contains the speed properties sold heading the heading variable is there an element with the opportunity for in page 1 and then we bring basically at the text of all the lost not all textual mold of this element by juridical texts also supplements inside this somehow and 1 and bring them single walled him all the result would be if case come all our world formation and actually there is a problem in the scope of that by charm would not with help of those type him and in order to find out what the problem is and why my child will find it and we should think of algorithm most of these things here so let's say it has that that i the fruits of the earth that would think all this project we have to find out if the indexing operator that square bracket apparatus is available on the result of the texts so let's find out what the attempts to talk a lot we still don't know what what will which class contains this statistics of matter because we don't know all the possible the heading variable so we need to go and look through and look up the definition of the hidden variable is right there on the previous slide and it is assigned uh Raton below the upper case having fun and knows that topic is Haiti has no type annotations whatsoever part a type checker would try to infer the return type of the topic is heading function in this case it you don't spend a lot of elements variable and it is assigned that will you all basically calling constructed on that this elementary got class so we know that the local element variable is actually a variable of type element it's an information inference inference from these very basic examples and we know that the total value of the property is hidden patterns would be better so variable the global variable tagging is also studied and let's go to the previous slide to to find out what each of text on elements of these returning according to our region type if you
do this is the last line on the slide but we have an object that is attached to a they over
strings as well as well as many of you would probably not probably due problem now that an iterator define it as an abstract base classes and it defines only 2 methods is that the diameter of the terms that up and down the next so there is no way that redundant data item method that is available on the literature so we cannot access you can have access to the elements of the literature by using the indexing so it is clearly an and try with the help of some some type inference by basic algorithms in this example but we don't have of with quite more advanced ones that can be a world can find this kind of error and know that we don't have to annotate the function of some of the reasons which in mostly another way
to think you can get from type in the school completion so by chance we will also called context-sensitive cold completion meaning that it is based on that uh actual type of the very it shows on the metals and use of the all the type of the variable so this is the same data sample we are iterating over the inter-rater all strings and we have people called completion all the tutorial will be the variable so we are doing all over that we know that the type exchange also we show that matches the string class and the members of the it's a super so those are the
benefits you can get from 2 let's switch to the benefits
from the documentation also in the mutation could become quite rewarding if you are careful writing down all the types of your function definition in English curiously and is a real world example from the standard library documentation for that function called Common all that treatment and not only is it what it what it also contains the actual area here marked in red it says that we can foster by string in here what is it you can see from the example below if we are actually asked the question which of them to register as it will bring them by the little has this report the quotation marks and so this is great this is it's not absolutely not what you are trying to do here so either the documentation is written badly and that not tested or the implementation has got something is definitely wrong or maybe I just don't get the distance to foreign speaker maybe or so so and what would be it would be better if we wanted to this function which type type things that aren't used just the don't change from from the library I did exactly these here and I used this seems to be the same as the condition generative applied with that the adult extension hold about extension can extract the docstrings from the court as well as and from my point of view of this piece of information is much more to Europe and its explains what is going on here using better and more formally defined syntax and time more I'm with this kind of mutation that this land here the numbers on the mutation that has they know that you can actually you a check your type used in some kind of test you can write a test suite for a cold and run a static type-checker on the objective function annotation what you can check that English and the language recommendation this is the idea
all right so now I'll go and I'm going to give some uh tips about how to actually use the type fields in order to be productive and just think the most important thing actually I want to tell you that presentation is that you should annotate your public API if you alterable or some kind of library that is used inside the company and uh colleagues are using it or you on and all for all samples source these and you want people on the Internet to start using a lot of the library and you want to get 1 of police please do we you type his voice of public API this is the most important slide I would like you to remember from that this whole talk and it is so important that I put reference here to this very slight so you would find it on the internet places so please if you like take a vote on something like that yes this is that the main idea that the conditions for your body maybe I would really make use of heavier because it would have liked that 1st of all the evidence but mutation and act like a safety net around the library basically you are defining a clear interface between your and the use of coal and give some kind of error is happening in the the library here while the the you know source was and they could not have lecture hall they have can just the errors their idea in and they will see that they are using it wrongly they will not to report the wrong box to tracker just because they don't get it in uh they wanted to know the condition right so this is the idea of types are like that of mutation by for ideas and so on and uh as I already told you you don't have to update every just you have to go public API and see if for I think that most cases that would be enough another thing is start with simple types the more space it is enough to use types like served by the built-in types and so on you can use built-in collection times like these decay and you will want to specify the types of elements inside your election please use action types from the technology the case version of capital cases deep and so and that therefore it was you know defined some advanced type constructor well by what use them only when you really need them and remember that you can always put any given some particular type is triggered you don't know how to express it was so long and so on so to stick with the simple things they would be a simpler for users and you won't spend time trying to invent some crazy started diving concert all right so the novelty of he's the liberal what set your functions and conservative in what you don't know if you have a little bit of both is basically a function that iterates over uh into the middle of the integers and filters out the even elements of a hundred tons of these all that here if you are using only that he and on interval greater on number because really use that generated comprehension the call so you don't have to restrict your users to fast and here for example on or off and iterate on it is not to say that the numbers that should be and trouble the more generic type you can probably imagine that would work for this function and speaking about it on values in this case I think you would be quite specific about what you're doing because you might want to allow users to use some methods on the country type we are drawn for example here we are at in the least exclusively and you might want to all users what some of that and or something like that and all that that typing module defines all kinds of these abstract connection types like maybe or sequence when you can access online index of data set and so on so that means for your parameter but there are some of less good for you the depends structure on on you all right so far we've seen some directions written formally using Python 3 translation right and in order to use those annotations it is enough for the use of to just people solely library that I patients find the call them available for things and this is compatible with white and to and some the appointment versions if you will stole that I've been module from idea right now you have to specify type people's equals 3 . 5 . 0 better 1 otherwise you will get just package but it is said that this is because of the library is very new and uh that developers have had haven't had a chance to fix the a and what about 22 I guess my point of view what actually I was invited to so here's the other with all of all going to diversify the older you are using bind to hand 90 per cent and what you I 1 of this is original by having is by having made alright so that those of you who do you need to support by tools there is this thing called white stops are just a good reminder of what they are they're are about files with a different view like I extends uh and they contain all but 1 of those jobs that leaves us earlier and here's our little starved for
that and see whether actually a library of this standard library what might be going on and this is we should define directions for this library not by modifying the of of the what where I was kind of stuff that you can use these by the 3 stops the White and Gold and that adjectives we will understand that so steps for good
for why the polarized have stopped having fun and did not only the system library logic for of that but as well type-checker as we would
expect them to be somewhere in on the opposite of what uh so if you are creating some kind of product that you have lots of the part of this and you want to them in order to get medical condition even just created fall inside a project for example affordable FolderBolt type units and your vision for for all the libraries and just added to your set of and then you can for example share this fall off as with the rest of the world of using the Don we're all along or what not and your colleagues we really have no type annotations and alright so you do that kind of stuff you find interesting and creative creating type by themselves files please share your style with subsequent models because of annotating about library there are several ways of doing that the main is to send that polar questions to this new type shareable is there a mention for form and it is supposed to be the universal collection of all the stocks with uh basically it's know so modeled after task definition type triple for that tragedy the language that has type hints from the it's going to base so when you want I and this model of greatest obstacle libraries is very successful is very very well established in the typescript community and in that of the community as a whole for example people who are still using Johnston they use this the form this definition director of the intact for the project and check and they they're called against the stats diffusion of libraries so it by then we will have the same situation where you can actually check your called this slot collection of possible of all kinds of libraries another way survive in the bad to create a package with stops 1 by the and use it what if you don't want to invest in writing directions i the function annotations then you still can benefit from the for example if any of you have like user so you might not not always what you already using what and annotation of because by chance we have this kind of behind-the-scenes repository of all types cancer for the standard library not multiple of about 4 some of so basically you are already using that his about various using are all let us assume that and we are going to switch to this new standards that's why we have to assess this proposal and we we have to be into it want all the tools used the same check collection of stops in order to be more productive but another way of using the some but other people would be calling this terrible and start alright so use I want you to try it and variable which I'm a fan and as you probably already know that so there is and there are a few steps if you very simple steps how to you you can get started with just the new latest were kind of 20 . 5 or use some older version of the and get some of the tools my Bible by chance and just start using the same hymns try but I do want to know what you thoughts about it so please come to me you me on Twitter and my website to extort are you really want to hear what is your type you do like that do find them good for users only about 4 users I went to switch to by the tightly and I'm very interested to during all those those kinds of stuff from you so please please come to me and the size already available on the internet on real this year so thank you for your uh the addition of some of the data and any questions I I think it's
a little over 100 users usually Gouldman the each especially to the type used to conditions for the receivers part for obvious obviously well you have to write some kind of tool that you would want to stand there uh just actually got of your hobbies libraries and generate this kind of those kind of so it is a piece of software that is perfectly well and it's a good idea to this kind of things for for for your purposes but can you know you children to the time interval is visible part of the idea of this sort of ideas thanks for the talk so you said you shouldn't call annotate our public figures and he also said that much of example philosophcially to the 1st type is users from the cold itself but in some simple cases for an it is also a way to create stubs for us with those related to individuals who are reacting about this kind of feature the and value of the but it's still work in progress sold let's say you will see that what is possible here we are quite good at the very very the tight I don't I functions but it is very educational the for of the type of parameters to the bank was the only source of information for parameters actually there are 2 sources that have used to accessing of all the parameters of the body the function and the user just for the fun function but they're all kind of the soul that contain much less information than you can get for the entire time based on sampling construct there's only 2 of them so you it was possible Is it was Bush's in countries in of flowers tastes like when types different and I didn't have have 3 of certain European the Russian is supposed to have like in a place to stop files are yeah definitely and there better for for define several ways so for using additional like that 1 the we don't support and with them and by China for 1 5 but there was for them much do you think that it would be a good way to provide source compatibility with like did make 2 . 3 2 generates stockpiles from the source code instead of repeating itself defined API basically 2 in life genetic I the talk very much in development and then generated stockpiles files from the resource come on yeah yeah it is actually a very simple task like this and this and that it would be a good addition for example for for that might to yes yes different possible hi is there any hair follicle project putting the divergent genes and all those kind of library because it will be at spotting would eat all their money on 1 level 3 . 5 release having almost on the library and you the project is called actions so that will eventually has that stops for the whole of vitamin the 1 5 library and wasn't oneself library there will be subset of I'm giving you the need to to genetic so 5 from the Duke studies have you know where of any kind of like that would be not sold of great 1 of of the world thank I the its own around with with my height and the 1st thing I looked into was trying to detect the function that has the option of values that along with the something else that's something that might not have been because there was a term didn't work so hard it's so question this of history and we used to support those kind of jokes when we still didn't have any type of annotations was over and we use only the inferred 1st that information and we used to have these checks when you have to actually derive something like these you and then you would be able to use the value of the and if you are we will be sure that it it does not matter but then we have received a lot of reports from our already know is that this is not what they are expecting what they were expecting from my job because the difference of inference time would be if not so thank and if you are explicitly specifying that you are that you expected and optional type 1 or you are daunting an optional right and then using this type of then I guess we can trust the orientations because this is not what you are expected to go from this is the defined behavior over all of your functions and we will see also checking for for optional dives right from high anyone questions not
Point (geometry)
Computer programming
Code
Multiplication sign
Mathematical singularity
Parameter (computer programming)
Attribute grammar
Type theory
Bit rate
Positional notation
Hypermedia
Authorization
Module (mathematics)
Physical system
Social class
Programming paradigm
Electric generator
Information
Software developer
Port scanner
Functional (mathematics)
Type theory
Computer animation
Pauli exclusion principle
Module (mathematics)
Formal grammar
Website
Physical system
Annihilator (ring theory)
Standard deviation
Dynamical system
Mathematical analysis
Perspective (visual)
Field (computer science)
Formal language
Sound effect
Type theory
Positional notation
Iteration
Formal grammar
Compact space
Information
Augmented reality
Sampling (statistics)
Code
Bit
Functional (mathematics)
Element (mathematics)
Type theory
Word
Computer animation
Function (mathematics)
Pauli exclusion principle
Typprüfung
Natural language
Library (computing)
Standard deviation
Code
Ring (mathematics)
Multiplication sign
Source code
Parameter (computer programming)
Complete metric space
Function (mathematics)
Data dictionary
Mereology
Food energy
Medical imaging
Video game
Type theory
Error message
Descriptive statistics
Social class
Area
Metropolitan area network
Software developer
Constructor (object-oriented programming)
Parameter (computer programming)
Bit
Open set
Type theory
Process (computing)
Network topology
Order (biology)
Typprüfung
Right angle
Energy level
Writing
Resultant
Computer programming
Freeware
Open source
Line (geometry)
Typinferenz
Online help
Mathematical analysis
Electronic mailing list
Attribute grammar
Revision control
Latent heat
Term (mathematics)
String (computer science)
Integer
Regular expression
Task (computing)
Form (programming)
Compact space
Information
Element (mathematics)
Mathematical analysis
Code
Machine code
Line (geometry)
Inclusion map
Computer animation
Function (mathematics)
Pauli exclusion principle
Element (mathematics)
Natural language
Key (cryptography)
Library (computing)
Image registration
Parameter (computer programming)
Junction (traffic)
Inference
Variable (mathematics)
Type theory
Information systems
Social class
Musical ensemble
Metropolitan area network
Algorithm
Spacetime
File format
Hidden variable theory
Ext functor
Bit
Variable (mathematics)
Functional (mathematics)
Higher-order logic
Type theory
Category of being
Eigenvalues and eigenvectors
Order (biology)
Pattern language
Resultant
Web page
Slide rule
Computer programming
Statistics
Online help
Number
Iteration
String (computer science)
Operator (mathematics)
Form (programming)
Information
Poisson-Klammer
Element (mathematics)
Projective plane
Operator (mathematics)
Grand Unified Theory
Inclusion map
Subject indexing
Computer animation
Personal digital assistant
Statement (computer science)
Element (mathematics)
Social class
Iteration
Local ring
Slide rule
Typinferenz
1 (number)
Online help
Variable (mathematics)
Type theory
Iteration
Term (mathematics)
String (computer science)
Error message
Metropolitan area network
Algorithm
Inheritance (object-oriented programming)
Element (mathematics)
Ext functor
Line (geometry)
Functional (mathematics)
Diameter
Subject indexing
Database normalization
Computer animation
Element (mathematics)
Iteration
Object (grammar)
Abstraction
Type theory
Arithmetic mean
Type theory
Computer animation
String (computer science)
Mathematical singularity
Sampling (statistics)
Code
Complete metric space
Context-sensitive language
Social class
Standard deviation
Suite (music)
Group action
View (database)
Multiplication sign
Direction (geometry)
Mathematical singularity
Source code
Parameter (computer programming)
Weight
Unicode
Formal language
Word
Type theory
Cuboid
Module (mathematics)
Extension (kinesiology)
Abstraction
Error message
Typprüfung
Area
Spacetime
Software developer
Computer file
Constructor (object-oriented programming)
Keyboard shortcut
Sampling (statistics)
Interface (computing)
Ext functor
Bit
Port scanner
Functional (mathematics)
Sequence
Element (mathematics)
Connected space
Type theory
Process (computing)
Order (biology)
Right angle
Erlang distribution
Point (geometry)
Filter <Stochastik>
Slide rule
Implementation
Presentation of a group
Computer file
Real number
Translation (relic)
Electronic mailing list
Distance
Field (computer science)
Number
Revision control
Internetworking
String (computer science)
Software testing
Integer
Traffic reporting
Mathematical optimization
Condition number
Standard deviation
Information
Conservation of energy
Element (mathematics)
Code
Density of states
System call
Inclusion map
Subject indexing
Voting
Computer animation
Personal digital assistant
Function (mathematics)
String (computer science)
Pauli exclusion principle
Element (mathematics)
Iteration
Abstraction
Library (computing)
Extension (kinesiology)
Goodness of fit
Type theory
Computer animation
Logic
Direction (geometry)
Computer file
Element (mathematics)
Social class
Binary file
Library (computing)
Extension (kinesiology)
NP-hard
Standard deviation
Group action
Gradient
Direction (geometry)
Scientific modelling
Multiplication sign
Orientation (vector space)
Mathematical singularity
Source code
Parameter (computer programming)
Mereology
Inference
Formal language
Subset
Video game
Type theory
Computer configuration
Metropolitan area network
Beta function
Product (category theory)
Software developer
Shared memory
Sampling (statistics)
Functional (mathematics)
Type theory
Process (computing)
Repository (publishing)
Internet service provider
Order (biology)
Website
Right angle
Figurate number
Quicksort
Arithmetic progression
Task (computing)
Freeware
Observational study
Computer file
Twitter
Revision control
Goodness of fit
Term (mathematics)
Energy level
Traffic reporting
Subtraction
Units of measurement
Task (computing)
Form (programming)
Condition number
Addition
Standard deviation
Information
Projective plane
Code
Binary file
Computer animation
Software
Personal digital assistant
Pauli exclusion principle
Statistics
Library (computing)
Cloning
Loading...
Feedback

Timings

  597 ms - page object

Version

AV-Portal 3.8.0 (dec2fe8b0ce2e718d55d6f23ab68f0b2424a1f3f)