hello pike online 2021 welcome back to
the education track and it is my
absolute pleasure to introduce shrey
samaya to give a talk uh on python type
hints and how they can supercharge cs
education um this is very exciting i uh
i want to know this and i i want this to
be better and i'm hoping that you stray
will improve the quality my quality of
life through this talk
it's a big ask i know um
but i i i reckon you can do it um now
uh
trey i have i found a fabulous bio that
i did submit but i didn't find it to
begin with um and he said that i could
make up anything about him um so uh i
would like to start his bio by saying uh
that shrey is joining us um following a
wildly successful uh olympic performance
um uh i was just amazed at how how how
true to form um
you showed real courage and
determination i'm i'm proud of you i'm
proud of you stretch for me yeah um
but also shay is a course administrator
and tutor of comp 1511
uh the programming fundamentals course
uh in unsw's school of computer science
um he's been teaching in some form for
most of his life and started tinkering
with python in early high school uh
trey's also currently co-president of
the cse soc unsw which is the resident
computer society of unsw um
which is one of our universities here in
new south wales um
and he's leading a team of about 150
people to create produce social events
podcasts hackathons career events videos
workshops peer mentoring ctfs assorted
technical projects high school outreach
and more
so many things uh but you do so many
fantastic things as part of it so um
it's a fantastic society
um
and otherwise uh shrey apparently you
love cats music reading and video games
and that seems
great and i would say you also
love python ah
we could have had a cat on the stream
yes that's quite sad ah maybe next time
all right well
i'm keen to hear what you think um shrey
will be
accepting questions assuming i haven't
taken up all of the time in this chat um
so throw your questions in the stream
yard
chat
and i will let you get to it
cool awesome thank you so much nikki um
cool so hello everyone as nicki drews my
name is shrey
just a quick note on this talk this talk
is going through quite a lot um as usual
um i want to talk about python type in
so we're going to go a bit introduction
about what is a type what is types
safety what other type systems what are
they useful for um we'll then go into
the specifics of python type hints and
how we can look at doing those and then
we'll jump from there to
using them in education how we can use
type ins to force students to design
better think uh as a software engineer
and actually do a full uh front to back
sort of design of that program so let's
jump straight into it um and i'm hoping
that i stick on time otherwise i'm going
to get yelled at so let's go um
disclaimer i am not a genius i'm not
guido
i do not know everything i only know
some things and i'm going to tell you
about those some things i may be wrong
and please please inform me if i am
yes thank you cool so
safety uh what is safety when we talk
about programming languages and running
our code what is safety um i was reading
a stanford article that talks about
safety as a vague term lacking great
definition and i was like great that's
very useful for my python talk i'm going
to slap that right in there um but for
the purpose of this talk we'll say
something on the lines of safety is
about avoiding harmful mistakes um now
when we run our code when we run our
python code we usually run into sort of
two major problems uh our program
crashes and burns it holds it stops
there's some sort of error and says you
can't do this or this failed or we get
we have like a human error undefined
behavior unexpected behavior something
went wrong but the program still runs
fine um and so if only there was a way
that we could avoid these behaviors as
much as we could uh
psych there is and that's called type
safety so
a lot of type languages
such as c rust etc can attempt to
enforce good behavior including safety
by performing static checks when you run
the code so when you run the code it
scans through and checks all right
you've said this variable is an integer
but you're trying to multiply it by a
string i can't multiply an integer by a
string i'm not going to let you run the
code so it doesn't even get to that
running point so this is what we call a
statically checked language
the process of checking the code is
called type checking
and the algorithm that does the checking
is called the type checker so if you
hear me jumble those like language later
on the talk
that's what we're talking about so
essentially
every value has a type and when you run
the code the operations on the value are
checked per the type now python doesn't
do this python does something different
python's what we call it dynamically
oops i already talked about this yikes
um
python does something different python
does what we call dynamically typelink
it's a dynamically typed language and
we'll go over it in a bit um but in
python we don't have these types right
so why not if it sounds so great if we
can avoid these errors if we can if we
can make our code better why don't we do
it and the answer here is convenience
right imagine if you had to specify for
every single variable what type it was
it would be so slow to write your code
especially when you want to use python
for quick quick tools right so a lot of
the time i'll use python for doing some
quick dynamic or changing formats of
stuff i don't really want to make it
work correctly i just want to work very
quickly i want to write it quickly so
we lose some safety but we get a lot of
convenience um
so python instead instead of having
static typing we have what we call
doctyping um or dynamic typing so what
is doctyping and how is that relevant to
what i'm about to tell you so duck
typing is not this one duck typing is
something like this so python it only
checks if you're allowed to do something
it doesn't check if it's the it doesn't
check it at compile time it checks it
when you're running the code so for
example
let's say you have a class duck
my cursor doesn't show let's say you
have a class duck
this duck has a quack method when you
call the quack method
it will print out quack
there's also in this code a say quack
function which takes in a variable
called duck
so when takes in the variable it'll say
uh
of that instance of a class of that doc
tell it to quack so if we have a new
duck called jeff and we say all right
jeff say quack jeff will say quack cool
this this works fine this is as we
expect um
but
what happens if we have a dog class and
this dog class also has a quack method
uh if we tell the dog to quack it'll
quack and so duck typing essentially is
if it quacks like a duck and it walks
like a duck then it must be a dock so if
it's able to do the thing that you ask
it to do then that's fine that's okay
and that's all we care about um
so you might be wondering oh my gosh why
are we talking about type systems and
type theory i thought i was here a lot
of education and yes as i mentioned
we'll get we'll get there we'll get
there i promise uh assuming i get kicked
out but we'll get there um
so
i was going through some old hsc code
that i wrote so uh in new south wales
there's a system called the hsc where
students will do a series of subjects
towards their final grade um and as part
of the software design hsd subject which
i did in elective you have to write a
massive major work so i was going
through a lot of my code for this talk
to see if there's anything i could use
as an example of code that was
questionable and hard to understand and
i came across this function um this
function is called get body it takes in
a message it checks if the message is
multi-part uh then it returns itself
or a get payload so what is what is
message uh is message some sort of class
is is it a function or a type from
somewhere else like uh what is is
multi-part i've never seen it before at
all um so
looking at my code it's very difficult
to reason what it's doing and how it
works i don't know what messages i don't
know what it's supposed to return there
are so many things here that is very
difficult to understand and extracts the
body from the email
doesn't help at all
until i read this comment i had no idea
this is about emails um so
i wonder what that's looking at
so if i were to call get body of hello
uh i don't really know what will happen
here and so oh no my error message
didn't appear oh no okay well if i did
this
there's a big error saying uh you cannot
run is multipart on hello um so my
python code just stops and breaks
which is not good we want this stuff to
be checked before we run the code um so
the way we can do this is using type ins
um and so we can tell our function
message is of type blah and we can say
please only do this if this is correct
right so if i say message is a string
this will run fine if i say message
should be an integer or a number and i
say get body of hello my program will
error saying hey you've made a mistake
you've passed in a string where i
expected a number um so let's go over
how to do some of that in python
so in python type ins are written as
annotations
our type ins are completely ignored when
you run the program uh which is you
might be thinking well what the heck's
the point then um typings are ignored
for quite a good reason
which is that you shouldn't be forced to
write python code that has types right
you should that shouldn't be mandatory
so there are actual tools out there
where you run you run the tool on your
code
a good example is something called my pi
which is a very common python supported
type checking program
and you run this over your code it will
then look at the annotations and it will
check that you've done the right things
when you just run python 3 in your code
it won't actually check your types you
can do something completely wrong um so
as python we start as annotations and so
the syntax for this looks something
along the lines of this so
for example i have a variable called age
age i've said is an int or an integer or
a number um and that has a value of 20.
so quite standard you say your variable
name the type and then the value
groceries groceries is a dictionary of
uh string keys to int values
and you can see i've initialized it with
apples goes to five and chocolates goes
to three
and so there are a bunch of what we call
basic types here um so integer bull
float string bytes list blah blah blah
blah
none is also a type that we can use um
so a quick aside
um oh that's not the quickest i wanted
uh here back here we have a dict
in earlier versions of python so
previous to 3.9
you actually have to use typing.dick so
typing.capitaldedict and so you often
see sometimes that sometimes people use
capital dedic sometimes people use
lowercase dict it's encouraged that if
you're using above python 3.9 you'll be
using the lowercase d dict because i
think it just looks a lot better like
why would you use typing into
typing.typing.list
when you can just write dict list int
right it's a lot easier to read um so
just want to note that if you do come
across this in the wild and you see a
uppercase d dict
or an uppercase l list
that is presumably using old uh typing
instances so
we want to know that so
if i was to run this code i specified
all right agent int
age is equal to string 20.
this should probably error right if i
run this with a type checker tool like
my pi it says incompatible types you
said age is an int but now you're saying
it's a string and all of a sudden we can
really see the value in this sort of
type checking um if i make a silly
mistake later on the line in my code and
i make so many silly mistakes trust me i
can statically check this before i run
my code before i run my code in a server
or i ship my code out to people i can
check everything is okay and there's no
silly mistakes rather than just running
it or writing a billion tests now tests
are important in their own right but um
this gives you some sort of static
assurance which is a common buzzword
that you'll hear thrown around um cool
so we've talked about variable
annotations let's talk about function
annotations um so function annotations
have a similar sort of syntax where you
have your function name and each of the
arguments each of the arguments are
annotated as variables so for example
our nums variable or nums argument is a
list of integers right this is really
easy to read um your target is also an
integer
now the next part is a bit tricky
traditionally with python functions
you'd write uh we'll go back to our
example over here
over here all you need to do is write
def your function name and in brackets
your parameters followed by a
colon
when you're using typeints
you want to write your function name
your arguments which are all typed
followed by an arrow to the return type
so in this case our two sum function
should uh find which two integers and so
it should return a list of int so this
is the syntax you'll see it returns with
the arrow a list of int followed by the
colon
in the case where your function returns
nothing which is often the case for some
python programs um you can just say it
returns none
um
which is a really useful pattern to have
sometimes people will
won't add the
returns none part which can make type
checkers confused some type checkers
will uh will assume that you mean none
some type because won't um you can also
do parameters with multiple options so
for example you say my argument is a
string or it's an integer or it's none
um now notably this syntax for the the
union or the bar is only supported from
python 3.10 so i did bait you a little
bit there um but you can use what's
called a union uh if you google python
type union uh you can do the same sort
of thing i just like the syntax a lot
better it's a lot easier to read
um cool so
i did some more research on this
function that we're talking back back
about before um it turns out that my
message is an email message um and it
goes through the email and it checks if
it's a if it's a message that has
multiple parts and it will get me a list
of all those parts so
i added some types to this thing um
i have to import email because these
types are their classes so you can also
add
type annotations that are classes so i
can say my message is an email message
email message class so i have to import
email there so i can access to that so
now when i run get body of test it
errors and says all right argument one
to get body is an incompatible type it
has string i expected an email message
um
please try again uh so
this is really really useful now looking
at this code i can clearly see all right
i know what it's expecting and i know
what it's uh what i what i'm gonna get
out of it and so
without having to run the code i can
already do some manipulations on this
right if i was just reading this code i
can say all right well get body returns
a list i want to then have another
function that will process the body of
emails and it will go through each item
in the list i don't have to waste time
trying to figure out what it's returning
or what it's actually giving etc etc so
you save so much time and energy and
cognitive load you just have to read
this sort of one line to understand how
the function works um which is really
super useful so
let's hop back over to the education
part of things um type ins allow us to
design our program first um when i was
talking to some hsc students who were
doing their software major works
i was talking a lot about designing
their program first don't just rush into
things and code it all up design how
your how your program how part of your
program talk to each other and
they would have had a lot of trouble
coming up with how to do that um and so
this is how i thought of this talk
because type ins allow us to do this
they allow us to specify all right my
function a is going to return a message
that looks like this
function b should also expect a message
that looks like this and return a
different type of message and so all you
need to do is write out the function
prototypes and then you can see how your
program interacts so
for example um i was doing a networks
assignment for my university my
university course um
and so i wanted a way to have a user as
a dictionary right because i didn't want
to do classes classes are too confusing
for me sometimes um so i just wanted a
dictionary but i wanted the dictionary
to have a special type i wanted to be
able to say all right this function
should expect a user this function is a
return user and so there's a thing
called a typed dictionary which you can
use
which will allow you to make a uh
essentially a type which is essentially
a dictionary and it's a bit confusing
but essentially
my user dictionary has a password which
is a string
uh in a number of inbound login attempts
which is an integer at the last login
which is a date or time
the last login can also be none and you
can see the same pattern goes further on
so when i want to make a new user all i
have to do is say all right well john
john is of type user remember we have
the columns say this is of type blah so
this is a type user
and this is all the values for when we
first make our new new
user now this is really useful now
because when i'm designing my program i
can say all right well i want a function
that gets all the logged in users for my
program so let me just write out the
prototype for this uh what the function
might look like uh get logged in users
will return a list of users like how
useful is this now we can just say all
right we don't have to think about
anything else we can just go through and
design all the different things that
we're going to need for our program so i
actually went through and i designed a
whole bunch of functions and a whole
bunch of types that i might need later
on
and it was super super useful um and
easier to sort of break down my program
into multiple parts so types allow us to
not only have guaranteed safety but they
allow us to design our program a lot
better and this is why i think typins
and the future of python typing is gonna
be super awesome for teaching we can now
design and teach about python in terms
of design first no longer do we have to
say all right just run your program test
for this test with that while that is a
useful strategy sometimes we can now
also say if we want to oh design out
your code first and then go back and
code it um
so
another awesome part of type hinting is
documentation um for example back on our
email function we can now see if i'll
send this screenshot to someone and say
hey do you know why this isn't working
they can now see all right well message
is supposed to be this and it's supposed
to return this if they wanted to look at
uh all right what is is multipart
they'll just need to google the
documentation for an
email.message.emailmessage
um or they could find it elsewhere in my
code
um and
i've been sent screenshots of code
before where you know students like help
this is broken and i've sent screenshots
of code before saying help this is
broken
and having these type hints allow us to
uh communicate better with our tutors
with our students about what the code is
doing what is it expecting what is it
going to return to us how is it going to
operate
and it also allows some sort of static
guarantees so when your student runs a
code um and say hey my code is broken
you can just say hey have you tried
running your uh type checker yet have
you done something obviously wrong um
which happens all the time uh
so i guess what i want to look up next
is what's what's next we've talked about
where type hinting is up to today um
we've talked about some of the stuff
that's available in 310 with the bar
union operator but what's coming up next
um what's coming up next is a lot of
cool features uh but i realized my
slides are in order and i'm so sorry for
that uh
there are a bunch more python type
hinting uh
shenanigans that i didn't really mention
in today's talk so we are time
constrained um for example generic types
if you want to say this function accepts
a type t and returns a list of type t
you can do something like that you can
create your own types uh you can
pass in functions as types so a callable
type you can say all right this this
function should return anything that's
iterable anything that implements the
iterable uh sort of subclass
and so you can do a whole bunch of
complex things with type bits typings
aren't just the basic stuff that i've
shown today they are very very very
advanced they have a lot of room to grow
because python was not made for this in
mind they were added on in 2015
but they're they're very very advanced
if you want to get into that level so
don't think that you know your hsc
students or the wiz kids can't use this
this is really really good and i think
it's actually encouraged that they
should use this because you get better
documentation and awareness of how the
code runs and how they can design their
code um
here's the what's next slide so
what's next so 310 hasn't been released
yet and it has some really cool typing
features so for example type union we
talk about the type union
there are two also extra things which uh
i'm not really going to explain too much
but essentially recursive types so for
example if you had a type a tree and
that tree has a field in it which is a
tree aren't very well done in python you
have to do a quote around the second
type it's a bit weird if you have a look
at it later you'll probably run into
this problem um but they are adding a
feature to help that be a lot better um
so
a quick disclaimer before we wrap it up
um
my pi and a lot of python
type checkers are still in beta and
they've been in beta for the last couple
of years for last five years because the
language is constantly evolving even as
i was running this talk i was trying to
show off the the lowercase l list syntax
uh when i was running my error my
programs um and the latest version of my
pi actually didn't let me do that i had
to jump onto the uh the nightly branch
to have a look at using that so just be
in mind there there are some things that
are impossible to do with python type
ins
for example a very common restriction is
higher kind of type so if you want a a
type that has a type
and which you probably won't need but if
you do want to do that that is something
you can't do um recursive types can also
be a pain sometimes uh and a bunch of
super complex things for the most part
though python types are really really
really cool
and i'm very excited for the future i'm
very excited for how they can
supercharge our education how we teach
how we talk about design first how we
can improve documentation how we can
improve clarity of our code um and
overall i think they're a really great
thing for the language and i'm very
excited to see where the future goes um
but yeah thank you so much for coming
along everyone i hope you've learned
something today um and i'll be taking
questions for the next few minutes if
that's okay
nikki
hello
i cannot see you thank you so much thank
you sorry the the duck maybe isn't
adding to the uh mystique here um
uh
thank you so much uh we do have a few
questions and there is time
uh for a few more questions to be added
as well um now the first question is
simply a question of a duck
uh with a question mark um how would you
like to respond
uh duck
quack excellent fantastic um great um uh
can you make my pie figure out what
types are instead of finding them
yourself is our next question yes you
definitely can so my pi has some
functions there's a particular function
called reveal type so if you if you
print reveal type of a variable it'll
print out this type this variables of
blah blah blah which is really useful
for big complex code bases of you know a
couple files or even even larger
and you can just get a variable and you
can say all right revealed what this
type is um so yes you definitely can and
a lot of them are built in mind to do
that
fantastic excellent
um uh another question here which
version of python will this work for you
took about 3.9 onwards what about 3.6
so uh lovely chart type painting was
first implemented in three five
three six onwards has support for type
hinting there are some features that
aren't there like for example if you
really want generic types or super
complex stuff 373839 are your are your
best but yes it does exist in three six
and you can do the basic stuff you may
have to do typing dot uppercase d dict
instead of a lowercase d dick but that's
just semantics everything else will work
fine
excellent fantastic
um
here's a question on
uh why would you pipe int
to none
um can none be implied if there's no int
available
uh
so this is a really good question
i propose you a counter question imagine
i pass in a variable to a function um
and for some reason that variable's
turned to none now if i am implying now
that my function of you know string or
int should also take in none the type
checker won't error it will say all
right none is expected but often times
you want it to error you want to say hey
for some reason this is actually turned
to none whereas i'm expecting a string
or an int so you should explicitly
specify this should also accept none if
that's a valid use case because
sometimes things will turn to none and
that's not what's supposed to happen and
you want to yell you an error and say
hey something's gone wrong so
that's a good question um but yes you
should definitely be very explicit about
those things
that is a great answer excellent
um
two more questions
um
uh one of my favorite things about
typing as a dev is it enables ide magic
for navigating around your code
there's more there's more this question
continues it's like an essay it's
fantastic our ide is the editor tooling
types sorry our ids and the editor
tooling types uh
are they able to enable useful
things for teaching or is the magic a
hindrance in understanding how the
system works what are your thoughts so a
lot of editors will
analyze your types as you write the code
and so when you call the function so
oftentimes you write the function and
use it later right so you write the
function you go to call the function and
it will actually hover in your preview
saying this function expects two
arguments the first argument between
integer blur and the second argument
should be of string blah
and so oftentimes it won't do the magic
of checking if you pass in a variable
blah blah so it won't actually hinder
you that sense um i think that editors
can be super super useful when it comes
to hinting because they will enable that
magic feature and so
as to whether it's a it's too magic
that's a question that gets asked
sometimes like i don't want my students
to have this magical pop-over of this is
what it should expect i want to fail and
learn the hard way um which is which is
a valid strategy that's totally okay um
but i think you can disable those if
that's something you're interested in
doing um so i'm not entirely sure if
that answers your question feel free to
ask me more on the hallway channel
afterwards um but yes they're they're
super helpful five years
i i think i have to agree that those
that special magic can be ridiculously
useful when
um helping students
gain that sense of confidence like oh
wow i can actually do this this isn't
just in fact exactly almost ironically
this isn't just magic i can i can i can
use the magic um but then it also
um is handy to to occasionally
um
to feel like uh that
that they understand they really
understand what's going on under the
hood yeah
yeah so
everything's optional which is great
um one last question um unless there's
another one that comes in now uh could
we run my pie as part of the gitlab ci
i am almost certain that is a
possibility i would be very surprised if
gitlab has no way to
enable some static type checking so i'm
going to say 95
yes
and leave that last five percent as an
exercise to the homework for the viewer
google it i'm guaranteed there's
something there yeah
excellent
well those were the questions um
that our fantastic audience have come up
with
um so far and we are
pretty much at time
so i am going to invite you
uh dear viewer and you sray um to join
us in the
hallway
in the education hallway track um now
byo beveridges please
um and we'll have a good old chat about
ducks and type checking and all sorts of
things
before you all rejoin us here for our
next talk
uh by andre roberge uh entitled it
doesn't work
i just love it i'm not sure what it is
but it doesn't work it definitely
doesn't work and uh you'll have to come
back uh in 15 minutes to find out more
but trey thank you so very very very
much um that talk really resonated with
that audience and resonated with me it
resonated with my hundreds of ducks
and
thank you
thank you for having me
you