1 00:00:00,460 --> 00:00:02,960 So what if you need to work with Python versions before 3.5 and 2 00:00:02,960 --> 00:00:05,210 you still wanna use types. 3 00:00:05,210 --> 00:00:07,951 Or maybe you don't want to stub your entire library of code, 4 00:00:07,951 --> 00:00:10,822 you don't wanna hint everything, just a file or two at a time. 5 00:00:10,822 --> 00:00:14,379 But you wanna make sure they don't look weird to the other developers on your team 6 00:00:14,379 --> 00:00:17,411 or other teams at your company, or maybe you want to specify types for 7 00:00:17,411 --> 00:00:21,510 someone else's library, but they're resisting poor requests for type hints. 8 00:00:21,510 --> 00:00:25,180 This is where a very handy feature known as stub files comes in. 9 00:00:25,180 --> 00:00:29,930 If you're familiar with C# typescript, or Collin, this may look very similar. 10 00:00:29,930 --> 00:00:33,320 So to start off we're gonna create a new file in typing workshop. 11 00:00:35,430 --> 00:00:40,990 That is called calculator.pyi. 12 00:00:40,990 --> 00:00:42,409 So this is the stub file. 13 00:00:42,409 --> 00:00:45,474 Stub files in Python always have a .pyi extension and 14 00:00:45,474 --> 00:00:49,640 they have to share the module name with the module they define types for. 15 00:00:49,640 --> 00:00:53,590 So calculator.py, calculator.pyi. 16 00:00:53,590 --> 00:00:56,610 ingredient.py, ingredient.pyi and so on. 17 00:00:57,770 --> 00:01:00,970 There are ways to change the location of the PYI files. 18 00:01:00,970 --> 00:01:02,590 If you check the my pie docs. 19 00:01:02,590 --> 00:01:05,150 If you need that then you'll see how to do it. 20 00:01:05,150 --> 00:01:06,508 I've put a link in the teacher's notes. 21 00:01:06,508 --> 00:01:11,501 Now in calculator.pyi, I basically just redefined my functions and types. 22 00:01:11,501 --> 00:01:13,650 In fact, I'm actually just going to copy and paste them over. 23 00:01:13,650 --> 00:01:18,090 So copy all that, paste it into here. 24 00:01:18,090 --> 00:01:22,261 But a PYI file doesn't actually need to have any logic or 25 00:01:22,261 --> 00:01:24,830 anything like that in it. 26 00:01:24,830 --> 00:01:26,651 So I'm gonna remove the body of the functions. 27 00:01:29,691 --> 00:01:34,080 I'm gonna remove move down here where I called the print method. 28 00:01:34,080 --> 00:01:38,159 And I'm gonna remove the colons from the ends of the function definitions. 29 00:01:39,880 --> 00:01:41,220 Because we don't need those either, 30 00:01:41,220 --> 00:01:43,230 because we're not actually defining functions. 31 00:01:44,900 --> 00:01:48,130 So, all we're saying is, hey there's a function here named add and 32 00:01:48,130 --> 00:01:50,910 it takes these two numbers that are reals. 33 00:01:52,180 --> 00:01:53,781 In fact, let's go ahead and change this to real. 34 00:02:04,753 --> 00:02:06,473 There we go, okay. 35 00:02:06,473 --> 00:02:08,777 Now that I have the stub file defined, 36 00:02:08,777 --> 00:02:12,410 I can remove all of the type hinting from calculator.pyi. 37 00:02:12,410 --> 00:02:14,880 So I don't need this from numbers import real. 38 00:02:16,100 --> 00:02:19,251 And I don't need any of this stuff, so give me a second while I clean it up. 39 00:02:33,932 --> 00:02:37,869 And so now if you look, you notice that I still have the warning for 40 00:02:37,869 --> 00:02:40,671 using the string when it expected a type real. 41 00:02:41,850 --> 00:02:46,780 And if you're in py charm you'll see these little stars here 42 00:02:46,780 --> 00:02:50,880 that have a stub item and if I click on those I'm taken directly to the stub. 43 00:02:50,880 --> 00:02:54,750 Now if you're using Python 2.7 or 2.3 or whatever. 44 00:02:54,750 --> 00:02:58,420 You'll never know that these type hints are there, that they're available. 45 00:02:58,420 --> 00:03:02,510 So you don't have to worry about confusing other members of your team or 46 00:03:02,510 --> 00:03:05,365 other people who use a project with these type hints. 47 00:03:06,440 --> 00:03:09,330 If you wanna use type hints but you don't use py charm. 48 00:03:09,330 --> 00:03:13,612 You can use the my pypackage to check your types. 49 00:03:13,612 --> 00:03:18,510 Type hints add a great safety net to Python code bases, especially large ones. 50 00:03:18,510 --> 00:03:20,630 I know that every project no matter how big or 51 00:03:20,630 --> 00:03:23,660 small needs more documentation than it actually has. 52 00:03:23,660 --> 00:03:27,260 While hints won't replace documentation, they can definitely reduce the number of 53 00:03:27,260 --> 00:03:30,150 times you or a coworker has to dive into the source code for 54 00:03:30,150 --> 00:03:33,400 some other module to see if a given type will work with it. 55 00:03:33,400 --> 00:03:37,540 Python is still the friendly, strong, dynamic doc type language we all love, but 56 00:03:37,540 --> 00:03:40,770 now it has a handy way to make sure we're using it correctly. 57 00:03:40,770 --> 00:03:41,360 Thanks for watching. 58 00:03:41,360 --> 00:03:42,050 And I'll see you next time.