-
Notifications
You must be signed in to change notification settings - Fork 12
Using Type4Py Rest API
Type4Py's Rest API provides a predict
endpoint, which accepts a Python source file via a POST
request.
A minimal example is given below in Python.
import requests
# Replace `example.py` with your desired Python source file.
with open('example.py') as f:
r = requests.post("https://type4py.com/api/predict?tc=0", f.read())
print(r.json())
The response contains type information in JSON format. See here for the description of the response format. For other programming languages, it should be possible to write a similar code like the above example.
Note 1: The sent Python file should have valid syntax.
Note 2: Above example uses
https://type4py.com
, our public API. Usehttp://localhost:5001
instead if Type4Py's Docker image is running. See here for the usage of Type4Py's Docker images.
A dummy response is shown below to describe the JSON's fields:
{
"error":"None",
"response":{
"classes":[
{
"cls_var_ln":{
"cls_var_name":[[4,4],[4,13]]
},
"cls_var_occur":{
"cls_var_name":[["token","var","name"]]
},
"funcs":[
{
"docstring":{"func":"None","long_descr":"None","ret":"None"},
"fn_lc":[[5,4],[8,28]],
"fn_var_ln":{"var_name":[[7,8],[7,16]],},
"fn_var_occur":{"var_name":[["token","var","name"]]},
"name":"__init__",
"params":{"age":"int",},
"params_descr":{"age":"comment"},
"params_occur":{"age":[["self","age","age"]],},
"params_p":{"age":[["int",0.9999999991180025],["str", 2.3463255785983247e-10]],},
"q_name":"Person.__init__",
"ret_exprs":[""],
"ret_type":"None",
"variables":{"age":""
},
"variables_p":{"age":[["int", 0.2801499039103035]],}
},
{
"docstring":{
"func":"None","long_descr":"None","ret":"None"
},
"fn_lc":[[10,4],[11,24]],
"fn_var_ln":{
},
"fn_var_occur":{
},
"name":"get_name",
"params":{
},
"params_descr":{
},
"params_occur":{
},
"params_p":{
},
"q_name":"Person.get_name",
"ret_exprs":["return self.name"],
"ret_type":"",
"ret_type_p":[["str", 0.7073830464758581]],
"variables":{
},
"variables_p":{
}
}
],
"name":"Person",
"q_name":"Person",
"variables":{"person_id":""},
"variables_p":{
"person_id":[["str", 0.703074717210447],]
}
}
],
"funcs":[
{
"docstring":{"func":"None","long_descr":"None","ret":"None"},
"fn_lc":[[18,0],[25,18]],
"fn_var_ln":{"leave_hours":[[19,4],[19,15]],},
"fn_var_occur":{"leave_hours":[["no_hours","leave_hours"]],},
"name":"work",
"params":{"no_hours":""},
"params_descr":{"no_hours":""},
"params_occur":{"no_hours":[["no_hours","leave_hours"]],},
"params_p":{"no_hours":[["Type", 0.0999]]},
"q_name":"work",
"ret_exprs":["return \"Done!\""],
"ret_type":"",
"ret_type_p":[["str", 0.287441260068372]],
"variables":{"leave_hours":""},
"variables_p":{"leave_hours":[["int",0.2]]}
}
],
"imports":["os"],
"mod_var_ln":{"A_GLOBAL_VAR":[[1,0],[1,12]]},
"mod_var_occur":{"A_GLOBAL_VAR":["token"]},
"no_types_annot":{"D":1,"I":0,"U":14},
"session_id":"a0bvkdCC8utA35r8JrOho07FrDpV9qaLr2lccFzoXB4",
"set":"None",
"tc":[false,"None"],
"type_annot_cove":0.07,
"typed_seq":"",
"untyped_seq":"",
"variables":{"A_GLOBAL_VAR":""},
"variables_p":{"A_GLOBAL_VAR":[["str",0.41389838221497904]]}
}
}
Note: Predicted types by Type4Py are stored in the fields
variables_p
,params_p
,ret_type_p
.
-
error
: An error message if the process of a request is unsuccessful. -
response
: Contains a processed Python file with type information-
classes
: A JSON array of processed classes-
cls_var_ln
: Contains line no. info for class variables. -
cls_var_occur
: Occurences of class variables in the class itself (tokenized) -
funcs
: Contains JSON array of class-level functions. Its structure is described below. -
name
: Name of the class -
q_name
: Fully qualified name of the class -
variable
: List of class variables and their type annotation (if available) -
variables_p
: List of predicted types for the class variables
-
-
funcs
: Contains JSON array of processed module-level functions-
docstring
: Contains comments about what the function does (if available) -
fn_lc
: Line numbers for the start and end of the function -
fn_var_ln
: Contains line numbers for the start and end of local variables in the function -
fn_var_occur
: Occurences of local variables in the function itself (tokenized) -
name
: Name of the function -
params
: List of function's parameters with their type annotation (if available) -
params_descr
: Comments about function's parameters (if available) -
params_occur
: Occurences of parameters in the function itself (tokenized) -
params_p
: List of predicted types for the function's parameters. Each parameter's predicted types are sorted by the model's confidence. -
q_name
: Fully qualified name for the function -
ret_exprs
: Return expressions in the function (tokenized) -
ret_type
: The return type of the function (if available) -
ret_type_p
: List of predicted types for the function's return value. -
variables
: List of local variables and their type annotation (if available) -
variables_p
: List of predicted types for the function's local variables.
-
-
imports
: An array of imported names in the Python file -
mod_var_ln
: Contains line numbers for the start and end of global variables in the file -
mod_var_occur
: Occurences of global variables in the file (tokenized) -
no_types_annot
: No. of pre-existing type annotations in the file,D
: developer-provided,I
: Inferred by Pyre,U
: No. of type slots with no type annotation. -
session_id
: Each request has a unique session ID (hashed string). -
set
: Its value is alwaysNone
and should be ignored. -
tc
: Whether the file can be type-checked by Mypy (this feature is disabled as of this writing). -
type_annot_cove
: The file's pre-existing type annotation coverage -
typed_seq
,untyped_seq
: Seq2Seq representation of the file. [Always empty] -
variables
: List of the file's global variables and their type annotation (if available) -
variables_p
: List of predicted types for the file's global variables.
-
Note: Line numbers fields contains line and column number for both the start and end of an identifier.