SMAT-Lab / Scalpel

Scalpel: The Python Static Analysis Framework
Apache License 2.0
286 stars 43 forks source link

Is type inferencing supposed to be interprocedural? #33

Closed khatchad closed 2 years ago

khatchad commented 2 years ago

Consider the following modified type_infer_example.py (`:

from os import getcwd

def my_function():
    x = "Current working directory: "
    return x + getcwd()

def main():
    y = my_function()

Running type_infer_tutorial.py on this file, I get the following output (slightly modified for clarify):

python type_infer_tutorial.py 
{'file': 'type_infer_example.py', 'line_number': 4, 'function': 'my_function', 'type': {'str'}}
{'file': 'type_infer_example.py', 'line_number': 5, 'variable': 'x', 'function': 'my_function', 'type': {'str'}}
{'file': 'type_infer_example.py', 'line_number': 9, 'variable': 'y', 'function': 'main', 'type': {'any'}}

Given that my_function has type str, should not y have type str as well instead of any? I am confused about two things:

  1. Is the type inference supposed to be interprocedural or intraprocedural?
  2. Shouldn't the entry point be a function rather than a file? Is the issue that the entry point is specified as the entire file, i.e., type_infer_example.py and that multiple functions are defined in that file?
Jarvx commented 2 years ago

Hi, Thank you for raising this issues. The tool is interprocedural in terms of implementation. However, it looks to me the results from the tool is :


{'file': 'case18.py', 'line_number': 5, 'function': 'MyParentClass.my_function', 'type': {'str'}}
{'file': 'case18.py', 'line_number': 11, 'function': 'ChildClass.my_function', 'type': {'List[str]'}}

Can you please provide your execution environment information?

Best regards, Jiawei

khatchad commented 2 years ago

Sure:

$ uname -a
Linux rk1424-ThinkPad-L14-Gen-1 5.15.0-27-generic #28-Ubuntu SMP Thu Apr 14 04:55:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ python --version
Python 3.10.4

I'm on be392671a5f55f5da8b2ca0a079207c55261ab03.

Jarvx commented 2 years ago

This is due to the dependency issue when accessing typshed project. Will fix it soon.

Jarvx commented 2 years ago

I got the following error Running [type_infer_tutorial.py]: Traceback (most recent call last): File "C:\Users\00\Desktop\Hityper\Scalpel-master\Scalpel-master\ex.py", line 1, in from scalpel.typeinfer.typeinfer import TypeInference File "C:\Users\00\Desktop\Hityper\Scalpel-master\Scalpel-master\scalpel\typeinfer\typeinfer.py", line 14, in from scalpel.typeinfer.analysers import ( File "C:\Users\00\Desktop\Hityper\Scalpel-master\Scalpel-master\scalpel\typeinfer\analysers.py", line 9, in from typed_ast import ast3 ModuleNotFoundError: No module named 'typed_ast'

what should I do to get the type inference for a file?? Thanks for reporting this. It seems due to the Python version. Did you execute the command under Python3.10?

alaalial commented 2 years ago

Python 3.9.7

Jarvx commented 2 years ago

Python 3.9.7

Okay, I will test this very soon.

alaalial commented 2 years ago

thanks for replay should I install python 3.8

Jarvx commented 2 years ago

from typed_ast import ast3

You can try ' from typed_ast import ast3' and see if that works for your environment

Jarvx commented 2 years ago

how did you get type inference and you used python 3.10? @khatchad

I tested it under 3.9 and it works. We will conduct future testing on this. Thanks for pointing this out

Jarvx commented 2 years ago

Please feel free to reopen if you have any questions.