{"id":329486,"date":"2023-09-27T14:00:00","date_gmt":"2023-09-27T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python312-typing\/"},"modified":"2023-09-27T14:00:00","modified_gmt":"2023-09-27T14:00:00","slug":"python-3-12-preview-static-typing-improvements","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/27\/python-3-12-preview-static-typing-improvements\/","title":{"rendered":"Python 3.12 Preview: Static Typing Improvements"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p>Python\u2019s support for static typing gradually improves with each new release of Python. The core features were in place in <a href=\"https:\/\/docs.python.org\/3\/whatsnew\/3.5.html\">Python 3.5<\/a>. Since then, there\u2019ve been many tweaks and improvements to the <strong>type hinting system<\/strong>. This evolution continues in Python 3.12, which, in particular, simplifies the typing of generics.<\/p>\n<p><strong>In this tutorial, you\u2019ll:<\/strong><\/p>\n<ul>\n<li>Use <strong>type variables<\/strong> in Python to annotate generic classes and functions<\/li>\n<li>Explore the <strong>new syntax<\/strong> for type hinting type variables<\/li>\n<li>Model inheritance with the new <strong><code>@override<\/code> decorator<\/strong><\/li>\n<li><strong>Annotate <code>**kwargs<\/code> more precisely<\/strong> with typed dictionaries<\/li>\n<\/ul>\n<p>This won\u2019t be an introduction to using type hints in Python. If you want to review the background that you\u2019ll need for this tutorial, then have a look at <a href=\"https:\/\/realpython.com\/python-type-checking\/\">Python Type Checking<\/a>.<\/p>\n<p>You\u2019ll find many other new features, improvements, and optimizations in Python 3.12. The most relevant ones include the following:<\/p>\n<ul>\n<li><a href=\"https:\/\/realpython.com\/python312-error-messages\/\">Ever better error messages<\/a><\/li>\n<li><a href=\"https:\/\/realpython.com\/python312-perf-profiler\/\">Support for the Linux <code>perf<\/code> profiler<\/a><\/li>\n<li><a href=\"https:\/\/realpython.com\/python312-f-strings\/\">More powerful f-strings<\/a><\/li>\n<li><a href=\"https:\/\/realpython.com\/python312-subinterpreters\/\">Support for subinterpreters<\/a><\/li>\n<\/ul>\n<p>Go ahead and check out <a href=\"https:\/\/docs.python.org\/3.12\/whatsnew\/3.12.html\">what\u2019s new<\/a> in the changelog for more details on these and other features.<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Free Bonus:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/python-312-code\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download your sample code<\/a> for a sneak peek at Python 3.12, coming in October 2023.<\/p>\n<\/div>\n<h2 id=\"recap-type-variable-syntax-before-python-312\">Recap Type Variable Syntax Before Python 3.12<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python312-typing\/#recap-type-variable-syntax-before-python-312\" title=\"Permanent link\"><\/a><\/h2>\n<p>Type variables have been a part of Python\u2019s static typing system since its introduction in Python 3.5. <a href=\"https:\/\/peps.python.org\/pep-0484\/\">PEP 484<\/a> introduced type hints to the language, and type variables and generics play an important role in that document. In this section, you\u2019ll dive into how you\u2019ve used type variables so far. This\u2019ll give you the necessary background to appreciate the new syntax that you\u2019ll learn about later.<\/p>\n<p>A <strong>generic type<\/strong> is a type parametrized by another type. Typical examples include a <a href=\"https:\/\/realpython.com\/python-list\/\">list<\/a> of integers and a tuple consisting of a float, a string, and another float. You use square brackets to parametrize generics in Python. You can write the two examples above as <code>list[int]<\/code> and <code>tuple[float, str, float]<\/code>, respectively.<\/p>\n<p>In addition to using built-in generic types, you can define your own <a href=\"https:\/\/mypy.readthedocs.io\/en\/stable\/generics.html\">generic<\/a> classes. In the following example, you implement a generic <a href=\"https:\/\/realpython.com\/queue-in-python\/\">queue<\/a> based on <a href=\"https:\/\/realpython.com\/python-deque\/\"><code>deque<\/code><\/a> in the standard library:<\/p>\n<div class=\"highlight python\">\n<pre><span><\/span><code><span class=\"c1\"># generic_queue.py<\/span>\n\n<span class=\"kn\">from<\/span> <span class=\"nn\">collections<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">deque<\/span>\n<span class=\"kn\">from<\/span> <span class=\"nn\">typing<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">Generic<\/span><span class=\"p\">,<\/span> <span class=\"n\">TypeVar<\/span>\n\n<span class=\"n\">T<\/span> <span class=\"o\">=<\/span> <span class=\"n\">TypeVar<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"T\"<\/span><span class=\"p\">)<\/span>\n\n<span class=\"k\">class<\/span> <span class=\"nc\">Queue<\/span><span class=\"p\">(<\/span><span class=\"n\">Generic<\/span><span class=\"p\">[<\/span><span class=\"n\">T<\/span><span class=\"p\">]):<\/span>\n    <span class=\"k\">def<\/span> <span class=\"fm\">__init__<\/span><span class=\"p\">(<\/span><span class=\"bp\">self<\/span><span class=\"p\">)<\/span> <span class=\"o\">-&gt;<\/span> <span class=\"kc\">None<\/span><span class=\"p\">:<\/span>\n        <span class=\"bp\">self<\/span><span class=\"o\">.<\/span><span class=\"n\">elements<\/span><span class=\"p\">:<\/span> <span class=\"n\">deque<\/span><span class=\"p\">[<\/span><span class=\"n\">T<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">deque<\/span><span class=\"p\">()<\/span>\n\n    <span class=\"k\">def<\/span> <span class=\"nf\">push<\/span><span class=\"p\">(<\/span><span class=\"bp\">self<\/span><span class=\"p\">,<\/span> <span class=\"n\">element<\/span><span class=\"p\">:<\/span> <span class=\"n\">T<\/span><span class=\"p\">)<\/span> <span class=\"o\">-&gt;<\/span> <span class=\"kc\">None<\/span><span class=\"p\">:<\/span>\n        <span class=\"bp\">self<\/span><span class=\"o\">.<\/span><span class=\"n\">elements<\/span><span class=\"o\">.<\/span><span class=\"n\">append<\/span><span class=\"p\">(<\/span><span class=\"n\">element<\/span><span class=\"p\">)<\/span>\n\n    <span class=\"k\">def<\/span> <span class=\"nf\">pop<\/span><span class=\"p\">(<\/span><span class=\"bp\">self<\/span><span class=\"p\">)<\/span> <span class=\"o\">-&gt;<\/span> <span class=\"n\">T<\/span><span class=\"p\">:<\/span>\n        <span class=\"k\">return<\/span> <span class=\"bp\">self<\/span><span class=\"o\">.<\/span><span class=\"n\">elements<\/span><span class=\"o\">.<\/span><span class=\"n\">popleft<\/span><span class=\"p\">()<\/span>\n<\/code><\/pre>\n<\/div>\n<p>This is a first-in, first-out (<a href=\"https:\/\/realpython.com\/queue-in-python\/#queue-first-in-first-out-fifo\">FIFO<\/a>) queue. It represents the kind of lines that you\u2019ll find yourself in at stores, where the first person into the queue is also the first one to leave the queue. Before looking closer at the code, and in particular at the type hints, play a little with the class:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"kn\">from<\/span> <span class=\"nn\">generic_queue<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">Queue<\/span>\n\n<span class=\"hll\"><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">queue<\/span> <span class=\"o\">=<\/span> <span class=\"n\">Queue<\/span><span class=\"p\">[<\/span><span class=\"nb\">int<\/span><span class=\"p\">]()<\/span>\n<\/span>\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">queue<\/span><span class=\"o\">.<\/span><span class=\"n\">push<\/span><span class=\"p\">(<\/span><span class=\"mi\">3<\/span><span class=\"p\">)<\/span>\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">queue<\/span><span class=\"o\">.<\/span><span class=\"n\">push<\/span><span class=\"p\">(<\/span><span class=\"mi\">12<\/span><span class=\"p\">)<\/span>\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">queue<\/span><span class=\"o\">.<\/span><span class=\"n\">elements<\/span>\n<span class=\"go\">deque([3, 12])<\/span>\n\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">queue<\/span><span class=\"o\">.<\/span><span class=\"n\">pop<\/span><span class=\"p\">()<\/span>\n<span class=\"go\">3<\/span>\n<\/code><\/pre>\n<\/div>\n<p>You can use <code>.push()<\/code> to add elements to the queue and <code>.pop()<\/code> to remove elements from the queue. Note that when you called the <code>Queue()<\/code> <a href=\"https:\/\/realpython.com\/python-class-constructor\/\">constructor<\/a>, you included <code>[int]<\/code>. This isn\u2019t necessary, but it tells the type checker that you expect the queue to only contain integer elements.<\/p>\n<p>Normally, using square brackets like you did in <code>Queue[int]()<\/code> isn\u2019t valid Python syntax. You can use square brackets with <code>Queue<\/code>, however, because you defined <code>Queue<\/code> as a generic class by inheriting from <code>Generic<\/code>. How does the rest of your class use this <code>int<\/code> parameter?<\/p>\n<p>To answer that question, you need to look at <code>T<\/code>, which is a <strong>type variable<\/strong>. A type variable is a special variable that can stand in for any type. However, during type checking, the type of <code>T<\/code> will be fixed.<\/p>\n<p>In your <code>Queue[int]<\/code> example, <code>T<\/code> will be <code>int<\/code> in all annotations on the class. You could also instantiate <code>Queue[str]<\/code>, where <code>T<\/code> would represent <code>str<\/code> everywhere. This would then be a queue with string elements.<\/p>\n<p>If you look back at the source code of <code>Queue<\/code>, then you\u2019ll see that <code>.pop()<\/code> returns an object of type <code>T<\/code>. In your special integer queue, the static type checker will make sure that <code>.pop()<\/code> returns an integer.<\/p>\n<p>Speaking of static type checkers, how do you actually check the types in your code? Type annotations are mostly ignored during runtime. Instead, you need to install a separate type checker and run it explicitly on your code.<\/p>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> In this tutorial, you\u2019ll use <a href=\"https:\/\/microsoft.github.io\/pyright\/\">Pyright<\/a> as your type checker. You can install Pyright from <a href=\"https:\/\/pypi.org\/project\/pyright\/\">PyPI<\/a> using <a href=\"https:\/\/realpython.com\/what-is-pip\/\"><code>pip<\/code><\/a>:<\/p>\n<div class=\"highlight sh\">\n<pre><span><\/span><code><span class=\"gp\">$ <\/span>python<span class=\"w\"> <\/span>-m<span class=\"w\"> <\/span>pip<span class=\"w\"> <\/span>install<span class=\"w\"> <\/span>pyright\n<\/code><\/pre>\n<\/div>\n<p>If you\u2019re using <a href=\"https:\/\/realpython.com\/advanced-visual-studio-code-python\/\">Visual Studio Code<\/a>, then you can use Pyright inside the editor through the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-python.vscode-pylance\">Pylance<\/a> extension. You may need to activate it by setting the <em>Python \u203a Analysis: Type Checking Mode<\/em> option in your settings.<\/p>\n<\/div>\n<p>If you install Pyright, then you can use it to type check your code:<\/p>\n<div class=\"highlight sh\">\n<pre><span><\/span><code><span class=\"gp\">$ <\/span>pyright<span class=\"w\"> <\/span>generic_queue.py\n<span class=\"go\">0 errors, 0 warnings, 0 informations<\/span>\n<\/code><\/pre>\n<\/div>\n<p>To see an example of the kinds of errors that Pyright can detect, add the following lines to your generic queue implementation:<\/p>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python312-typing\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python312-typing\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this tutorial, you&#8217;ll preview the new static typing features in Python 3.12. You&#8217;ll learn about the new syntax for type variables, making generics simpler to define. You&#8217;ll also see how @override lets you model inheritance and how you use typed dictionaries to annotate variable keyword arguments.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python 3.12 Preview: Static Typing Improvements - ITTeacherITFreelance.hk","description":"In this tutorial, you'll preview the new static typing features in Python 3.12. You'll learn about the new syntax for type variables, making generics simpler to"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329486"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329486"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329486\/revisions"}],"predecessor-version":[{"id":329487,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329486\/revisions\/329487"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}