Without the _future_ import, you’d need to use a string annotation manually since the import isn’t done at runtime. If TYPE_CHECKING : from bad_import_cycle import NameWeNeed def some_function ( arg : list ) -> None. The simplest case of a forward reference is this: Users of forward references, people caring about memory weight of their Python processes, and people who want Python 3.10 syntax in annotations but are still stuck with Python 3.7 in production. Those not only solved the most raised usability issues, but also worked in the runtime contexts mentioned above, making them true adult solutions to the problem. list instead of typing.List), and allowing compact notation for type unions (i.e. It just didn’t have to be executable.Īnd it became unnecessary because in time PEP 585 and PEP 604 implemented the most important quality-of-life improvements to typing syntax: using generics with builtin types (i.e. It needed to be syntactically valid Python. So you never really could, for example, put a question mark to denote an optional type. It was incomplete because from _future_ import annotations unparses code back to a string from an AST. It was naive because we quickly realized that important pieces of typing will always remain in the runtime part of the language: type aliases are essentially runtime assignments, type casting is a function call, using generic types as bases of new classes puts annotation syntax inside a class definition, and so on. The second of the secondary goals, to enable new forms of annotation syntax that’s free from runtime constraints, was both naive, incomplete, and in time became unnecessary anyway. This might be less visible 4 years later but it was a big consideration at the time, especially among large commercial Python users. In any case, it’s important to acknowledge that at the time there was some community pushback against adopting type annotations that was based on their perceived negative performance impact. Note that string interning currently ignores strings with square brackets in them, we could change that to further improve annotation performance. After all, many annotations in a single Python process are alike. For many simple annotations we also get to exploit Python’s string interning to further decrease memory usage. The first of those secondary goals was largely achieved, string annotations are much cheaper memory-wise compared to full-blown Python objects. The secondary goals, not really spelled explicitly in the PEP, were to decrease annotations’ runtime cost, and to allow use of more flexible annotation syntax. The primary goal of PEP 563 was to allow forward references to seamlessly work. AFAICT this functionality is not going away. This is why typing.get_type_hints() was introduced in the first place, long before PEP 563. So, in this sense, string annotations might appear in user code and libraries processing annotations have to deal with them anyway. The idea of a string annotation doesn’t come from PEP 563 though, it comes right from PEP 484 where the forward reference problem was acknowledged. PEP 563 is often criticized for making the contents of _annotations_ strings. First things first: string annotations are valid annotations If we can secure this behavior, I’m +1 to accepting PEP 649. However, in the current situation it’s not that clear because I find it very important to allow writing a single codebase that works on Python 3.7 - 3.11. Books Lifestyle Movies Music Philosophy Programming RPLKTR # PEP 563 and PEP 649Īs the author of PEP 563, I can summarize my position as follows: had PEP 563 never been implemented, it would be easy to accept PEP 649.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |