[Python-Dev] Type hinting: TypeError: 'type' object is not subscriptable

Edward Ned Harvey (python) python at nedharvey.com
Sat Apr 29 08:32:16 EDT 2017


Years ago, I fell in love with python and left C an C++ behind. Sometime later, I fell in love with C# because with strong typing, my IDE does most of the work for me - I never have to browse the API to find which method or attribute exists in a class, I can use tab completion for everything, avoid type-o's, etc. So I was recently thrilled to discover type hinting in python, and support included in pycharm. I'm coming back to python now (for the last few months).

In python 3.6.1, and pycharm CE 2017.1.1 on windows (I haven't tested other versions or other platforms):

The following is recognized and supported by pycharm, but then at runtime, python throws an exception:

                #!/usr/bin/env python3

                def foo(bars: list[str]):
                                for bar in bars:
                                                if bar.startswith("hi"):
                                                                raise ValueError("bar should never say hi")
                                return True


                if foo(["a","b","c"]):
                                print("Ok")


                TypeError: 'type' object is not subscriptable

The problem is that python doesn't recognize list[str] as a type. Pycharm handles it, and everything works fine if I'm using a non-subscripted type, like plain old int, or str, or anything else.

Is this a bug, or is it something python simply hasn't implemented yet, or is it something that's not going to be implemented?

Is there a workaround?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170429/57309ad5/attachment.html>


More information about the Python-Dev mailing list