 Okay, welcome back in the Optiver room made possible by our Keystone sponsor. At this time of the summer, the whole world is looking into Japan, and so are we. So I'm really happy to introduce a new speaker that I've not met before. He's a staff for PythConJP and an application developer at CMS.com. Please welcome Yoshi Takai. Hello. Hello. Thank you for introducing me. You're coming in directly from Japan, so we must have a big leg. Yes, sure. Oh, it's perfect. So what's the time where are you? Where you are? It's now 17.30. Oh, okay. So it's like a late talk and an early talk for us. You want to talk about statically typed Python. So please share your slides and the stages yours. Sure. Okay. Hi. Thanks for introducing. Let's start my talk. My talk title is Getting Started with Statically Typed Programming in Python 3.10. I know that it's talk after Mac, but I really enjoy. Okay. Sorry. It's prologue. Let me introduce myself. Before the presentation, my presentation, please note that this slide is uploaded. You can see it via QR code or chat link. And chat room is conference one, optional bar. If you have a combined or a conference ticket, you can see or you can write it. And paper styles in sample codes are some you've not yet to space limitations. Sorry for how to see. Okay. My name is Yoji Takai, but please call me just Peacock. Okay. And Twitter and Facebook. GitHub link is here, here ID. And I'm attending from Japan. Now it's 1730 in Japan standard time. Thank you for considering our time zone. My favorite is music. Most is classical music and skiing gadgets. I'm working at CMS.com full time since 2020 June. We are the only company in Japan that uses pro. So we are pro company. And in addition to my work, I'm also involved at PiconJP association and from JP. A member of ProJP as it's from users group in Japan. We created a short video for introducing from six and just today published. You can see it before this year. I'm operating member of PiconJP association. I'm staff of PiconJP 2020 and 2021. I'm also director of PiconJP TV. It's a YouTube live about Picon's local events, health once a month. Okay. This is today's topic. Next, I will explain why I talk about typing. And second, interaction of typing, how to write basically. It's I want to most, I must want to say topic. And next, Jenix user defined types. It is included best practices. Okay. And first, I will talk about updates, overview and background compatibility for 3.9 or visual. And last is a bit difficult. It's overview of new features on 3.10. Okay. Thanks for YouTube channel comments about comments. Okay. First, I will talk, I will explain my motivation. It's for talking. This is to get a word or in art coherent way. It's been five years since Appear's Python 3.5 at 2015. Since Appear appeared. I think this several big peps were adapted and updated over the years. Even I think many people don't know where to start because there is little coherent information. Okay. These are topics I will not talk about. Such as developing library with typing. Confidence of MyPy. How to use typing in CI. For example, GitHub action, cycle CI and more. And history of typing, implementation of typing and MyPy. And also abstract syntax tree. Okay. Let's take a look how to actually start typing. First, let's look at what typing can you for do? Yes, I think there is three benefits. First, it knows, typing knows the types when you reference it in the editor. And second, typing gets angry when I or you try to give it the wrong one. Last, the computation will work when accessing the return value of function using dot. This is a minimal example. For typing hint, type hint, we don't know the type of return value. Yes. So it is any, if try to pass into the function, it will occur an error. But we don't know this like this. How about this case? It looks like str. Return value is also str. Yes. And the editor can tell the argument is wrong. Yes, like this. Without typing, the editor can't tell. But with typing, can't tell. And more, there are advantages to call review. With a type hint, a reviewer can't know the return value from reading the function definition. Like this, yes, it's brought. As a result, many people may have had these experiences. However, typing may take the review processes more seriously. Yes. We can know that the argument return value type takes three type of value, three type kind. Yes, let me fix it. Okay. Let's start typing with functions definitions. After the argument definitions, write colon and type. After the colon at the end of function definition, write an arrow with hyphens and write a type. That's all. Okay. Well, let's take a look at the types can be used in practices. Okay. There are five types built-ins. Well, bytes, float, int, and str. You don't need to do anything to use them. Yes. No need. No need to import. Also, none is used for functions that return nothing. Yes, that's also nothing import. And if you want to escape from complex type puzzles, you can use any. This is final result. Yes. It can hold instance of any type. Of course, it's better not to use it. Import. If you use it, using it, import is import and use from typing when it is necessary. There is standard collections in GenX. There is also five types standard collections. Dict, frozen set, list, set, and tuple. These collections can be written with square brackets for type inside. It can use 3.9 and later only. And if you use 3.7 or 3.8, please write this statement from the future import annotations. So, of course, I will explain after this. For using 3.6, import annotations starting with uppercase letters from typing module. Until 3.8, it was from typing. Now, it's deplicated. So, it may be recommended for small case, lower case. For standard built-ins, start with lower case without doing anything. Yes, there is no need import statement such as list, tuple. Start with, not start with small case, lower case. For collections, for example, q, default dict, import modules, start with collections. And interval, callable, and protocol-related items, import modules, start with collections.apc. And regular expressions is available from Ali. And collect context-related items are available in context live. Yes, it is important for it's deplicated since 3.9. But until 3.9, you have to write from typing import dict, such as dict, list. Yes, so since 3.9, no more need this import statement. It's a happy. There are many types in collections.abc. Although it's unlikely that you will use these in fine-graded way, it's better to choose a collection with few methods as possible to increase portability. The next figure shows the relationship between collections.abc sequence of built-in types defined by method inclusion rather than implementation inheritances. It is a good idea to look at the method used in your functions to choose the return types of the left side of the diagrams as much as possible. Okay, this is the diagram. The further or the left you go, the fewer methods it has. To the right side of this slide, the more methods it has. For example, if you want to rip over the sequence of augmenting function, you can use collections.abc.itable here. If you need random access, use sequence here. If you need change the value, use type, user type, start with mutable, such as this, this, and this. Or if you simply specify this as the augment type, you will not be able to pass, set, or add. In particular, it is better not to set concrete types, such as this table, just because you are familiar to use them. However, I think it is easier to understand using these concrete types, yes, because you are familiar. And after you confirm, sorry. So you may want to apply first these concrete types. After you confirm that you use fewer operators and methods, you may want to gradually move to the left side of the types. It's a great inheritance tree, method inheritance tree. And it is the difference between tuple and other sequences. I think many personists don't know about this. Tuples are fixed up the length information. Please specify the type of the number of elements. And you can use mixed type, mixed tuple, such as like this. There is three types in tuple. A sequence such as this has the same constraint, same constraint for all elements in the element, can be regardless of the length of length sequence by setting one element. Next, there are few advanced types. At first is union. It's module type. It can be represented by vertical bar since 3.10. Yes, it is 3.10 style. You've probably seen it has scale or types grid. And top half code is a function that accepts both integer or a float. And return types is also int or a float. And bottom code is union object can be tested for equality with other union object. Yes, these brackets are ignored, flattened, and redundant types are removed. This int ignored, removed. And the order is ignored, unless convertible with typing.union. 3.10 style is here. Before 3.9 style is here. As next is optional. It is a generic type for shorthand. It is shorthand equivalent to union with now. Yes, behaves just like union like this. If you use it in function return value or something, it will propagate. But it's big useful, but so be careful how to use it. This is the reason of this. Avoid using optional as much as possible. Optional is useful. Of course, useful. But cause is cause-blot. When you up the function, you might another guard return now. As a result, we need to write a guard to the previous or outside method. Which reduce reliability, but it is not good. In this case, in my opinion, it will be cleaner to raise the runtime error. Yes, because the cost of raising exception in Python is low. Relativity, the performance would be fine. And the lack of the safe method in Python is also a factor. But if there are such methods, they would be opposite. No safe means a method that does not raise exception when passed on that. Okay, the next is callable. It can be used when writing functions that take a function as argument, such as decorator functions. In first API or Flask, you can see it. You've probably seen it. Okay, it's a validation function to passing JSON. Yes, last genix is user defined types. It is typically declared by inheriting from installation. This example is defining genix mapping type. And this is use case. In definition, please note that the key type, this key type is same type. And value type is also same type. This value type. And this class that can be used like this. X is arguments. Type arguments. This, this, this X is also same type. And Y, Y, this Y, and this Y is different type, but Y is also same. Let me, let give me one more promotion about PyConJP 2021. Okay. PyConJP 2020 was held online. This photo, this picture is from the toast of the party. Yes, there are our websites and blogs, Twitter links. The date of conference is October 15 and 16. And we haven't decided what we will do for Spring and training yet. Now, now call for proposal is over. We are currently in the process of review and adoption for talk. These are about the venues. The venue could be both online or hybrid. Yes, if it is onsite venue, it has onsite venue. It is Versal Kanda in Tokyo. Onsite venue starts the afternoon of Friday, 15, 15th. The day one start, start in the afternoon. It, sorry for it may change depending on the COVID-19 situation. And Saturday, day two is all online day. Now, sponsor application and second time is open. For the latest information, please check out blog and Twitter. And please share this page with more Pythonista and you. Okay, thanks. Let's back to my talk. It's an update overview on how to use new features in older versions. Thank you for clapping. It is there recently Python updates in 2021. Yeah, this year, October 4, it will be released Python 3.10. About now, it's beta 4 status. It's a big new future pattern matching. And 3.9, 3.8, 3.7, and 3.6. And let's talk about Danda future, which has come up many times before. Modules and method is Danda underscore, double underscore, to either the pronounced Danda. It exists for backward compatibility. Using typing new features in older versions, right from Danda future import annotations. It describes when dispatcher changes are introduced and become mandatory. And in addition to typing, it was also used to call 3.x features. For example, print functions, unicorn leaders. And last topic is new features in Python 3.10. Yes. It will be released October this year. It is a difficult future. So I'm not sure I can explain it to you there. Okay. It is a come up many times before. It is a union type operator. Using vertical bar or pipe, you can use shorthand for typing.union. You can also, when asking, is instance. Yes. It's very useful. It increases leaderability. And this topic, this feature is very difficult. It's parameter specification variables. Motivation is here. Trying to write a generic decorator, it's difficult to write the type. It needed a way to represent the function that has the same arguments as the specified function. Yes, approach. Adding an argument type called parameter specification solve this problem. It can be used with callable behaves like a j called object. Yes, you can think of an argument version of type bar. It is an example. It is also a decorator function for other logging. Adding log log in log R. So please note that this, you can use p s dot args like this. Keyard args are also available. It is very difficult. Type areas. Motivation is here. We consider global variables without typing. So we type areas. This tends to cause problems with further references, scoping and more. So we are going to make it possible to express the defined type areas. You can still define type areas and impactivity. Approach an example. Adding new typing dot type areas. Write a variable of type areas, type like, like type, like this. Variables define the global level considering type areas. Yes, it works. Using further reference, you can write like this. Using double quote. Example is here. You can, please note that this, you can use user defined class using double quote. For example, such as at static method decorator, you can use it useful. And next is user defined type class. Motivation is here. It is similar to pattern matching. The type checker tools, such as my byte, use technique called type narrowing to determine, type narrowing to determine the type of information. This is an example. The if statement and is none are used to automatically narrow type, narrow down type, the type. Yes. Is none is type guard. Yes. If in this statement, the bar of narrowed to SDR or the S statement, the type of bar is narrowed to none. However, that will not work as in state or if user function is used. Yes. It will occur an error. And type guards allows you to define user defined type guards with new typing. By using user defined type guys, it is easier to get support for type narrowing. So it is example defining like this. Yes. Please note that the type guards. This is but also works like this. This is two element in tuple. Yes. It will box. It's very fine. Happy. The type checker assumes that the first argument matches the specified in type guards in if needs a functional returns to. Yes. Above every example, let that is a pass. Passes is SDR to list will be test related as SDR to SDR. Note that this function returns false type knowing not will be performed. In the next example. If is the two element tuple is this block, the type narrowing to the type narrow to SDR, SDR, SDR, SDR. So it is as a result of type narrowing. Why in the else called block is called block. The type remains unchanged. Yes. It is unchanged. And it is summary of my talk. First, I talked about intellect motivation. Let's starting building types. This is standard collection with that lower case. And the second, their collections and genics. They are union, optional, co-operable and user defined genics. And third topic is update overview and how to use new features in order versions. Last is Python 3.10 styles, new features type meeting. There is four new features. New type, union operator, parameter specification variables, type areas and user defined type cars. And this is thanks for the reference page. Okay. We look forward to seeing you again at our conference. Python JP 2021. Thanks. Thank you very much for the talk. You have seen the chat. You've got a lot of applause in the chat and people commented how well they liked your slides. And they were very interesting. So thank you very much. Yes. We have time for questions. And there is a question. Let me just quickly get that on screen. Sure. Thanks. Create a banner. There we go. The question is how do we use a type hinting of an undefined class? Type hinting, it is undefined class. I not recommend it to all of types defining. I recommend to start with the defining function, definition of function. Is it the answers? Correct. Okay. So we'll just leave this open. And there was one more question. Like if I use type hinting and return multiple values, should I explicitly wrap the returned values in the tuple? Yes. It is passing, not definition of function, definition statement. If in function definitions, you must use tuple. But using unpack variables, you not must do it. Okay. Okay. Thank you very much again. I really liked that you pointed out for which version of Python you can start using these new functions. And it really made sure that type hinting gets a big boost from 3.9. And it will be just great from 3.10. So thank you very much for giving all the detailed information. And let's give you another applause from here to you.