Skip to content

Commit

Permalink
Merge pull request #21 from saltudelft/fn_lineno
Browse files Browse the repository at this point in the history
Add line and column no. for the start and end of functions
  • Loading branch information
mir-am authored May 31, 2021
2 parents 99c758b + ee38b41 commit c66eaab
Show file tree
Hide file tree
Showing 12 changed files with 1,167 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to the [LibSA4Py](https://github.com/saltudelft/libsa4py) to
- Extracting type annotations with a qualified name from source code files.
- Adding `apply` command to apply inferred type annotations to source code files.
- Adding a qualified name for functions in the JSON field `q_name`.
- Adding line and column no. for the start and end of functions in the JSON field `fn_lc`.

### Fixed
- Malformed output sequences containing string literal type, i.e., `Literal['Blah \n blah']`.
Expand Down
5 changes: 5 additions & 0 deletions libsa4py/cst_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def leave_FunctionDef(self, node: cst.FunctionDef):
# Decrease stack depth of the current function
fn = self.stack.pop()

fn.ln_col = self.__get_line_column_no(fn.node)
fn.docstring, params_descr = extract_docstring_descriptions(self.__extract_docstring(node))
fn.params_descr = {p: params_descr[p] if p in params_descr else '' for p in fn.parameters.keys()}

Expand Down Expand Up @@ -701,3 +702,7 @@ def __get_type_from_metadata(self, node: cst.Name) -> str:

except KeyError:
return ''

def __get_line_column_no(self, node) -> Tuple[Tuple[int, int], Tuple[int, int]]:
lc = self.get_metadata(cst.metadata.PositionProvider, node)
return (lc.start.line, lc.start.column), (lc.end.line, lc.end.column)
13 changes: 9 additions & 4 deletions libsa4py/representations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Optional, Pattern, Match
from typing import Dict, List, Tuple
from libsa4py.exceptions import OutputSequenceException

import re
Expand All @@ -16,6 +16,8 @@ class FunctionInfo:
def __init__(self, name) -> None:
self.name = name
self.q_name = None
# Line, Column no. for the start and end of the function
self.ln_col: Tuple[Tuple[int, int], Tuple[int, int]] = None
self.parameters: Dict[str, str] = {}
self.parameters_occur: Dict[str, list] = {}
self.params_descr: Dict[str, str] = {}
Expand All @@ -27,13 +29,15 @@ def __init__(self, name) -> None:
self.node = None

def to_dict(self):
return {"name": self.name, "q_name": self.q_name, "params": self.parameters, "ret_exprs": self.return_exprs,
"params_occur": self.parameters_occur, "ret_type": self.return_type, "variables": self.variables,
"fn_var_occur": self.variables_occur, "params_descr": self.params_descr, "docstring": self.docstring}
return {"name": self.name, "q_name": self.q_name, "fn_lc": self.ln_col, "params": self.parameters,
"ret_exprs": self.return_exprs, "params_occur": self.parameters_occur, "ret_type": self.return_type,
"variables": self.variables, "fn_var_occur": self.variables_occur, "params_descr": self.params_descr,
"docstring": self.docstring}

def from_dict(self, fn_dict_repr: dict):
self.name = fn_dict_repr['name']
self.q_name = fn_dict_repr['q_name']
self.ln_col = (tuple(fn_dict_repr['fn_lc'][0]), tuple(fn_dict_repr['fn_lc'][1]))
self.parameters = fn_dict_repr['params']
self.parameters_occur = fn_dict_repr['params_occur']
self.params_descr = fn_dict_repr['params_descr']
Expand All @@ -48,6 +52,7 @@ def from_dict(self, fn_dict_repr: dict):
def __eq__(self, other_func_info_obj: 'FunctionInfo'):
return other_func_info_obj.name == self.name and \
other_func_info_obj.q_name == self.q_name and \
other_func_info_obj.ln_col == self.ln_col and \
other_func_info_obj.parameters == self.parameters and \
other_func_info_obj.parameters_occur == self.parameters_occur and \
other_func_info_obj.params_descr == self.params_descr and \
Expand Down
2 changes: 1 addition & 1 deletion tests/constans.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FN_DICT_REPR = {'name': '', 'q_name': '', 'params': {}, 'ret_exprs': [], 'params_occur': {}, 'ret_type': '',
FN_DICT_REPR = {'name': '', 'q_name': '', 'fn_lc': None, 'params': {}, 'ret_exprs': [], 'params_occur': {}, 'ret_type': '',
'variables': {}, 'fn_var_occur': {}, 'params_descr': {},
'docstring': {'func': None, 'ret': None, 'long_descr': None}}
5 changes: 5 additions & 0 deletions tests/examples/different_fns.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ async def async_fn(y: int) -> int:

# An abstract function with typed args
def abstract_fn(name: str) -> str: ...


# For extracting line and column no. of a function
def fn_lineno(x):
print(x)
50 changes: 50 additions & 0 deletions tests/exp_outputs/extractor_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
{
"name": "__init__",
"q_name": "MyClass.__init__",
"fn_lc": [
[
18,
4
],
[
19,
18
]
],
"params": {
"self": "",
"y": "builtins.float"
Expand Down Expand Up @@ -78,6 +88,16 @@
{
"name": "cls_fn",
"q_name": "MyClass.cls_fn",
"fn_lc": [
[
21,
4
],
[
23,
44
]
],
"params": {
"self": "",
"c": "builtins.int"
Expand Down Expand Up @@ -138,6 +158,16 @@
{
"name": "__init__",
"q_name": "Bar.__init__",
"fn_lc": [
[
27,
4
],
[
28,
12
]
],
"params": {
"self": ""
},
Expand All @@ -164,6 +194,16 @@
{
"name": "my_fn",
"q_name": "my_fn",
"fn_lc": [
[
31,
0
],
[
32,
17
]
],
"params": {
"x": "builtins.int"
},
Expand All @@ -188,6 +228,16 @@
{
"name": "foo",
"q_name": "foo",
"fn_lc": [
[
35,
0
],
[
39,
16
]
],
"params": {},
"ret_exprs": [],
"params_occur": {},
Expand Down
Loading

0 comments on commit c66eaab

Please sign in to comment.