Improving Readability With Aliases
00:00 Improve Readability With Type Aliases Now you’ve seen how to use type hints to specify multiple different types, it’s worth considering best practices for maintainability, and the first concept you’ll look at is type aliasing.
00:14 If you find yourself using the same set of return types across multiple functions, then it can be tedious trying to maintain all of them separately in different places across your code base.
00:24 Instead, you should consider using a type alias. You can assign a set of type hints to an alias and reuse that alias in multiple functions within your code.
00:35 The main benefit of doing this is that if you need to modify the specific set of type hints, then you can do so in a single location and there’s no need to refactor the return types in each function where you use them.
00:47 It’s worth noting that even if you don’t intend to reuse the same type hint across multiple functions, using type aliases can improve the readability of your code.
00:56 You do this by giving your type hint an alias name and then using this alias as a type hint. On screen, you can see an example of how to do this for the function you’ve seen previously.
01:09
Here you define a new EmailComponents
variable as an alias of the type hint indicating the function’s return value, which can be either a tuple containing two strings or None
.
01:19
Then you use the EmailComponents
alias in the function signature.
01:28 Python 3.10 introduced the type alias declaration to make type aliases more explicit and distinct from regular variables. On screen, you can see how to use it.
01:43
You import TypeAlias
from the typing
module, and then you use it as a type hint to annotate the EmailComponents
alias.
01:55
Note that since Python 3.12, you can specify type aliases using the new soft keyword type
. A soft keyword only becomes a keyword when it’s clear from the context. Otherwise, it can mean something else.
02:08
Remember, that type()
is also one of the built-in functions in Python. You can see how you would use it starting from the code where you originally defined EmailComponents
.
02:21
You can specify the type alias name and type hint. The benefit of using type
is that it doesn’t require any imports.
02:30
There are subtle differences in using type
and TypeAlias
, which aren’t drop-in replacements for each other. To learn more about type
and other typing features introduced in Python 3.12, check out this Real Python article.
02:46 Aliasing type hints with descriptive and meaningful names is a straightforward and elegant trick that can improve your code’s readability and maintainability.
02:54 Remember the code that you write will be meaningful to you as you write it, but when you come back to it months later, you’ll be reading it in the same way as someone who’s never seen it before.
03:03 Type aliases can help flatten that learning curve. In the next section of the course, you’ll take a look at how you can use tools for static type checking.
Become a Member to join the conversation.