tristanlatr / astuce

AST inference utilities
GNU Lesser General Public License v2.1
1 stars 0 forks source link

Proposed API #8

Closed tristanlatr closed 2 years ago

tristanlatr commented 2 years ago

The following code fragment illustrate how we could be able to use the high level inference API:

from astuce import parser, inference

p = parser.Parser()

mod1 = p.parse('''
from mod2 import __all__ as _l
__all__ = ['f', 'k']
__all__.extend(_l)
''', modname='mod1')

mod2 = p.parse('''
__all__ = ('i', 'j')
''', modname='mod2')

inferred = list(inference.infer_attr(mod2, '__all__'))
assert len(inferred) == 1
assert inferred[0].literal_eval() == ['f', 'k', 'i', 'j']

Feedback welcome :)

tristanlatr commented 2 years ago

Here is an example where the inference is ambiguous:

from .mod import l as _l
import sys
l = ['f', 'k']
if sys.version_info > (3,8):
    l.extend(_l)

To handle these kind of cases I believe we could optionally set a value to the name sys.version_info (or any name) as a context manager for instance, such that the If.test condition can be inferred. But it's probably something to add in a second phase of development.