Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
What do you do when you're the only one on your team using type hints? Or when you have to work with Python versions before 3.5? This is where stub files save the day!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So what if you need to work with
Python versions before 3.5 and
0:00
you still wanna use types.
0:02
Or maybe you don't want to stub
your entire library of code,
0:05
you don't wanna hint everything,
just a file or two at a time.
0:07
But you wanna make sure they don't look
weird to the other developers on your team
0:10
or other teams at your company, or
maybe you want to specify types for
0:14
someone else's library, but they're
resisting poor requests for type hints.
0:17
This is where a very handy feature
known as stub files comes in.
0:21
If you're familiar with C# typescript,
or Collin, this may look very similar.
0:25
So to start off we're gonna create
a new file in typing workshop.
0:29
That is called calculator.pyi.
0:35
So this is the stub file.
0:40
Stub files in Python always
have a .pyi extension and
0:42
they have to share the module name
with the module they define types for.
0:45
So calculator.py, calculator.pyi.
0:49
ingredient.py, ingredient.pyi and so on.
0:53
There are ways to change
the location of the PYI files.
0:57
If you check the my pie docs.
1:00
If you need that then
you'll see how to do it.
1:02
I've put a link in the teacher's notes.
1:05
Now in calculator.pyi, I basically
just redefined my functions and types.
1:06
In fact, I'm actually just going
to copy and paste them over.
1:11
So copy all that, paste it into here.
1:13
But a PYI file doesn't actually
need to have any logic or
1:18
anything like that in it.
1:22
So I'm gonna remove
the body of the functions.
1:24
I'm gonna remove move down here
where I called the print method.
1:29
And I'm gonna remove the colons from
the ends of the function definitions.
1:34
Because we don't need those either,
1:39
because we're not actually
defining functions.
1:41
So, all we're saying is,
hey there's a function here named add and
1:44
it takes these two numbers that are reals.
1:48
In fact, let's go ahead and
change this to real.
1:52
There we go, okay.
2:04
Now that I have the stub file defined,
2:06
I can remove all of the type
hinting from calculator.pyi.
2:08
So I don't need this
from numbers import real.
2:12
And I don't need any of this stuff, so
give me a second while I clean it up.
2:16
And so now if you look, you notice
that I still have the warning for
2:33
using the string when it
expected a type real.
2:37
And if you're in py charm you'll
see these little stars here
2:41
that have a stub item and if I click on
those I'm taken directly to the stub.
2:46
Now if you're using Python 2.7 or
2.3 or whatever.
2:50
You'll never know that these type hints
are there, that they're available.
2:54
So you don't have to worry about
confusing other members of your team or
2:58
other people who use a project
with these type hints.
3:02
If you wanna use type hints but
you don't use py charm.
3:06
You can use the my pypackage
to check your types.
3:09
Type hints add a great safety net to
Python code bases, especially large ones.
3:13
I know that every project
no matter how big or
3:18
small needs more documentation
than it actually has.
3:20
While hints won't replace documentation,
they can definitely reduce the number of
3:23
times you or a coworker has to
dive into the source code for
3:27
some other module to see if
a given type will work with it.
3:30
Python is still the friendly, strong,
dynamic doc type language we all love, but
3:33
now it has a handy way to make
sure we're using it correctly.
3:37
Thanks for watching.
3:40
And I'll see you next time.
3:41
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up