You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The possibility of using Pyright to build deducers for Python code
Background
Pluto positioning is a tool for static deduce of intent in multilingual application programming, including cloud native, AI applications, etc., without the need for developers to perceive infrastructure such as Docker, Kubernetes, and cloud. Currently, Pluto supports using Typescript to write Pluto applications and deploy them with just one click. We want to extend it to Python, so we need a compiler/analysis/tool/IDE extensions like tsc (The typescript compiler and tools) to help analyze the infra architecture in Python. Pyright is one of the potential candidates.
This article explores the possibility of using Pyright to analyze Pluto Python applications.
Quick Case
import{Router,HttpRequest,HttpResponse}from"@plutolang/pluto";constrouter=newRouter("router");router.get("/hello",async(req: HttpRequest): Promise<HttpResponse>=>{constname=req.query["name"]??"Anonym";constmessage=`${name} access at ${Date.now()}`;return{statusCode: 200,body: `Publish a message: ${message}`,};});
This is the simplest example of a pluto typescript application. Let's first write an example of a pluto Python application.
fromplutoimportRouter, HttpRequest, HttpResponse# Python 3.10+importdatetimeasyncdefhello_handler(request: HttpRequest) ->HttpResponse:
name=request.query.get("name", "Anonym")
message=f"{name} access at {datetime.datetime.now()}"returnHttpResponse(status_code=200, body=f"Publish a message: {message}")
router=Router("router")
router.get("/hello", hello_handler)
The simple code of types Router, HttpRequest of HttpResponse is as follows:
Therefore, what we need to do in Python is basically the same as in Typescript, and its basic principle can be referred to here
Why Pyright
Pyright is written by Typescript and Pluto is written by Typescript.
Pyright directly relies on typescript and references many of them from the implementation of typescript. The structures of the two projects are also similar
Both copyright and typescript contain static analysis capabilities, which is consistent with Pluto's positioning.
The author of Python is currently at Microsoft, and the type systems of Python and TypeScript are borrowing from each other. TSC and Copyright are both maintained by Microsoft, ensuring reliability.
For a personal reason, KCL's progressive type system to some extent references typescript and python, and there is room for improvement in type derivation and checking.
Pyright is more suitable for analysis than mypy and implements simple calculation of literal types.
How to use Pyright to analyze Python code
Pyright is a full-featured, standards-based static type checker for Python. It is designed for high performance and can be used with large Python source bases.
Binder analyzes Scope symbol and other information from a syntax tree
Checker performs type check
Pluto special constructs and methods calling analyze
In pyright, declarations are divided into class and function, and Pluto needs to extend its special class and function declarations based on this, that is, carrying special decorators or implementing a specific interface class or function. This definition is completed in binder, and there is no type information at this time. We need to obtain type information through the analyzer interface function to assist in judgment.
locate all CallNodes in the Pluto Python project using the tree walker function visitCall.
Find the declaration leftExpression in the CallNode.
If it is a ClassDeclaration. We can get the type information of the ClassDeclaration and judge whether it is a special class
Validating the special calling. For example
private_validateIsInstanceCall(node: CallNode){if(node.leftExpression.nodeType!==ParseNodeType.Name||(node.leftExpression.value!=='isinstance'&&node.leftExpression.value!=='issubclass')||node.arguments.length!==2){return;}// Omit the other code}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The possibility of using Pyright to build deducers for Python code
Background
Pluto positioning is a tool for static deduce of intent in multilingual application programming, including cloud native, AI applications, etc., without the need for developers to perceive infrastructure such as Docker, Kubernetes, and cloud. Currently, Pluto supports using Typescript to write Pluto applications and deploy them with just one click. We want to extend it to Python, so we need a compiler/analysis/tool/IDE extensions like
tsc
(The typescript compiler and tools) to help analyze the infra architecture in Python. Pyright is one of the potential candidates.This article explores the possibility of using Pyright to analyze Pluto Python applications.
Quick Case
This is the simplest example of a pluto typescript application. Let's first write an example of a pluto Python application.
The simple code of types
Router
,HttpRequest
ofHttpResponse
is as follows:Therefore, what we need to do in Python is basically the same as in Typescript, and its basic principle can be referred to here
Why Pyright
Why not mypy
See here
Pyright is more suitable for analysis than mypy and implements simple calculation of literal types.
How to use Pyright to analyze Python code
Pyright is a full-featured, standards-based static type checker for Python. It is designed for high performance and can be used with large Python source bases.
The pyright arch is as follows
Pluto special constructs and methods calling analyze
In pyright, declarations are divided into class and function, and Pluto needs to extend its special class and function declarations based on this, that is, carrying special decorators or implementing a specific interface class or function. This definition is completed in
binder
, and there is no type information at this time. We need to obtain type information through the analyzer interface function to assist in judgment.The workflow is
CallNode
s in the Pluto Python project using the tree walker functionvisitCall
.leftExpression
in theCallNode
.ClassDeclaration
. We can get the type information of theClassDeclaration
and judge whether it is a special classThe core interface is
TypeEvaluator
A typical analysis function.
Reference
Pyright
Python type related PEPs
Beta Was this translation helpful? Give feedback.
All reactions