A New Parser and Type Hints for Generics
00:32 That being said, it also means there are some corner cases. There are some situations where to get the syntax that you have and that you’re used to right now, some hairy stuff has to be done to work around the LL(1) parser. Work recently has been done on changing over the main parser to something called a PEG parser, or a Parsing Expression Grammar. The work’s now complete enough that it’s been included in Python 3.9. Moving to the PEG parser means some of those corner cases that I mentioned before can be worked around more easily. For Python 3.9, both the PEG parser and the LL(1) parser will be built-in. The PEG parser is used by default and constructs the exact same abstract syntax tree.
01:37 The LL(1) will be fully replaced in Python 3.10, so 3.9 is a transition. Make sure your code works, and if you happen to be one of those unlucky sorts who finds a bug in the parser—well, you can still use LL(1) and then report the bug. As of 3.10, once the new parser’s the only parser there, the maintainers can take advantage of this and start to introduce features that would not have been possible with the LL(1).
Recall that a type hint is a way of indicating the type a variable is using the basic types of the system:
float—whatever. That kind of stuff. Up until Python 3.9, you weren’t able to do this with generics, like lists and dictionaries.
The intention of this change is to reduce some confusion. You no longer run into the problem of “What kind of list is this?” and “Why is there a capital-L
List?” and “Why would it work in this situation or that situation?” It should make any code with generics as type hints easier to read.
03:51 One of the features that was put in there was the delayed evaluation of type annotations. What this means is the evaluation doesn’t happen at load time, which means you can get away with doing some things that the parser would otherwise balk at.
This delayed evaluation was put into
__future__ inside of Python 3.7 and 3.8. This means you can take advantage of the small-L
list use of
annotations by importing from
__future__ in versions prior to Python 3.9.
Become a Member to join the conversation.