Instagram / Instagram/LibCST

Clarifying expression context of import statements

Open
#761 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

Currently `ExpressionContextProvider` treats import statement as having `LOAD` context. For a tiny example as `import a`:

```python
import libcst as cst
from libcst.metadata import ExpressionContextProvider
from libcst.helpers import ensure_type

module = cst.parse_module("import a")
wrapper = cst.MetadataWrapper(module)
metadata = wrapper.resolve(ExpressionContextProvider)

line = ensure_type(wrapper.module.body[0], cst.SimpleStatementLine)
a = ensure_type(line.body[0], cst.Import).names[0].name

print(metadata[a]) # result is ExpressionContext.LOAD
```

This may look correct and natural at first sight. But if we think more carefully, we see that the symbol `a` in the module namespace is actually being `STORE`'ed. (we can think of a **symbol** like a designation pointing to a tiny storage unit within a large storage room called **module namespace**)

The import statement is actually loading somethingᵀᴹ from some external place, and store this somethingᵀᴹ inside the symbol `a` in the module namespace.

It's not retrieving something already previously stored inside symbol `a` in the module namespace. Before the import statement, the module namespace stores nothing for symbol `a`.

Based on these observations, I would prefer this being a `STORE` context more than a `LOAD` context. I could be missing something though. What's your thoughts on this? 🤔

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.