Effective PyCharm (2021 edition) Transcripts
Chapter: The Editor
Lecture: Working with the editor demo: Types
0:00 Things were going. So so well our app is coming along remember on the first part
0:04 I told you how we didn't have to go to the documentation,
0:07 we could just come in here and say 'cmd+p'.
0:10 And it gives us information about what's supposed to go into this section and we get
0:14 all the support. But I feel a little bit let down here.
0:17 For example that is not the things on the episode.
0:22 And even over here it's this episode id.
0:24 But is that like a
0:25 UUID Is that an integer?
0:27 Is it a string? What's that supposed to be?
0:30 So what you'll see is that if we use type annotations type hence in python
0:35 PyCharm will leverage that information and flow it through the rest of our applications.
0:40 So let's go down here and let's say that this is going to return what's it
0:46 going to return? I want to turn an episode.
0:50 Not quite right. We're going to come back to it but let's go over here
0:52 and just do this again. Now this we're not declaring as an episode which we
0:57 could we could do that and then go import this over here but we don't have
1:02 to because this function returns an episode PyCharm is certain that that's an episode.
1:08 So now let's go and hit dot yes look at that title id.
1:11 Date link all the things that it needs to be an episode.
1:18 So that's really fantastic. How is it?
1:21 Not quite right technically we could ask for an episode that doesn't exist in which case
1:27 this would be none. The type system in python says it can't be none unless
1:33 you explicitly say it's none. So the way we do that in python,
1:36 as we say optional from typing of this.
1:40 So that's the more accurate thing.
1:42 But still you'll see the exact same behaviors.
1:46 Okay down here this is going to return an int not really using it but let's
1:54 go out and just be complete here.
1:58 This we go to the episodes like for example if I go episodes zero dot nothing
2:06 let's make that better. This we can say is a dictionary a dict of an int
2:15 and then an episode the key is an integer.
2:18 The thing is an episode. Now if I go to hear and say dot look at that
2:22 Fantastic. I'll say id
2:24 Equals one something like that. If I were good in him put zero it says
2:29 whoa, whoa, whoa you're trying to pass a string even though it's number like
2:35 it's not a number, you're trying to pass a string using the wrong type of
2:38 key. So it's not just that it gives us better.
2:41 Auto complete. It gives us checking and validation as well.
2:45 So this doesn't take a return anything.
2:47 I think we've done our type information and now that we do of course we get
2:51 all kinds of good stuff here we go down to the end and say dot you'll
2:55 see integer integer integer it knows what it is.
2:58 So whenever it makes sense. Whenever you can add this type information to your program
3:04 because it's going to make working with your code much better.
3:07 PyCharm will help you write new code by giving you better auto complete and it
3:11 will help you catch errors by checking that the types match.
3:15 But again, you don't have to write it everywhere through your code.
3:18 Just put it on the sort of service boundary,
3:20 data, access boundary pieces and then often that information flows through like automatically to there
3:25 And to here, I'm a super big fan of using types like this to
3:29 make your editor make PyCharm,
3:31 better think about doing it for yourself.