{"id":436666,"date":"2024-10-29T03:02:42","date_gmt":"2024-10-29T03:02:42","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=436666"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=436666","title":{"rendered":"<span>SQLAlchemy 2.0 + Python Generic, \u0438\u043b\u0438 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0438, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f, \u0442\u0430\u043a \u0441\u043a\u0430\u0436\u0435\u043c, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/848592\/\" rel=\"noopener noreferrer nofollow\">\u043e\u0431 SQLAlchemy 2.0 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432<\/a>, \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0443\u0437\u043d\u0430\u0435\u043c \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Python Generic \u0438 \u043a\u0430\u043a \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u044f\u0445 \u043f\u0440\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u0441 \u0411\u0414.<\/p>\n<h4>\u041f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430<\/h4>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0451 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c MyPy. \u042d\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u0438\u043f\u043e\u0432 \u0432 \u0432\u0430\u0448\u0435\u0439 IDE, \u0434\u043b\u044f VS Code \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043e\u0442 \u043c\u0430\u0439\u043a\u0440\u043e\u0441\u043e\u0444\u0442.<\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 MyPy \u0432 VS Code<\/h3>\n<p>\u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0438 \u0437\u0430\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b VS Code. \u0422\u0430\u043c \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0443\u043d\u043a\u0442: &#171;Mypy-type-checker: Args&#187;. \u0416\u043c\u0451\u043c \u043a\u043d\u043e\u043f\u043a\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 \u0432\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c: <code>\"mypy-type-checker.args\" = [\"--enable-incomplete-feature=NewGenericSyntax\"]<\/code>. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 Python 3.12.x, <strong>\u043e\u0431\u0440\u0430\u0449\u0430\u044e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/strong>, \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0443 \u0432\u0435\u0440\u0441\u0438\u044e \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u042d\u0442\u0430 \u0436\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u0430 \u043f\u0440\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 MyPy \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 IDE. \u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f<\/p>\n<h4>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Generic?<\/h4>\n<p><strong>Generic, \u0438\u043b\u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438<\/strong> &#8212; \u044d\u0442\u043e \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043b\u044e\u0431\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0442\u0435\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0443\u043a\u0430\u0436\u0435\u043c, \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">from typing import Type  def foo[T](data: T) -&gt; Type[T]: return type(data) # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430  print(foo(5),   foo(\"hello\"),   foo(4.56) ) <\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code>&lt;class 'int'&gt; &lt;class 'str'&gt; &lt;class 'float'&gt;<\/code><\/pre>\n<p><code>[T]<\/code> &#8212; \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043d\u0430\u0448 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 <code>T<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c, \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0442\u0430\u043d\u0435\u0442 \u043b\u044e\u0431\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u0442\u0435 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435.<br \/>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c <code>[T]<\/code> \u043d\u0430 <code>[T: (int, float)]<\/code> \u0438 \u0432\u0430\u0448\u0430 IDE \u043d\u0430\u0447\u043d\u0451\u0442 \u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <code>foo(\"hello\")<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043b\u0438 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u0442\u0438\u043f, \u043e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043a\u043e\u0434, \u0442\u043e \u0432\u0441\u0451 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<pre><code class=\"python\">def foo[T: (int, float)](data: T) -&gt; T: return type(data) # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430  print(foo(5),   foo(\"hello\"), # \u043d\u0435 \u041e\u041a   foo(4.56) ) <\/code><\/pre>\n<p>\u0412\u044b, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u0434\u0443\u043c\u0430\u0435\u0442\u0435: &#171;\u042f \u0436\u0435 \u044d\u0442\u043e \u043c\u043e\u0433 \u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0431\u0435\u0437 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432, \u0434\u0430 \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043e\u043d\u0438 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442, \u0442\u0430\u043a \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043c\u043d\u0435 \u0442\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u044b?&#187;. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e &#8212; \u044d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u043e \u0441\u0442\u0440\u043e\u0433\u0443\u044e \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u0432\u0430\u0448 \u043a\u043e\u0434, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043a\u043e\u0434\u0430(\u043a\u0430\u043a \u0440\u0430\u0437 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e MyPy), \u0441\u0430\u043c\u043e\u043c\u0443 Python \u0431\u0435\u0437 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043d\u0430 \u0432\u0430\u0448\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f, \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044f \u0435\u0441\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044f. \u0418 \u044d\u0442\u043e, \u043e\u0442\u0447\u0430\u0441\u0442\u0438, \u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0432 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0435\u0448\u0438\u0442\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438 \u0434\u043b\u044f \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439\/\u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u043e \u043f\u043e\u043f\u0440\u043e\u0449\u0430\u0439\u0442\u0435\u0439\u0441\u044c \u0441 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0430\u043c\u0438 \u043e\u0442 \u0432\u0430\u0448\u0435\u0439 IDE \u0438 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c\u044e \u043a\u043e\u0434\u0430.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 &#8212; \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"python\">class Stack[T]:     def __init__(self) -&gt; None:         # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0441 \u0442\u0438\u043f\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 T         self.items: list[T] = []      def push(self, item: T) -&gt; None:         self.items.append(item)      def pop(self) -&gt; T:         return self.items.pop()      def empty(self) -&gt; bool:         return not self.items  stack = Stack[int]() stack.push(5) <\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>push()<\/code>, \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u0438\u0434\u0430:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/904\/8a8\/fb8\/9048a8fb84408a1b262b664fb55ec144.png\" width=\"364\" height=\"142\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/904\/8a8\/fb8\/9048a8fb84408a1b262b664fb55ec144.png\"\/><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0442\u0438\u043f\u0430 <code>float<\/code>, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/359\/9a5\/f17\/3599a5f175f1ad27194fb37b2732cabc.png\" width=\"883\" height=\"144\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/359\/9a5\/f17\/3599a5f175f1ad27194fb37b2732cabc.png\"\/><\/figure>\n<p>\u0427\u0442\u043e, \u0432 \u0446\u0435\u043b\u043e\u043c, \u043b\u043e\u0433\u0438\u0447\u043d\u043e. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043c\u044b \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u043b\u0438\u0441\u044c \u043a \u043d\u0430\u0448\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0446\u0435\u043b\u0438 &#8212; \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u0447\u043d\u0451\u043c.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Generic \u0438 SQLALchemy<\/h4>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u044b:<\/p>\n<pre><code class=\"python\">from typing import Type, Sequence  from sqlalchemy import select  from sqlalchemy.orm import Session   from app.repositories.database import engine <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0438\u043c\u043f\u043e\u0440\u0442: <code>Type<\/code> \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e\u0432\u043d\u0443\u0442\u0440\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, <code>Sequence<\/code> &#8212; \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 &#171;\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c&#187;, \u043d\u0443\u0436\u0435\u043d, \u0442.\u043a. \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>all()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0438 \u0442\u0440\u0435\u0442\u0438\u0439 \u0438\u043c\u043f\u043e\u0440\u0442\u044b: \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>select<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0411\u0414, \u043a\u043b\u0430\u0441\u0441 <code>Session<\/code> \u0434\u043b\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0438<\/p>\n<p>\u0427\u0435\u0442\u0432\u0451\u0440\u0442\u044b\u0439 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0411\u0414, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/848592\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> (\u0435\u0441\u043b\u0438 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435 &#8212; \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e, \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u044b \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0411\u0414 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043b\u0438\u0448\u043d\u0435\u0439).<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043b\u0430\u0441\u0441 <code>Repository<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0411\u0414:<\/p>\n<pre><code class=\"python\">class Repository[M]: def __init__(self, model: Type[M]) -&gt; None: self.Model: Type[M] = model <\/code><\/pre>\n<p><code>model: Type[M]<\/code> &#8212; \u0447\u0435\u0440\u0435\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e\u0432\u043d\u0443\u0442\u0440\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f. <code>Type<\/code> \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u0435\u043d, \u0442.\u043a. \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u043a\u043b\u0430\u0441\u0441, \u0430 \u043d\u0435 \u0435\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445: <code>Category<\/code> \u0438 <code>Product<\/code>, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u043b\u044f \u043d\u0438\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438:<\/p>\n<pre><code class=\"python\"># \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u044b from app.models.category import Category from app.models.product import Product  ...  category_repo = Repository[Category](Category) product_repo = Repository[Product](Product) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u044e: \u0432 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 <code>[Category]<\/code> \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u0442.\u0435. \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u0435\u043c\u0443 \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0432 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 <code>(Category)<\/code> \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0411\u0414:<\/p>\n<pre><code class=\"python\">def create(self, new_object: M) -&gt; None: with Session(engine) as session: session.add(new_object) session.commit()<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 <code>new_object: M<\/code>, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u0442\u0438\u043f\u0430, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u044b\u0433\u0440\u0430\u0435\u0442 \u043d\u0430\u043c \u043d\u0430 \u0440\u0443\u043a\u0443? \u0412\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/175\/bec\/972\/175bec972c091cd559e9bde6c7d6ace4.png\" width=\"515\" height=\"106\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/175\/bec\/972\/175bec972c091cd559e9bde6c7d6ace4.png\"\/><\/figure>\n<p>\u0410 \u0432\u0435\u0434\u044c \u043f\u0440\u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 <code>M<\/code>, \u0430 \u043d\u0435 <code>Category<\/code>, \u043d\u043e \u043d\u0430\u0448\u0430 IDE \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438 \u0438 \u0434\u0430\u0451\u0442 \u043d\u0430\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438.<br \/>P.S. \u043a\u0440\u0430\u0441\u043d\u044b\u043c \u043f\u043e\u0434\u0441\u0432\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u0437 \u0411\u0414:<\/p>\n<pre><code class=\"python\">def get_all(self) -&gt; Sequence[M]: with Session(engine) as session: statement = select(self.Model) objects = session.scalars(statement).all() return objects<\/code><\/pre>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>get_all()<\/code>: \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u043d\u0430\u043c\u0438 <code>self.Model<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c\u0438 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u0430\u043a <code>select<\/code>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>get_all()<\/code> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5ff\/699\/897\/5ff6998975ddea7bb58c16c07c5aaeba.png\" width=\"515\" height=\"106\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5ff\/699\/897\/5ff6998975ddea7bb58c16c07c5aaeba.png\"\/><\/figure>\n<p>\u0422.\u0435. IDE \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0451\u0442.<\/p>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">from typing import Type, Sequence  from sqlalchemy import select from sqlalchemy.orm import Session  from app.repositories.database import engine from app.models.category import Category from app.models.product import Product  class Repository[M]: def __init__(self, model: Type[M]) -&gt; None: self.Model = model  def create(self, new_object: M) -&gt; None: with Session(engine) as session: session.add(new_object) session.commit()  def get_all(self) -&gt; Sequence[M]: with Session(engine) as session: statement = select(self.Model) objects = session.scalars(statement).all() return objects  category_repo = Repository[Category](Category) product_repo = Repository[Product](Product)  category_repo.get_all() <\/code><\/pre>\n<p>\u041b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e &#8212; \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0412\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>update()<\/code>, <code>get_by_name()<\/code> \u0438 <code>delete()<\/code>. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u044d\u0442\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0431\u0435\u0437 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432 \u0438\u043c\u0435\u044e\u0442\u0441\u044f \u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/848592\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/a><\/p>\n<p><strong>\u041f\u043e\u0434\u0432\u0435\u0434\u0451\u043c \u0438\u0442\u043e\u0433:<\/strong> \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u043e-\u0441\u0442\u0440\u043e\u0433\u0443\u044e \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043d\u0430\u0441, \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 IDE, \u043d\u043e \u043d\u0435 \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python! \u0427\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u0442 IDE.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u043c, \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043e\u0434 \u043a\u0430\u0436\u0434\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0435\u0441\u043b\u0438 \u0443 \u0412\u0430\u0441 \u0442\u0430\u043a\u043e\u0432\u044b\u0445 \u0448\u0442\u0443\u043a 50&#8230; \u0432\u044b\u0432\u043e\u0434\u044b \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0441\u0430\u043c\u0438 \ud83d\ude42<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/853326\/\"> https:\/\/habr.com\/ru\/articles\/853326\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0438, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f, \u0442\u0430\u043a \u0441\u043a\u0430\u0436\u0435\u043c, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/848592\/\" rel=\"noopener noreferrer nofollow\">\u043e\u0431 SQLAlchemy 2.0 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432<\/a>, \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0443\u0437\u043d\u0430\u0435\u043c \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Python Generic \u0438 \u043a\u0430\u043a \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u044f\u0445 \u043f\u0440\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u0441 \u0411\u0414.<\/p>\n<h4>\u041f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430<\/h4>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0451 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c MyPy. \u042d\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0442\u0438\u043f\u043e\u0432 \u0432 \u0432\u0430\u0448\u0435\u0439 IDE, \u0434\u043b\u044f VS Code \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043e\u0442 \u043c\u0430\u0439\u043a\u0440\u043e\u0441\u043e\u0444\u0442.<\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 MyPy \u0432 VS Code<\/h3>\n<p>\u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0438 \u0437\u0430\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b VS Code. \u0422\u0430\u043c \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0443\u043d\u043a\u0442: &#171;Mypy-type-checker: Args&#187;. \u0416\u043c\u0451\u043c \u043a\u043d\u043e\u043f\u043a\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 \u0432\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c: <code>\"mypy-type-checker.args\" = [\"--enable-incomplete-feature=NewGenericSyntax\"]<\/code>. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 Python 3.12.x, <strong>\u043e\u0431\u0440\u0430\u0449\u0430\u044e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/strong>, \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0443 \u0432\u0435\u0440\u0441\u0438\u044e \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u042d\u0442\u0430 \u0436\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u0430 \u043f\u0440\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 MyPy \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 IDE. \u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f<\/p>\n<h4>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Generic?<\/h4>\n<p><strong>Generic, \u0438\u043b\u0438 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438<\/strong> &#8212; \u044d\u0442\u043e \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043b\u044e\u0431\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0442\u0435\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0443\u043a\u0430\u0436\u0435\u043c, \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h4>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">from typing import Type  def foo[T](data: T) -&gt; Type[T]: return type(data) # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430  print(foo(5),   foo(\"hello\"),   foo(4.56) ) <\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435:<\/p>\n<pre><code>&lt;class 'int'&gt; &lt;class 'str'&gt; &lt;class 'float'&gt;<\/code><\/pre>\n<p><code>[T]<\/code> &#8212; \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043d\u0430\u0448 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 <code>T<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c, \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0442\u0430\u043d\u0435\u0442 \u043b\u044e\u0431\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u0442\u0435 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435.<br \/>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c <code>[T]<\/code> \u043d\u0430 <code>[T: (int, float)]<\/code> \u0438 \u0432\u0430\u0448\u0430 IDE \u043d\u0430\u0447\u043d\u0451\u0442 \u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <code>foo(\"hello\")<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043b\u0438 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u0442\u0438\u043f, \u043e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043a\u043e\u0434, \u0442\u043e \u0432\u0441\u0451 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<pre><code class=\"python\">def foo[T: (int, float)](data: T) -&gt; T: return type(data) # \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430  print(foo(5),   foo(\"hello\"), # \u043d\u0435 \u041e\u041a   foo(4.56) ) <\/code><\/pre>\n<p>\u0412\u044b, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u0434\u0443\u043c\u0430\u0435\u0442\u0435: &#171;\u042f \u0436\u0435 \u044d\u0442\u043e \u043c\u043e\u0433 \u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0431\u0435\u0437 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432, \u0434\u0430 \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043e\u043d\u0438 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442, \u0442\u0430\u043a \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043c\u043d\u0435 \u0442\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u044b?&#187;. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e &#8212; \u044d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u043e \u0441\u0442\u0440\u043e\u0433\u0443\u044e \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u0432\u0430\u0448 \u043a\u043e\u0434, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043a\u043e\u0434\u0430(\u043a\u0430\u043a \u0440\u0430\u0437 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e MyPy), \u0441\u0430\u043c\u043e\u043c\u0443 Python \u0431\u0435\u0437 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043d\u0430 \u0432\u0430\u0448\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f, \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044f \u0435\u0441\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044f. \u0418 \u044d\u0442\u043e, \u043e\u0442\u0447\u0430\u0441\u0442\u0438, \u0443\u0434\u043e\u0431\u043d\u043e, \u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0432 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0435\u0448\u0438\u0442\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438 \u0434\u043b\u044f \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439\/\u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u043e \u043f\u043e\u043f\u0440\u043e\u0449\u0430\u0439\u0442\u0435\u0439\u0441\u044c \u0441 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0430\u043c\u0438 \u043e\u0442 \u0432\u0430\u0448\u0435\u0439 IDE \u0438 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c\u044e \u043a\u043e\u0434\u0430.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 &#8212; \u043e\u0431\u043e\u0431\u0449\u0451\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"python\">class Stack[T]:     def __init__(self) -&gt; None:         # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0441 \u0442\u0438\u043f\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 T         self.items: list[T] = []      def push(self, item: T) -&gt; None:         self.items.append(item)      def pop(self) -&gt; T:         return self.items.pop()      def empty(self) -&gt; bool:         return not self.items  stack = Stack[int]() stack.push(5) <\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>push()<\/code>, \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u0438\u0434\u0430:<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0442\u0438\u043f\u0430 <code>float<\/code>, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0427\u0442\u043e, \u0432 \u0446\u0435\u043b\u043e\u043c, \u043b\u043e\u0433\u0438\u0447\u043d\u043e. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u043c\u044b \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u043b\u0438\u0441\u044c \u043a \u043d\u0430\u0448\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0446\u0435\u043b\u0438 &#8212; \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u0447\u043d\u0451\u043c.<\/p>\n<h4>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Generic \u0438 SQLALchemy<\/h4>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u044b:<\/p>\n<pre><code class=\"python\">from typing import Type, Sequence  from sqlalchemy import select  from sqlalchemy.orm import Session   from app.repositories.database import engine <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0438\u043c\u043f\u043e\u0440\u0442: <code>Type<\/code> \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e\u0432\u043d\u0443\u0442\u0440\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, <code>Sequence<\/code> &#8212; \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 &#171;\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c&#187;, \u043d\u0443\u0436\u0435\u043d, \u0442.\u043a. \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>all()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430.<\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0438 \u0442\u0440\u0435\u0442\u0438\u0439 \u0438\u043c\u043f\u043e\u0440\u0442\u044b: \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>select<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0411\u0414, \u043a\u043b\u0430\u0441\u0441 <code>Session<\/code> \u0434\u043b\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u0435\u0441\u0441\u0438\u0438<\/p>\n<p>\u0427\u0435\u0442\u0432\u0451\u0440\u0442\u044b\u0439 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0411\u0414, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/848592\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> (\u0435\u0441\u043b\u0438 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435 &#8212; \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e, \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u044b \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0411\u0414 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043b\u0438\u0448\u043d\u0435\u0439).<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043b\u0430\u0441\u0441 <code>Repository<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0411\u0414:<\/p>\n<pre><code class=\"python\">class Repository[M]: def __init__(self, model: Type[M]) -&gt; None: self.Model: Type[M] = model <\/code><\/pre>\n<p><code>model: Type[M]<\/code> &#8212; \u0447\u0435\u0440\u0435\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e\u0432\u043d\u0443\u0442\u0440\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f. <code>Type<\/code> \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u0435\u043d, \u0442.\u043a. \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u043a\u043b\u0430\u0441\u0441, \u0430 \u043d\u0435 \u0435\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445: <code>Category<\/code> \u0438 <code>Product<\/code>, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u043b\u044f \u043d\u0438\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438:<\/p>\n<pre><code class=\"python\"># \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u044b from app.models.category import Category from app.models.product import Product  ...  category_repo = Repository[Category](Category) product_repo = Repository[Product](Product) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u044e: \u0432 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 <code>[Category]<\/code> \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u0442.\u0435. \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u0435\u043c\u0443 \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0432 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 <code>(Category)<\/code> \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0411\u0414:<\/p>\n<pre><code class=\"python\">def create(self, new_object: M) -&gt; None: with Session(engine) as session: session.add(new_object) session.commit()<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 <code>new_object: M<\/code>, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u0442\u0438\u043f\u0430, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u044b\u0433\u0440\u0430\u0435\u0442 \u043d\u0430\u043c \u043d\u0430 \u0440\u0443\u043a\u0443? \u0412\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0410 \u0432\u0435\u0434\u044c \u043f\u0440\u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 <code>M<\/code>, \u0430 \u043d\u0435 <code>Category<\/code>, \u043d\u043e \u043d\u0430\u0448\u0430 IDE \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438 \u0438 \u0434\u0430\u0451\u0442 \u043d\u0430\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438.<br \/>P.S. \u043a\u0440\u0430\u0441\u043d\u044b\u043c \u043f\u043e\u0434\u0441\u0432\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043a\u043e\u0431\u043e\u043a.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u0437 \u0411\u0414:<\/p>\n<pre><code class=\"python\">def get_all(self) -&gt; Sequence[M]: with Session(engine) as session: statement = select(self.Model) objects = session.scalars(statement).all() return objects<\/code><\/pre>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>get_all()<\/code>: \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u043d\u0430\u043c\u0438 <code>self.Model<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c\u0438 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u0430\u043a <code>select<\/code>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>get_all()<\/code> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0422.\u0435. IDE \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0451\u0442.<\/p>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">from typing import Type, Sequence  from sqlalchemy import select from sqlalchemy.orm import Session  from app.repositories.database import engine from app.models.category import Category from app.models.product import Product  class Repository[M]: def __init__(self, model: Type[M]) -&gt; None: self.Model = model  def create(self, new_object: M) -&gt; None: with Session(engine) as session: session.add(new_object) session.commit()  def get_all(self) -&gt; Sequence[M]: with Session(engine) as session: statement = select(self.Model) objects = session.scalars(statement).all() return objects  category_repo = Repository[Category](Category) product_repo = Repository[Product](Product)  category_repo.get_all() <\/code><\/pre>\n<p>\u041b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e &#8212; \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0412\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>update()<\/code>, <code>get_by_name()<\/code> \u0438 <code>delete()<\/code>. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u044d\u0442\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0431\u0435\u0437 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432 \u0438\u043c\u0435\u044e\u0442\u0441\u044f \u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/848592\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/a><\/p>\n<p><strong>\u041f\u043e\u0434\u0432\u0435\u0434\u0451\u043c \u0438\u0442\u043e\u0433:<\/strong> \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u043e-\u0441\u0442\u0440\u043e\u0433\u0443\u044e \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043d\u0430\u0441, \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 IDE, \u043d\u043e \u043d\u0435 \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python! \u0427\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u0442 IDE.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u043c, \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043e\u0434 \u043a\u0430\u0436\u0434\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0435\u0441\u043b\u0438 \u0443 \u0412\u0430\u0441 \u0442\u0430\u043a\u043e\u0432\u044b\u0445 \u0448\u0442\u0443\u043a 50&#8230; \u0432\u044b\u0432\u043e\u0434\u044b \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0441\u0430\u043c\u0438 \ud83d\ude42<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/853326\/\"> https:\/\/habr.com\/ru\/articles\/853326\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-436666","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/436666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=436666"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/436666\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=436666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=436666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=436666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}