Daily Dose of Data Science

Share this post

How To Enforce Type Hints in Python?

www.blog.dailydoseofds.com

Discover more from Daily Dose of Data Science

High-quality insights on Data Science and Python, along with best practices — shared daily. Get a 550+ Page Data Science PDF Guide and 450+ Practice Questions Notebook, FREE.
Over 36,000 subscribers
Continue reading
Sign in

How To Enforce Type Hints in Python?

...and make them more powerful.

Avi Chawla
Jun 21, 2023
16
Share this post

How To Enforce Type Hints in Python?

www.blog.dailydoseofds.com
7
Share

When writing Python functions, type hints provide an incredible way to specify explicit information about:

  • the expected types of function arguments, and

  • their return type.

Yet, Python NEVER enforces them.

This means that despite having type hints, a function can still accept (or return) a conflicting type.

And Python will not raise any errors/warnings if there is a type mismatch.

To enforce type hints, use Typeguard.

It provides a decorator for type-checking of Python functions.

As a result, in case of conflicting types, an error is raised.

Enforcing type hints can provide an additional layer of type safety. One can catch type violations that could only be detected at run time.

Get started with Typeguard: Docs.

👉 Over to you: Why Python developers didn’t enforce type hints? Let me know your thoughts :)

Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (350+ pages) with 250+ tips.


👉 Read what others are saying about this post on LinkedIn and Twitter.

👉 Tell the world what makes this newsletter special for you by leaving a review here :)

Review Daily Dose of Data Science

👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.

👉 If you love reading this newsletter, feel free to share it with friends!

Share Daily Dose of Data Science

👉 Sponsor the Daily Dose of Data Science Newsletter. More info here: Sponsorship details.


Find the code for my tips here: GitHub.

I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn and Twitter.

16
Share this post

How To Enforce Type Hints in Python?

www.blog.dailydoseofds.com
7
Share
Previous
Next
7 Comments
Share this discussion

How To Enforce Type Hints in Python?

www.blog.dailydoseofds.com
Prashanth Reddy
Jun 21Liked by Avi Chawla

Hey, been following your content for a while and it’s awesome. Keep going until it reaches the vast community.

Expand full comment
Reply
Share
Graeme Robertson
Writes Graeme’s Substack
Jun 26Liked by Avi Chawla

Could there be a future where Python is made more type safe just like Typescript did for javascript? I think it would be better being built into Python itself as an option, rather than dividing the user base into those who want a "typescript-like" experience and those who prefer classic Python.

Expand full comment
Reply
Share
1 reply by Avi Chawla
5 more comments...
Top
New
Community

No posts

Ready for more?

© 2023 Avi Chawla
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing