Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Stub Files

You can also Type Hint in Docstring

It's worth mentioning that you can also add your type hints to the docstring when using PyCharm. That's probably the cleanest way since it doesn't mess with the actual code and improves the documentation at the same time. For example (using NumPy docstring format):

def multiply(a, b=1):
    """
    Parameters
    ----------
    a: int
        A number
    b: {float, int, str}, optional
        Something that can be multiplied by a
    """
    result = a * b
    return result


w = multiply(3, 2)
x = multiply(3, 2.0)
y = multiply(3, 'hello')
z = multiply(3, [1, 2])  # PyCharm will show a warning for this one

print(w, x, y, z)

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

This is awesome! That being said, it's still a good idea to provide type hints in your actual code, too. In future versions of Python, the language itself will warn you or potentially give errors if the static analyzer finds some problems with your types, which will save you a lot of time and effort hunting bugs when that happens