So this paper is written with Julia Camiotti, who is a PhD student here, and Emanuel Oriol from the Toulouse School of Economics. In fact, the idea of the paper is the following, is that when I was a student in the development economics, all the main people are actually with Hirshman, Boron, Nyaume, Meir, and all those people were thinking that norms, culture cannot be an obstacle to growth and development. And in a sense, they will be moving in the process of growth, so you shouldn't be really concerned with that. I call this the organic approach to culture and norms. But then the mood change, and I think it's changed mainly when Amartya Senke came in with his writing, saying that well-being cannot be measured only by material level of living or income ahead and things like that. There are other dimensions that directly affect the well-being of the people. And I think the talk we had yesterday by Josly in about situations in San Salvador shows that it makes no sense just to measure development in terms of income level. So a second point is that indirectly, culture matters or norms matter in the sense that it may constrain the way economic opportunities can be seized by the people, for instance by women. If you have mobility-constraining norms like seclusion norms, it's obvious that you can say we have growth and expanding economic opportunities. But if they cannot be seized by a part of the population, then there is a problem in culture matters. So in a sense, since the 80s, there has been a huge focus on the issue of what we call social engineering, by which we mean an attempt to legislate to change the situation, to change the norm, to radically modify the norm, so that victimized parts of the population or marginalized parts of the population can benefit from these changes. And the UN has been in the vanguard of that. So in a sense, it's not coincidental that this conference is sponsored by a wider institute who belongs to the UN. So the first one was in 79, the convention to eliminate harmful practices called CEDO. And later, there was in 95, the so-called fact sheet on harmful traditional practices affecting the health of women and children who listed five main harmful practices, among which was female genital cutting, early marriage, early pregnancy, and female infanticide. So for the first time, there was this idea that you should do something, by the way of law, to change these harmful practices. And of course, this list is not exhaustive. What we try to do in this paper is really to provide an analytical guide about how do you go about social engineering and the impact of the law. So in a sense, you will see an echo to several things that I've been saying in several papers yesterday. And so in a sense, you can consider the paper as a reasoned survey of the literature, even though it doesn't claim to be completely exhaustive in terms of the literature. But it tries to say, OK, what kind of evidence have we to illustrate some ways of looking at the mechanism that is behind this attempt to change harmful norms and practices? Now, the first basic distinction that we have in the paper is the distinction between the expressive and the deterrent effect of the law. The expressive effect of the law is the design that the simple fact that the law is there should change the practices, even if the law doesn't have punishment mechanisms or enforcement mechanisms that is disposed of. The simple fact that it is there should have an impact. Now, the analytics of it is essentially that what you have in mind is a coordination game in which there are several Nash equilibria. One is social inferior to the other. And the custom which, in fact, involves the harmful practices is conceived as the socially inefficient equilibrium. And the whole thing is, how do you shift to the socially superior equilibrium, which is, for instance, you reject the harmful practice, like female genital mutilation, for instance. How do you shift from there? And since you adopt the idea in this literature that you have a kind of focal point story, then you say it's just a matter of changing the focal point of the people so that they believe are converging on the new equilibrium or the good equilibrium. So the idea is essentially, in the way of that Akarov initiated thinking about the caste system of India, is that people don't like the custom. In fact, they hate it. But they all believe that the others are going to follow it. And so because this pessimistic belief about the other thing and the way they will behave, people behave in a way that they themselves dislike. So they say, it's just if you can change the focal point and you bring the law, then this would be sufficient to improve the matter. So the best way to figure that out is to look at this simple game. It's a pure coordination game with symmetric payoff. And I take the example of female genital mutilation. So there are two actions that are possible, your culture daughter or your rejected practice. And the idea is that mu is a parameter that measures the benefit of coordinating. So if you coordinate on the rejection of the practice, you've got the full benefit. If you coordinate on the circumcision equilibrium, then you get minus, but you have to subtract the cost of it. And we imagine that the cost can be measured by a parameter theta. Theta is a psychological or health cost for the girls of benefits. And v is something that I will elaborate on later, which is, let's call it, an amplifying factor, which is determined by the environment, policy environment, political environment, NGO actions or interventions, whatever it is, which may increase the cost theta. If you look at that game, it's easy to see that there are two pure equilibria. There is also a mixed equilibrium, by the way. But if you focus on the two equilibrium and pure strategies, you find that both equilibrium are possible. And that's exactly the story that has been followed by a number of international organizations of NGO, is that if you can bring into the limelight a new law that emphasize the rights of the woman to reject this practice that is bad for women, you should shift from here to there. Now, also I would like to bring another consideration, which is a consideration related to the issue of equilibrium selection, is that you can say, OK, but there is a literature that deals with how you can select one equilibrium rather than the other when there are multiple equilibria available that does not rely on the focal point story. And of course, one of the most appealing concepts in that regard is the concept of risk dominance. No risk dominance is essentially saying that when you select a strategy or an action in this case, you're very concerned by the fact of what you can lose if the other party does not coordinate in the way you thought it would coordinate or you would like it to coordinate. So you take into account these losses. Because you are risk averse and there is uncertainty about what the other players are doing. Now, in the case of a 2 times 2 game with symmetric payoff, the way you compute the risk dominant equilibria is quite easy. It has been shown by Hassani and Zelten that in fact, what you have to do is not just compare what you earn under both coordinating equilibria, but also how much you would lose if the others deviate from the coordination equilibria. And you send that with symmetric payoff is very easy. Then what you see here, and that's what I want to draw your attention for, is that the risk dominant equilibrium in fact coincides with the payoff dominant equilibrium. So we don't have the celebrated stack-hand game story that the risk dominant literature has emphasized in which the risk dominant equilibrium does not coincide with the payoff dominant equilibrium. The proofs of that risk dominant equilibrium is often done in evolutionary theory. It's always in fact proven in evolutionary terms. So it's a kind of replicator of dynamics and people need pairwise in a repeated manner and they change the game. And then you see that they really in fact select the risk dominant equilibrium. That phrase, the first question, is to say, OK, if you have got the focal point story, maybe you're right. But if you have an equilibrium selection process through the risk dominant concept, then in fact you should not upset the shared decision story. It should disappear. OK, so that's the first point I want to make. No, coming to the empirics of it, this is a very important point because in fact UNICEF and so many UN agencies and NGO have in fact adopted the focal point story. And one of the biggest NGO that has worked on female genital mutilation in Africa called Tostan, to which I shall return, has explicitly adopted it and referred to the work of a political scientist in the US called MacTee who explicitly referred to the focal point story. So it has been extremely influential as a kind of story behind the way you do social engineering. And in fact, what is interesting in this Tostan thing is that the ultimate aim of the action is what they call public declaration, which means that the village come together, or about 50, 100 villages come together. And the chiefs of the village made a public declaration according to that, abandoning female genital mutilation.  And so that's why they declare that thanks to the effort they have obtained, they've got the vanishing of the practice of female genital mutilation, for which they got the Erick Newton Prize in 2002. They got a first space article in New York Times, et cetera. Now we know how to combat and fight against female genital mutilation is just a matter of having a commitment and coordination device, for instance, in the form of public declaration, that can solve the problem. Now what does the empirical evidence tell us that can help us make our mind of how to act in practice? One study is by Belmar endographers, who essentially say, looking at the cross-country evidence about West Africa, that in fact, most of the variation in the support of female genital mutilation is explained by individual and household level characteristics. So you don't see the social norm approach there. Because social norm approach, what I'm doing is influenced by how many other people, in fact, are doing. So in fact, what they show is the opposite. Where the practice is most supported, then you find that individual level and household level characteristics play even more role. So no support there for the theory. Eferson is a study that has been done in the context of Sudan. And Eferson has several findings. In fact, her starting point is interesting. She said, look, if the story is correct, we should observe that in some villages, the female genital mutilation, as the scholars say, practices disappear. In some other, it is fully implemented. Or maybe this is too extreme. But at least I should observe a discontinuity, a need discontinuity when I observe the prevalence of the practice across several communities. And what she finds is that there absolutely no discontinuity. You know, when you have the continuous, sorry, when you have a distribution of the support of the practice, what you find across communities, of course, what you find is a continuous distribution, which has no discontinuity in it. The second thing is say, OK, but maybe, since usually the argument is that you're too excited about your daughter because otherwise she won't get a proper husband on the marriage market, maybe the marriage pool does not coincide with the community. And what I'm looking at, which is the community, is not the right observation unit. But then she brings evidence that, in fact, marriage are mostly endogenous in those villages. And so there is no real evidence in Sudan that you can argue that the marriage pool does not coincide with the village community that she used as a unit observation. Moreover, when she interviewed the women and the father, the father and the mother, they all said that they don't care about marrying their daughter to a man who is not cut or a boy having a non-cut girl as a wife. OK, lastly, she has a last piece of evidence. She used an implicit association test to try to see what are the values of the people who, in fact, support or do not support cutting practices. And she expected them so to see within a community a clear divided that would reflect the association of a positive value with the cutting practices for those who support it and the opposite for those who do not support and she found nothing. Absolutely no coordination. OK, so you have just a bimodal distribution centered on zero. OK, Vogue and all have a different approach. They just have an experimental approach. They have a treatment in some communities where they dramatize the conflict between cutting and not cutting. So they bring it into the limelight and dramatize the pro and cons. And they measure the treatment outcome by the way of an implicit association test. And what they show essentially is that the treatment has an effect on the way people view female genital mutilation. And essentially, where you dramatize, people start having a more positive view of non-cut girls. The argument and the conclusion that this group of other draw is that, in fact, when you act on individual level perception, you have an effect which doesn't go well with the social norm approach. No, Camiotti has worked precisely on evaluating the impact of Tostan. And she has three main conclusions. First, that unlike what was publicized and said by Tostan itself, the impact of five-year action of Tostan in the villages and communities had practically no impact on female genital mutilation. A slight impact, yes, but absolutely nothing very massive of the difference. Marginal effect. Second conclusion is that if it had an effect, it had also, and most important of it, a perverse effect. This perverse effect is that the age at which the daughters are cut had been lowered compared to what they were before in the direction of Tostan. And she discussed various interpretations of that finding, but we know that from the health point of view of the girls, this is a bad outcome. And third, she saw that the impact and the effect in various ways, she saw that the impact of the public commitment, the so-called public declaration, isn't it? It's yours. So people just didn't take them seriously. That was just a perfunctory, you know, performance achievement of duties. OK, this has been recently confirmed by another study in Novak that essentially showed that there is a lot of heterogeneity in support for excision within communities. So it seems that the story is not credible in terms of the evidence that we have. And so the real question that we can ask analytically is, does that mean that the social norm approach, as it is reflected in this coordination game that I presented, is just wrong? And here, this is where we have to be careful because, in fact, when you look carefully at the evidence of some of this study, you see that, in fact, they insist on preference heterogeneity. That's really what come out of it. And in fact, what is really the limitation of the game that I presented is that we assume that people have homogenous preference. They all dislike social norm. The social norm, in this case, female excision. So now what I want to do with you is let's drop that. Still focus on the expressive function of the law. Following that story, but introducing preference heterogeneity into the analytical apparatus. OK, so when you can do that very easily, you just assume that the utility that the parents, let's say, I, denoted by I, have is a function, the positive component of the utility function, is a function of the proportion of the people who follow the custom. So this is the utility from following the custom for the parent. It is positively influenced by the proportion of the people in the community. That's the social norm approach. And this is the cost element. So you see this data that I was mentioning before is no idiosyncratic. So it's no more a common feature. And these is the amplifying factor. No explicitly brought this capital E variable, which means the environment, political, social, whatever you want, which can, in this context, bring either environment more or less hostile to the practice. You would say that if there is a law that is hostile to the practice, it's included in E. And the greater is E, the greater is the hostility to the practice. That's essentially the idea. No, to make things simple, we just assume that the positive element of the utility is linear, it's mu p. And so the maximum value of that positive element is mu. And when nobody follows the custom, it's, of course, 0. No, theta belongs, of course, to the range 0 to 1. But then what can you show when you follow this? How can you work that out? You define an indifferent agent for which the net benefit of following the custom is 0. And you have a critical value of theta. This is the individual form. It doesn't care between following the practice or not. Then you can compute the proportion of the people who are going to follow the custom. All those who have a theta value below this critical value. They're less aversion to the custom. That's all you should think of. Theta is the coefficient of aversion to the custom. All those people who have an aversion, which is lower than the critical value, will follow the custom. That's what this equation is telling you. And you can compute the critical value theta from the equality that ensures that there's 0 net benefit of following the custom. Now once you have done that, then you can see that there are many Nash equilibria. And the equilibrium with nobody follows the practice is always an equilibrium. And in order to bring some order into that, we restrict the range of possible equilibria. And we do that by a simple rule of dynamics that is rather standard in the literature. We assume that there is a probability that someone enters the pool of the people. And he enters the pool with a given theta, which is theta of a script A. And this theta may be above or below theta star. And we assume that he will follow the custom if the benefit of following the custom, which is a function of the equilibrium value theta star, is greater than the cost for him. And for him, it is V time. It's only just in practice aversion to the custom. Now this means that we are looking for a stability condition and that we want to retain only the stable equilibria. And the stability condition is, in fact, written here. And I will return to it because it pays off. Now let me go straight away to the important lesson that we can draw from this new way of approaching the problem of social norm approach to harmful custom. Now everything will depend on what is the shape of the distribution function of theta. You can feel that. This is where heterogeneity is waving. So this is one possibility in which your density function is, in fact, has a new shape, which means that a lot of people have a lower version to the custom. You could say they are traditionalists. And quite a number of people are averse to it. So they are progressive, if you want to say. And in the middle, we don't have many people. If that is the case, the cumulative distribution function will, in fact, have this shape. So theta is measured along the horizontal axis, along the vertical axis, and measured this curve. In fact, new f theta divided by v, which is the benefit divided by the cost, in fact, which plays a critical role because it is when this is equal to theta that you have the indifferent guide. So theta star is, in fact, corresponding to an intersection point. And now the whole question is, yes, but is that point stable? And the answer is, if you apply the stability condition, yes, it is stable if the curve crosses the identity function of the 45 degree line from above, then goes under. So if you follow that, you see that, in fact, in this case, there is a unique stable nasekelebrille that, in fact, define a positive fraction of people who are going to follow the custom and a positive fraction, the complementary function, which will not do it. And there is no other equilibrium. Now, of course, the immediate consequence, our implication of that is, here, you can bring any focal point story. There's nothing you can do because it's a unique equilibrium. There's no vertical equilibrium. No, assume that you have a situation where people, most of the people, have a kind of medium aversion to the custom. So they are concentrated around medium value of theta. Then you would have a convex function, cumulative distribution function that will cross the 45 degree line from below. Then this intersection point, which is an equilibrium, is not stable. And in fact, in this case, you have two equilibria. Nobody follows the custom or a large number of people follow the custom. And of course, you cannot rule out the possibility that you have a corner solution in the case where this expression is greater than 1. Then in this case, everybody follows the custom. So this is a case where you really have two equilibria. Either everybody follows the custom or nobody follows the custom. Then you could say, oh, then my focal point story will be two. Yes, but depending on this kind of distribution. No, you can have quite a number of other possibilities. Like for instance, this one, in which the benefit of coordination is rather low, then nobody will follow the custom. But you could have a function that is strongly OK, but starting with a very steep kind of slope. And then you will have everybody follows the custom. You can have uniform distribution, in which case the cumulative distribution function will be linear. It can be either above or below the 45 degree line. In one case, everybody follows the custom. In the other one, nobody follows the custom. So what does that show is that we don't know what is going to happen unless we know the distribution of the preferences with respect to this parameter theta, which is the aversion coefficient. OK, now because we are skeptical, in a sense, due to this possibility of having a focal point story, we want to look at another possibility that that is the deterrent effect of the law. So you look, in many cases, the law has a punishment mechanism. It intends to punish deviant behavior. And that is its role. So you cannot just use, in fact, the framework that we have to bring that aspect into the limelight, into focus. Now, what we do first is to say, OK, but then you can determine what the government will do if the government is keen on removing the norm. And then we assume, sorry, something is missing there. Yes, the government is, in fact, having a utility function that has this shape, very simple shape. This is, in fact, a positive component of this utility that is a function of the number of the proportion of people who reject the practice of, let's say, female excision. And the cost of hostility, the cost of having the law enforced, et cetera, we assume is linear. So there is a unit that I see which is constant. And what the government does is to select the value of theta star and the value of the environment, the degree of hostility of the environment, under the participation constraint that I have mentioned earlier that defines, in fact, the critical value of theta star. And when we do that, we find, it's rather easy to show, that the value of theta star is going to be smaller when you increase E. Which means since f theta star varies in the same direction that theta star, you have the intuitive result that, in fact, if the government is becoming more styled to the practice, in fact, the proportion of people implementing it, if you have an interior equilibrium, should be lower. That's a simple idea. The only thing that's important is that in order to get that result, you need to have this expression to be negative. And this is exactly we can show the same condition as the stability condition that was highlighted. So under the stability condition for the participation problem, then we have this rather intuitive result that the government can have a deterrent impact. But it's not the only way to look at the problem of the deterrent effect on the book. You can look at it from a bargaining angle. And here I get inspiration from a web that we did with Zaki, where we define a bargaining solution that has the following form that I quickly explained. So we have two parties. One is the husband, the other is the wife. No, v alpha is the extent to which the custom is being implemented. And I will consider the custom as the amount of inheritance. So to make things clear, I have a certain amount of inheritance to share. And if the custom say that the boy should inherit everything, it means alpha would be 1. And 1 minus alpha, what goes to the girl? In this sense, it will be the boy and the girl would get 0. No, f is what the law prescribes should be the share going to the boy. So here the framework is the following. We have a legislator that has the law. And the law provides an exit opportunity for the women or for the girls in this case. And the idea is that by having this exit opportunity, you enhance the bargaining power of the girl. And so you try to bring the Nash equilibrium that it is close to her preferred choice. So no, what we have, and this is important, and I will return to that here. So you have a bargaining Nash product here in which for the boy, he looks at his utility from getting the custom implemented, compared to what he would get if you would go to the law. In fact, he would appeal to the law. And then he would get f. And f is lower than alpha. By definition, we assume that the state wants to improve the lot of the women. So we are exactly the opposite situation that is the one highlighted by Jocelyn, where the state were in fact legislating against the women, or the case of Pakistan or Saudi Arabia that we were just mentioning before. And C1 is the cost of appealing to the law. And we assume that C1 and C2 can be different. And in fact, we assume that C2 is greater than C1. That it's harder for a girl to go to the court. Why? Because she's by this very act kind of antagonizing community and doing a step that is very hard for her. We can come back on that, and I will. So this is what you get. And you can easily show with appropriate satisfaction of the second order condition that you have this result. But if the state decreases the share by law going to the boy, then the custom is going to adjust to that in the light of the law. So that's a simple idea that is there. Now this threat point, this possibility to appeal of the law, act as a threat point. In fact, the cost of appealing to the law is the threat point. And the way the custom practice is being implemented is going to be influenced by what the law is. So it's because, in fact, you enhance the bargaining power of the women. OK, now if you look at this possibility, so no. Sorry, I want to add one more point. Yeah, so far I have completely left aside a corner solution, but I don't see we should go into too many details. But what I want to add is the following. Is that if you can refine this framework by doing what we have done with Zaki and Gagnal Dashev and Iman Chaharain in a series of other papers, is to say, look, let's complicate the matter a bit and assume that there is a customary authority. In this thing, we don't see there is only a formal judge. Let's bring an informal judge. So to make it look, the woman, if she wants to go to the formal court, first she has to go to the customary court. And she's dissatisfied with the fabric of the customary court. She goes to the formal court. So that's the subject. And we, like in the bargaining framework, but not like in the sort of norm approach, we have underlying the theory we have arrived at Clemens' game. So what you win, I lose. What the girl wins, or the wife wins, the husband wins, which is obvious in the case of inheritance. You have fixed pie, and then you have to decide who is going to get a higher share. So this will be the disadvantage of the other party. Now, of course, we will replicate this kind of expression. But we have one interesting result that you cannot achieve to the bargaining story that is there, which is that you can show that two radical laws may, in fact, harm the women. So the extent to which you make F lower to meet the needs of the women, or to protect the women, may have a backlash effect about a certain point. I think the strongest framework in which you can show that, and in fact it's interesting compared to the discussion of John, is that assume that you don't only have customary authority, which is facing a trade-off between, yes, this is the custom, I like the custom, my preferred outcome is the custom. But if I apply the custom to city, I lose a lot of people who are going to deny my jurisdiction and go to the formal court, which is an insult to the social prestige and power that they have. No, we have a formal judge that is facing a similar trade. Instead of saying the formal judge implements the law automatically and mechanically, we say, no, no, the story is more complicated. The formal judge has also a trade-off. He has also a preferred outcome, what he likes or doesn't like. And in fact, he is kind of balancing his preference for a certain outcome against what the law press is going to do. No, he has a positive utility of applying the law because it is his duty to strictly apply the law. But he doesn't like, or his utility decreases, if the law is going too far away from his own preferred judgment. Then if you assume that judges have idiosyncratic preference, then you have many specials above which, if the law goes too far, they just stop implementing the law and prefer to implement his preferred outcome. Then you can show very simply that in that case, in fact, the expectation of the wee members in this case, of the value of the judgment that they will get, may be decreased by a law that is too radical. So this kind of initiates a whole discussion about, OK, it's good to talk about the law. But how radical do you want the law to be? Is really a question that has been largely ignored. I've seen a recent paper, by the way, by Asemolouet Jackson, who exactly reached the same conclusion, but with a completely different setup because in this setup, you have decentralized matching, pairwise matching of people with the possibility of whistleblowing. So the idea is there is not only the law with a positive, but less than one probability of detection of whether you are actually following the law in your actual behavior, let's say in inheritance matters. But your partner, if you behave too differently from you, may in fact denounce you to the state for punishment. Then you will be put. So what they show is that, again, if the law is too radical, then the extent of the incidence of whistleblowing decline. And in fact, you may have a situation where the interest of the people whom the law wants to defend is, in fact, decreased. No, what about the evidence? OK, so the evidence, in fact, is not an ambiguous or complete. And here, I guess I have to go rather quickly. So let me just mention the fact that most of the evidence is on India, on the impact of the reform of the Hindu succession act. And people try to see whether the better protection embedded into this reform have got an impact on the way inheritance is being shared. There is the first paper by Dininger who is a very optimistic thing. The law has really increased the probability of women to inherit. And even more than that, the education level, this paper has been severely contested by Roir, and I think on firm ground, on, in fact, identification ground, that shows something very interesting. You see, the impact of the law has not been, in fact, to make women more likely to inherit. But as increased, the compensations made to women for the fact that they do not inherit. And this is in the form of Doherty's or in the form of better education, higher levels of education. I like this because it's very much, in fact, connected to the theory called the magnet effect theory that I was illustrating with the customary authority, where F is decreasing, alpha is decreasing, but never to the same extent. So you know, it's a kind of compromise solution that you find. The law is saying something. It is not implemented strictly. But still, it has an impact, and the impact is positive. So that's what the papers say. Then a series of papers were more saying that the law can have perverse effect, like increasing female sign mortality, rosin bloom, increasing barotry at all, female fetishite, and, understand, genical paper, which has been quoted yesterday, so that, in fact, the suicide rates of both husband and wife can be increased as a result or have been increased as a result of the law, which, I argue, is a result of the fact that, indeed, there has been enhanced bargaining power of women, but this enhanced bargaining power has, in fact, created more conflict within the household. And so, for instance, can bring more domestic violence. There are series of papers on Africa. I won't go into the detail. I think I have no time. But essentially, we can conclude, like we do in a paper with a survey paper that I wrote with Katin, that, in fact, the evidence is still very small, that it concerns a very limited number of countries, essentially Kenya, Ethiopia, and India, massively India, no other country, that the identification problem, in their hands, in measuring the impact of the law, are far from being satisfactorily solved in many of those studies, and that the time frame use is excessively short. So in fact, we still don't know very much what is happening. And one of the conclusion is that maybe we need something else than the law to, in fact, change the custom. OK, these are all the more so, because here we have assumed that the law, people know the law. The law is credible. But there is absolutely no evidence of that. Maybe you can improve the knowledge of the law by the people that's feasible. But still, you have two problems. You have the problem that people need to be convinced that the law is considered as credible by the other people, not only by myself. So again, the issue of credibility of the law is a big issue. OK, now, I have to go to the most important thing. And just say that it's very easy to see that one of the way of doing is by changing preferences. If you now make an assumption about the value of E, which you consider as given, so that this function measured along the vertical axis is transformed under the cumulative distribution function, then the reasoning that you make about the equilibrium is unchanged. And you have a departing value of 0, a value of 1 here, which is the assumed value of the cumulative distribution function. And you can, in fact, imagine all kinds of way you change preference and see how the equilibrium is evolving. So you see that if you start from a unique equilibrium where everybody follows the custom and you succeed of changing the preference of the people so that this curve is transformed into the dotted line, then you come to the very, very equilibrium where nobody follows the custom. But in fact, if you look at what it means, you are changing the concave into a convex shape here in the beginning portion, which means that you are working on a people who are very adverse to the law. That's all you're going to say, yeah, that's unrealistic. This is the people who are the most traditionalist. So you better work on the people who are individual. Then you get a marginal increase in the people who reject the practice. But you cannot have all or nothing results. So what we want to say is that if you just follow this kind of framework, you can explain the kind of empirics where you see that you can have a slight improvement, no massive improvement with many distribution functions. This is what you would get. OK, after the last point I want to make here is that the density function can be interpreted in a varying way, which is the following. You can say, yeah, but people do not have all the same influences. Then your density function measure the influence that each individual has. So you continue to have a continuous mass of people where they're varying influence. And the f theta function, and therefore the community function, is translating this important. OK, but still then you know, of course, the result is intuitive. You don't need much elaboration to know that work on the influential people. The whole question is, what do you mean influential people? And here I think the theory of social network is very useful. Because when you say, what do you mean by important people, then you have to see whether do you mean the people with the highest number of directing for the people, or the people with the highest direct plus in directing to other people, or the people who have links with the most important people that are around them. So in the literature of network theory, you may have a big concept of degree centrality, cut centrality, and eigenvector centrality. OK, so the last point, and here, of course, I have to be very short, but it's still an important part of the paper, is the role of the economic environment and economic opportunities. Of course, if you stick to the social norm approach, the impact of increasing economic opportunities, in many cases, may be to in fact increase the hostility to custom, which are going against progress, economic progress, like chasing economic opportunities. Then we know what is the effect, even in the context of social norm. But you can use the bargaining framework or the magnet effect theory framework and then say the exit opportunities are better, so the threat points are increasing, becoming stronger for the victimized party. OK, so that's easy to do. No, no, what does the evidence show? Oh, yeah, that's a game that I will not go into the detail, but which is in the paper, which is a game about the rule, the traditional rule, according to women, do not inherit. And you want to know the watch condition. You can have a shift to an equilibrium in which, in fact, both boys and girls inherit. And let's assume that in the initial situation, women do not inherit, but divorce is very low. So stability of marriage is guaranteed, so she can safely rely on the land of the husband. And now you move that and say, but assume that for a certain reason, the divorce rate is increasing. That security is no more there, so the payoffs are changed. And in fact, the paper details the utility for instance that are behind this payoff structure. But you start with an equilibrium in which parents will select this equilibrium, 55, which in fact is the unique equilibrium in these days. And if you change the payoff of coordinating or sharing the inheritance wealth among boys and girls, you have 77, then you see that now you start having two equilibrium. They can still stick to the old custom, but they can move to the new room. But what is interesting is that in this case, then we have the stuck-hand dilemma. If you compute the risk-dominant strategy, it is different from the payoff-dominant strategy. So the risk-dominant strategy is this one. So what do we want to say about that? That theory doesn't tell you that, as the social norm approach was kind of a hinting, that just a continuous increase in A will have an impact. No, it may have no impact at all. People can stick to the old equilibrium. Because this is a question. In many cases, you see all these customers persisted for such a long time, food binding in China, 1,000 years. And suddenly, it disappeared in two years, very abruptly. And so how do you explain that? You know, we had this kind of situation where it may be the case that the payoff research that people still selected the tradition, even so. You know, the superior, the efficient equilibrium is no more that one. And it may happen, you can have another pay-off structure where the opposite happens, where the unique equilibrium will be coordinating on the new one. In the case of food binding, and there is a whole discussion about that for early pregnancy, early mariage, domestic violence, we have a look at the empirical evidence that shows that in many cases, in fact, the economic incentives and the economic environment changes a lot. To last point, because I've exceeded my time, is that, in fact, you know, it is always the case that there is a resistance that has to do with the power structure of the household. OK, I was looking recently at a paper about the granting of property rights to women in movable assets in the US and in England in the second half of the 19th century, where both countries simultaneously abandoned the coverage law based on the common law to allow women to hold and to inherit movable assets. And it's interesting that in the debate before the English parliament, they were saying that we should oppose this reform of the law because this would reduce women domestic authority and, I quote, create discord in their homes. Expanding the economic autonomy of whites would wantonly interfere with the relations of married life and revolutionize the whole household system. So you see, it's really what we are used to when we look at developing countries today. But the interesting thing is that in spite of those resistance, the new law went through. And why? Because the economics incentives were so strong because it was really a wish to diversify out of agriculture and to its industry that, in fact, the economic proponents of the new law in fact won it. And my second and last remark is the role of culture that is a whole section in the paper that essentially saying this, so far we have talked about social norm, but not really about culture. And this last quote is hinting that culture is very important. And culture, in fact, refers to identity. And a key problem is the following, is that people may have their custom norm, not because they like them or don't like them, because they feel this expression of my identity. It's my culture. And this is very important. We bring evidence from Egypt and we bring evidence from Senegal, where the people use the following kind of words to describe the attempt to criminalize the traditional practice. They say it's cultural aggression. It's a manifestation of Western imperialism, whereby the West want to destroy our culture. If you're touching on those custom, it's our whole culture which is on the thread. And that's what the Westerners want, which shows that even when the government, like the Egyptian government, is enacting a new law that burns female genital systems, which is too immediate. It doesn't mean that it was not done under international pressure, but in fact, it doesn't coincide with the forces of enforcement that John was highlighting. So this expression of cultural identity is important. And in a sense, we discussed a number of references to some paper, even for US marriage and law. That shows that it is important. I think someone pronounced the word masculinity yesterday. This is part of it. You are touching on things that are much deeper than the practice itself. It is a symbol of something else that may be had to do with identity, but also distribution of gender growth. So this is where I end up here. I'm sorry I've written a bit long.


So good morning, everyone. I'm pretty happy to be here, and I'm also happy that there's such a large interest, it seems, in type annotations. Probably some of you already heard of type annotations in Python or maybe even use them, so that's a good thing, because type annotations are actually a pretty old feature already, and I wanted to give this talk because right now there's a lot of activity in this field and as I personally have worked a lot with static analysis and I'm interested in everything that is connected to code quality, I found this an interesting and very good opportunity to actually have a look at type annotations again and see what happened in these ecosystems in the last three and four years, and spoiler alert, it's quite a lot that happened. So I have three learning goals for you in this talk. First, I want to explain to you why actually we need type annotations, or we might need them and how they made their way into the Python language. Secondly, I want to show you how you can actually use those type annotations and type hints in your own code base, and for that we're going to have like an integration project where we take a small code base and actually try to put type hints in there. So then I want to show you also not only how we can use type hints, but how other people are using them in the wild, so in public projects, and to see if actually somebody else is even is doing something with that. So finally, then I want to see, I want to show you what else you can do with type annotations if you're not interested in type hints. Okay, so the first question you ask yourself is probably why do we need type annotations at all? I mean after all our code is just running fine without them, right? So to answer that we will just have a look at how and when we find bugs when developing software. So if we ask ourselves what's the first opportunity to find a bug when we have a new software project, and it's actually not it starts even before we write a code in the design phase, you know, because we could just in a perfect world think about what we want to build, like think about all the edge cases and all the input variables that we might have in our problem, and then with that perfect specification, just write the code and have something that has no bugs in it. And of course this would be nice, but you know this almost never happens. Actually, there are some system that can help you to write systems that to write software that is almost bug-free, but most of these do not apply to many of the daily projects that we are working on. So unfortunately, we have to do something else. The next opportunity to find a bug in our code is after we have written the code. And for example, you could have somebody else look at your code and try to find bugs in there, so called code review. And actually, this is a pretty good way of identifying bugs if you believe the empirical study statistics exists. So if you're not doing this already, you should really start doing it. And as an alternative, instead of having a human read your code, you could also feed it to a computer program. It's a so-called static analyzer and have that static analyzer try to find bugs in your program. And for Python, there are also several solutions available for this that you should use, and it can help you to identify some of the problems that can occur in your code. And, okay, the last one actually doesn't apply to Python, but it applies to other languages. For example, if you have C++ or Rust, you have a compiler that takes your code and puts it into an actual executable form. And while doing so, it also performs a lot of so-called type checking. And using this type checking, it can tell you before you run the program if there's actually some problem in there because there are some types that somehow don't fit together. And as you know, from your Python experience, the Python interpreter, in that sense, is very lenient because it allows you to almost run any code at all, any code you want as long as it corresponds to the Python grammar. And, I mean, you probably all know the situation where you just make a small change in your code and then it just, you run the program and it works fine in the beginning, but at some point it crashes because you might have forgotten to include like an import statement or some of your variables that you're using deep inside your program are undefined. And so this is a situation where like a compiler would really come in very handy because it would catch those bugs already before. Okay, so if you can't rely on that, what can we do to find other bugs in our program? Well, we can build a little exercise yard for our code and test it. And that's what we're doing a lot via unit testing, functional testing and integration testing with the goal of actually running as much as our code as possible and running it through as many different situations as possible in order to identify possible edge cases or things where the code might break. And this is also a best practice, so if you're not doing this already, I'm harshly recommend you to start with that because it can help you to find many bugs in the program. And so the last method that often is not discussed, but which is quite useful to find bugs is, of course, not find them, but have the customer do that. And that's what I call the so-called banana strategy because if you know, if you like shipping bananas, you actually take them off the tree when they're still green and unripe and then you let them ripen at the customer side, so to say. So with software, you can do the same, you just ship unready code and then your customer kind of discovers the bugs for you and then you go ahead and fix them. And again, I'm not saying this is a bad practice or something, it happens a lot, but it's not always what we really want because ideally we want to deliver software to the customer that is working as expected and not have him or her discover the bugs for us. And the Python community also thought that and that's why they decided to do something which is a bit like implementing a compiler for Python by having a system that makes it possible to actually check our programs for internal consistency before we run them. So a type checker for Python if you want. And that's what type hinting is about. Now the problem is, of course, there are already like billions of lines of Python code out in the wild. And you know from the situation we had when we tried to convince people to switch from Python 2 to Python 3 that it's not so easy to get people to rewrite their whole code in a new system. So if you would just introduce like a new version of Python that would require everyone to like statically type all of their code, this would be a very good way to drive people away from Python or have a fork in the Python interpreter maybe. So that's not a strategy that the Python community pursued and instead they did the so-called gradual typing which is also pursued by other languages like TypeScript for example and which lets you instead of having to statically annotate your whole code base, just lets you add type annotations or type hints to like small parts of your code where you think it might be worth having them. If you see here we have some external code for example, in grade would be unannotated code, in green it would be annotated code and then you can write your own code basically and you can also decide for which parts of your code base it actually makes sense to have these type annotations because maybe the code is really critical or maybe you really, really want to be sure that there are no mistakes or no errors in that code. So this is nice but of course now you need a system or way to also interface your own code with the external code that might or might not have type annotations and later we will see how exactly we can do that in Python. All right, so type annotations by themselves are actually not a new feature and so I want to give you a short history of that. The first change that led to this current state was the introduction of so-called function annotations which was done in 2006 so more than 10 years ago and it was formalized in this so-called PEP which is a Python enhancement proposal and that's a formal way to get new things into the Python interpreter or the language in general. And this PEP 3107 introduced a new syntax that would allow you to write these type annotations in your Python code without though giving any meaning to those annotations. So basically what you could put in there, whatever you wanted and the Python interpreter would just ignore that. And this feature landed in the Python 3.0 version in 2008 and after that not much happened for a while. The community just wanted the users to decide what they wanted to do with that feature. I think honestly users didn't do much with it if at all, anything. So in 2014 that's why the community again decided to introduce some new features so-called type hints. This is PEP 484 and this is actually the PEP that gave meaning to the annotations that were introduced in 2006. And there are also some other PEPs here 482 and 483 which explain the theory and the ideas behind the type hints. So if you're interested in that you can check that out by just replacing the number and inserting that into the URL that you find below. So this new feature set landed in Python version 3.5 and allowed us for the first time to really have a formalized system of using type hints in the language. Now you could think that might be the end of the story but actually no. There are still some open and active developments going on here. For example in 2016 there was another addition to the syntax of Python via PEP 526 which introduced a small change and new possibility to also annotate a different type of thing namely a variable because that's something that the writers of the original PEP in 2006 didn't think about but which turned out to be quite useful if you want to do good type checking. So that's something that they implemented just like last year in which landed in the new version of Python 3.6. And there's still some ongoing changes here for example PEP 544 which I find really exciting which is about so-called structural subtyping via protocols but which we are unfortunately not going to talk about in this talk because it goes a bit too far. So as you can see it's still a very active area where new things are created and it's at a state right now where you can actually start using it. So now we probably say okay stop with the theory let's show me the syntax already. So here's an example in all its glory and you might already spot some of the unusual things here are some of the annotations in the code base. So you can see here for example we have a so-called argument annotation which annotates the input value to a function and if you annotate the input value of course you also want to annotate what comes out of the function so the return value and this is done via so-called return type annotation which you see here on the right and this is done by this arrow syntax and the final element is this new so-called variable annotation which was introduced in Python 3.6 which also allows us to annotate a variable inside a function body and you might ask yourself why do we need that? Well it's actually useful because here what we have is that we initialize a result variable with an empty list and for a type checker this would be a problem because it would only know okay this is a list but it wouldn't know what you would actually want to put into that list and so this we can tell to it by saying okay we want to have a list of integer values and that's the main motivation why this feature was added. Alright so if you take a step back now and actually think about what those type hints or type annotations are doing well if you just would run that code with Python 3.6 actually would do not much nothing almost you know. Python interpreter would just take the annotations put them in a magic variable which is called underscore underscore annotations and then just go happily running your program without any run time effects at all so by putting these annotations you do not change the behavior of your Python program per se. Now you also saw maybe that we are importing this weird module there which is called typing and the motivation behind this typing module is that if we have a type checker if we want to implement one in Python we of course need a way to specify what types we actually have and we have some built-in types like integers and floats where you could do that directly in the code base but often we have types which are a bit more complicated so for example dictionaries that map strings to integers or lists of given other types you know and the typing module helps us to specify these types by providing us several classes and several ways to generalize these classes and adapt them to our own need when we are typing type annotating our program. You can see here two examples we have a mapping of a string to an int in the third parameter and we have this list of ints inside the function body. So okay now you're probably asking yourself okay if the Python interpreter doesn't do anything with that annotations then how are they actually getting used and the answer is they're getting used by external tools so there are several of them available for example the most popular one right now is probably MyPy but there are also some IDEs like PyCharm that can use these type hints and these external tools actually do the heavy lifting in type checking so they go through the Python code look for the type annotations and then try to make sense of them and try to find problems in the code and the decision of having that as an external tool and not as part of the core language was a deliberate one and I think it's a good compromise in order to have a system that is easy to evolve but I think eventually a lot of that functionality might also land in the core Python interpreter or at least as a tool that is shipped with Python but right now we have to install an external tool for it and now we're going to look at one of these tools and that's so called MyPy and MyPy was written by Yucca Lehtosalo I've probably horribly mispronounced his name right now who's as far as I know working at Dropbox now and developed many of the ideas for MyPy during his PhD thesis and the good news is that MyPy is strongly pushed by a team at Dropbox under the lead of Guido van Rossem which should give you some confidence in the stability or at least the active development of the tool that's pretty easy to install, you can download it via pip and then just run it on the files which we're going to do in a minute and here on the right side I also included their website which you can see is quite active and they have like several releases, almost one release per month and as you see from the version number the tool is still evolving but it's already functional enough so that you can actually use it Okay, so if we want to use it we need of course an example code base and I chose a project I did for an open source project I did for the German Cyber Security Organization it's called FLOR FLOR is a German translation of the word bloom and you might have heard of bloom filters so what the library is doing it provides such a bloom filter and what does a bloom filter do? Well it's a so called probabilistic data structure to which you can put in some values and then you can later ask the bloom filter have you seen this value or not and if you have put in the value the bloom filter will always remember it and always say yes I have seen that value but what can happen is also that it misremembers something so it might answer yes I have seen it with a value that you actually didn't put in there and this might not seem terribly useful but it's actually quite neat because we at the DTSO we have lots of indicators for example domain names corresponding to domains that like control malware or download malware onto computers and we want to check those domains on a local machine for example so what we can do is we put all those values like 100 million into a bloom filter which is done about depending on the probability that we choose of this misremembering about like 100 megabyte large and we can just copy that to the machine and ask the filter every time have you seen this domain or not so pretty useful library, very small code base and we're going to try now to add annotations to that and see how that goes so I choose a code base as I said because it's really compact it's less than 200 lines of code but it actually does something useful which I always find better than using like a toy example just to show how the stuff works so it's actually something that people use in production and the nice thing about it is also that it doesn't have any external dependencies beyond the standard library so you don't have to type annotate or interface with any external code and again this is good because it makes it easier I don't want to give you the impression though that it's always that easy to annotate code so of course if you have large projects you will need to put in some more effort to do type annotations but in order to get started I think it's a good project to just see how it works in theory but you can also actually do that with a larger code base of course and it doesn't also have many exotic types so most of the types that we're using in there are built-ins or simple compositions of built-ins like lists or dictionaries so it's not too hard to understand what's going on I hope and last thing is that we keep that code base compatible with Python 2 and Python 3 because some of our customers they can't use Python 3 yet so it's also an interesting case to see how type annotations can actually work for a Python 2 code base and in order to test that we need a script of course so we have like an example script here which imports our library it creates an instance of this bloom filter class which we initialize with two values so normally it expects a value n which gives the number of the elements that are in the filter and a probability p which gives the false probability rate so the probability that the filter will misremember a value that we didn't actually put in there and the number of elements should be an integer of course the probability should be a float and you can see here we instead put in a float and an int in the wrong place so this is something that would blow up so to say in runtime and then secondly we want to check or add a value to the filter again we have a problem here because our filter expects an array of bytes and what we put in here is a unicode string so this again would be an error and lastly we want to do something with the fingerprint of a given value that the filter returns and what this function actually returns would be a string of integers but here our user kind of assumed that it would be a string and he tries to or she tries to add another string to that which of course again is a problem and now let's see if we can get mypy to find these problems so in order to add type hints to our code base we would go through the code function by function and then try to add annotations to the arguments and to the return types so if possible or if needed we also might need might add annotations to other variables inside the function bodies or inside the module to help mypy actually make sense of them if they're not used as a part of a function argument or return value and to do that we need to import of course the necessary types from the typing module and then if we have done that we can just run mypy and try to make it happy so to say alright and the result of that is shown here so you can see that we added some type annotations to our init function so we specified that n should be an integer that p should be a float value and we have a function also to read a bloom filter from a file which of course expects something that you can read from and there's also a way to express that using the typing module which is this IO class here and so what we're doing it we're telling the read function that we're expecting an object which is readable and which returns byte that the function can then use to read into the values finally this contains function which checks if a value is in the filter expects of course a byte sequence so byte array and returns a boolean value which we also annotate after doing that we can just run mypy this is shown here so you can see on the left I specify this environment variable which is called mypy path which we need here because mypy actually has its own search path so in this external code we need to tell mypy where it can find that code and in my case I have my python libraries here in the lips folder and then I run mypy I also tell it which python version to use in that case we want to use python 3.4 because we don't use any variable annotations so we don't need 3.6 and it's not running anywhere on my Ubuntu machine and then we tell it a file that we want to check in this case floortest.py and hopefully I will tell you about the mistakes that are in that file but to my surprise what I got out of there instead was this like really weird cryptic error message which really made me doubt if I actually understand anything about typepins at all so it was really like when I do this I said ok Andreas this is easy I got this I'm just going to annotate that stuff and I said what why byte but this should work you know and so I was like digging in the documentation and I tried to do some debugging and finally I found the reason why this is not working and the reason is actually that I broke mypy with that and the problem here was and that I had in the Bloom filter class a variable which is called bytes and which mypy somehow mistakes for the built in bytes type so when I try to initialize that class here mypy thinks ok bytes this is a variable but this is not a valid type so I'm going to show an error but it crashes so as you can see it's still not perfect but on the other hand as I always like the debugging of your system is only as good as your most stupid user and probably I kind of lowered that bar from mypy by naming my variable as just like a built in type but the good news is the guys who are developing mypy they're really awesome so they I posted this issue here on Monday I got a response to that in like 30 minutes and last yesterday morning it was already fixed and merged into the master so as you can see they're really are very active and they really take feedback and bug reports very serious so if you have a bug that you encounter when running mypy I encourage you to just go to the GitHub issue tracker and post it there and they will definitely help you resolve that as fast as possible so having that fixed and quick work around by the way was to just rename variable with an underscore and then it didn't complain anymore so the results of that we can run it on our script again and amazingly as expected it finds all the three problems that we put in here so you can see when we initialize the bloom filter it says oh this is an incompatible type I'm expecting a float here sorry an int here but I get a float there's actually also an arrow for the second message which I don't show then we try to add a value to the filter wrong type and there mypy also complains is that oh here you should put in a byte object but I actually got a string and finally it says oh here you try to add a string to a list of integers and this is also not possible so this is pretty cool I find I mean it might not seem like a big thing but you have to realize that just by adding like a few snippets of additional code to our project we made it really much much harder for our users to use our code the wrong way so we make it much easier to like have a good state in the system and to avoid problems when using that library as a user but also of course make the code better for ourselves you know so normally this could be the end of the talk and we could go to our home but if we just push that to master it wouldn't take long for customer complaints to roll in because as I said some of our customers still rely on Python 2 and since type annotations are not supported by Python 2 I'm adding them to the codebase would just break it so that's bad news but can we do something about that yeah actually we can and because mypy or typins have a second approach for adding these type annotations to codebase which are called type comments so type comment is similar to a type annotation in the sense that we specify function arguments and return types as well as variables but instead of doing that in the code we do it as a comment so if you would again do the same annotation as before of our code only this time using comments the result would look like this so you can maybe I hope you can see them so we have one comment after the init function which starts with this magic word type in the column which tells mypy that this is a type comment and then afterwards it contains the signature of the function so it says okay we return a signature value or float value and we return nothing and you can do that for all of your functions again as before and the nice thing about is you don't have to use the new type annotation syntax what you still have to do though is as you can see above import the relevant types from the typing module which is possible though because the typing module has been back ported to Python 2.7 so we can just install that via pip for example okay now we're fine no because there's also often code that we just can't change maybe because the code is upstream somewhere we don't have like write writes or it's an external library that we want to use so there we're really out of luck both with code with type comments and with inline annotations but luckily again there's a third approach that we can use for scenarios like this which is called a stub file and I've shown you here an example of such stub files so here we have our directory with the code file so we have our init.py the filter.py which contains the code that we saw earlier and some helper functions and what you can see here that we also put some new files in there which are ending with pyi and these pyi are of course the stub files that we're talking about and mypy when going through codebase will also try to find such stub files and it has its own search pass for that so it will look for example in the current directory in the search pass that you give it via this mypy variable environment variable and also in another place which is called a typesheet which is a github repository that contains many type informations for for example built-in Python functions and also third-party libraries so and the thing is if mypy finds a pyi file it will only load the information from there and will completely ignore the corresponding py file and this means instead of changing our code just add this py file pyi file and add the type annotations there again it's the same operation as before we just start with our code and then we go through it but now in addition to adding the annotations we also remove any actual code that is doing stuff you could probably think about this as writing a header file for Python I don't really know if I like that concept or the idea but it's very similar to have a C or C++ header file where we specify the type information then have an actual Python file that implements that interface so to say and the result is looking like this so you have here again our class with the different functions and you can see that we added our type annotations but you can also see that instead of having the function bodies written out we just put this ellipsis there so these three little dots which tells mypy that there's a function body coming afterwards but it's not important so we don't put it there and so this is a very compact, very nice way to specify the typing information for code that we can't actually touch and we can as a set store that in various places and have then mypy look for that information when we try to change to check code that actually uses our library for example so these are three approaches that I showed you and now you might wonder which one is actually the best or how they compare in practice and to get an idea of that we push these three new branches to GitHub and we have a Travis CI integration there which automatically tests our code against the different Python versions and as you can see the results are here so for the if you look at the last elements where we have our stub files in there our code is still compiling or running fine which is not surprising because after all we didn't change any of the Python code we just added some new files that the Python interpreter doesn't care about so for the interpreter those files don't even exist but for the other two cases where we use inline type hints or where we use commented type hints we actually get an error and if we go look at the details of that you can see here the results for the inline and for the commented build for different Python versions so we can see that if you look at the left column the inline build for Python 7 of course fails because there are no type annotations in Python 7 and this 2007 and the same of course for PyPy and PyPy531 here this failure for Python 3.2 and for Py3 is more surprising because you see an exclamation mark there instead of an X which means that something went wrong during the setup of the test and not during the actual running and what went wrong there is that we tried to import the typing module but unfortunately the typing module requires Python 3.3 so Python 3.2 can't import it and neither can PyPy and that's why those two tests are failing and the same is basically the reason why the commented type hints are failing here because also we only have comments in our code but if you remember we still need to import the typing module so we still have the dependency on that so this is why the code is not running on Python 3.2 or PyPy5 actually this is not really true because I found a workaround for this which is maybe a bit ugly but it works so you can if you follow this link here actually also make the commented type hints work in Python 3.2 or PyPy3 alright so so which approach should you use if you look at the inline approach that's kind of of course the right way to use type hints or type annotations are intended to be used and the advantage is that it's easy to read and that the code and the hints are kept in one place the drawback is of course that your code will only be compatible with Python 3.3 or if you use variable annotations Python 3.6 even the alternative here using type comments is good because it keeps the code compatible with Python 2.7 but and it also uses allows to use variable annotations but it might be a bit ugly and it also still requires importing the typing module which there's a workaround for though so the final approach using the stop files is nice because it doesn't modify any of the original source at all and it allows it to use always the latest features that are available in PyPy so the advantage is of course that you have to write now instead of one file two so you will kind of duplicate your maintenance efforts and you will always have to take care to keep those two files in sync with each other which is not always easy so all right of course there's much more to the type hints than this and we just had a look at a very simple example if you want to learn more about that I recommend you to check out the documentation of the typing module which contains information about how you for example can create types for your classes or class hierarchies how you can use so-called generics so think about a list that contains a different element of a given type or like a new structure that you invent and that you use in your code so the typing module provides support for all of these use cases and many more like generators asynchronous things so it's really very well documented and can help you to type most of the variables that you will find in your everyday Python code I would say so probably now you say okay well this is nice but is anybody actually using this already I didn't know that so I checked and what I did for that is to download the top 1,000 repositories, Python repositories from GitHub which you can easily do through the API and then download the code of that and check it for inline annotation, type comments and stops so the code and the analysis can be found on GitHub I just want to show you the results here so what you see here on the left is an image showing you these 1,000 projects that I analyzed so those are really the most popular Python projects on GitHub and the color code represents the number of type pins or type annotations that are found in each project and you can see here on the bottom there's a scale so it goes from zero type pins obviously to about like 10,000 and if you look at the number of projects that actually use type pins you can see that from these 1,000 projects there are 103 projects which have at least one type annotation in their code and so you might say okay this is like a homeopathic dose of type pins so maybe it would be more useful to see the project that actually use many of them and if you go for example if you check the number of projects that have at least 10 type pins in the code base then it's going down to 53 already and if you check for projects that have 100 in them it goes down again to about 24 so you can see that popular projects on GitHub they're already starting to use type annotations some of them are really embracing them but others are just like maybe experimenting with them or like using them in some test cases and in order to see which kind of type pins they're using you can also have a look at the right diagrams here which shows you the usage of inline type pins, type comments and py files and if you're looking for some great examples of type annotations or type pins and Python code bases you can have a look at these repositories so the first one is no surprise of course because it's the MyPy project so I would expect them to use type pins if they're like writing that okay the second one I don't know you might know that if you have like a connection to Hacker School or Recourse Center because I think they use it as their main discussion system it's called Zulib and it's an online community system which was also acquired by Dropbox which might explain why they're using type pins a lot so that's the second project I mean these two you could maybe discount because you could say okay they're working then part of Dropbox and maybe they are forced to use type pins maybe they don't like that even at all but the third project is something that you probably use a lot which is the Sphinx documentation generator and I was actually surprised that they are using type pins but they have a lot of them in their code base about 4,000 and I find they really made very well use of them so if you're a code base with good examples about how to use type pins in a really large project you should have a look at that and afterwards there's still you can see the number of type pins and the project is going down rapidly there are some other things here which is I think this URH URH project is a radio hacking tool you have some home automation tools here a proxy and some AVS AWS related stuff so overall you can see that like of these 1,000 Python repositories that we looked at there are at least 10 that I would say use type pins or type annotations in a serious way alright so I'm almost true the last thing I wanted to show you is that you can actually use type annotations for other things than type printing because if you remember I said the type annotation syntax was introduced in 2006 and it wasn't tied to any actual use case so it was deliberately left open so that users could just use that additional syntax to do stuff with it and now if you look again at a simple example of a function with type annotations or like annotations in this case you can ask yourself what is happening actually when we run that and as I said before what the Python interpreter does is that it like creates a special variable called underscore underscore annotations where it puts everything that you put after the column here or after the the arrow so to say so that makes it really easy to access that information at runtime and it allows you to then build stuff on top of that and as an example again I don't show you much code here if you want to see how it's implemented you can look at the github gist here which is linked below so what I did as a small demo is to build a runtime type checker using this annotations and it works like this you have a function here which in this case takes 3 arguments and we want to make sure that the first argument is an integer which is in the range between 0 and 100 the second one is a float which is between 20 and 40 and the third one is again an in between 17 and 80 which also has a default value and we also say that okay the return value of that function should be a positive float value and this is so called contract because you're specifying not in the body of your function what the values should look like but already in the definitions of the arguments and then you have like a system that enforces that for you so if you call with an argument that does not correspond to the contract that you have the decorator here which is called check would complain about it and this is actually quite easy to build with this type annotation syntax in python and it took less than 100 hours to implement it I didn't know if I should put it here because I'm not sure if it's a good idea because the thing is if you use it in this way you will of course break the type hinting with it because tools like MyPy they expect the type annotations to make sense so if you put something like this here which MyPy doesn't understand it will be a problem so that's why I'm saying if you can use the stuff in that way but you shouldn't you should think about it well okay to summarize I hope I could show you that type hinting in python actually works and makes your code more robust for yourself and for your other people that you can already use it now even if you have python 2 code bases and the annotations can do more than type hinting if you want them to alright so that's basically it you can get the slides on slideshare you can find me on twitter or contact me via email and now I'm happy to take your question thank you thank you so questions thank you for the talk are there any plans in predictable future you are aware of using type annotations for optimizing byte generated by compiler yeah I think there was definitely ideas that are going in this direction I think the PyPy project they considered this but they said they had like other sources of information that they can use to do that optimizations but of course like having these type annotations in the code makes it much easier to do like specializations of your functions so you could use that definitely to make python code run in an optimized way or even have like a virtual machine that is running that is compiling it to something else I mean site is basically doing this by using it kind of different annotation syntax so yeah it's definitely possible more questions so do you suggest also write unit test the test for the correct typing if you remember you showed that the test script first failed and then correctly correctly failed suggest also that we should do this with unit test so the idea of the type checker is of course that you don't need to write any additional code to check your code base so to say you can of course do that but this was mostly an example where I wanted to show how you can use the type checking how users of your library could use the type checking in order to make sure that their code is using your code correctly and you can of course write your own test but it's not really necessary in order to benefit from the type checking information but it's possible Hi you showed how to access the information of annotation and the function is it possible to access the information of annotated variables? Yeah it's possible I don't remember where exactly it starts I think it's also inside like an annotations topic but I really have to look that up and you can also of course get the annotations in the AST which is how I found them in the Python projects on GitHub so I just didn't run the code I just compiled it to an AST using the AST module and then looked for the annotations in the syntax tree so both ways are possible Hi so with the type checking is there support for multiple types so say X is int or float Yeah I didn't show that but the typing module actually has support for many of the things that you would need in a typing system including so called union types so a union type would basically allow you to say okay this variable it could be either a list of integers or a list of floats or maybe something else so you can do that I didn't show that but it's possible Hi there there is also maybe a reason why Sphinx is using so many type annotations you can have a direct benefit also by using an extension for Sphinx that will put your comments like if you use type comments then it will search for them and put them in the documentation Of course that's a good reason thanks for pointing that out Does MyPy also do some sort of type inference So I don't know how MyPy works internally I've written some static analysis tools myself so normally what you do is you try to construct like the control flow graph or convert the code to single ssa form and then do something with that so I'm not actually sure how they do that but there's definitely some type inference involved so what I didn't show you in the code examples was that I can just go back here you see that we have these definitions here of pn and the big n and there we just put like a literal in there and MyPy can of course then figure out that the value in there for example so it can do some of that stuff it doesn't do too much of it but for simple use cases it can actually infer the types Okay, maybe finally a question for the audience who of you is already using type hints Who is using them in production Nice So this question will be asked again next year and we hope this number of hands goes up so thanks again Andreas for this very nice talk