Unexpected `ExpressionContext.LOAD` for names of keyword arguments
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 229
- PR merge metrics
- No merged PRs in 30d
Description
```python
import ast
import libcst as cst
from libcst.metadata import ExpressionContextProvider
src = "fn(kwarg=1)"
print(ast.dump(ast.parse(src).body[0].value, indent=4))
wrapper = cst.metadata.MetadataWrapper(cst.parse_module(src))
for node, ctx in wrapper.resolve(ExpressionContextProvider).items():
print(ctx, node)
```
```python
Call(
func=Name(id='fn', ctx=Load()),
args=[],
keywords=[
keyword(
arg='kwarg',
value=Constant(value=1))])
ExpressionContext.LOAD Name(
value='fn',
lpar=[],
rpar=[],
)
ExpressionContext.LOAD Name(
value='kwarg', # <---- does not match the behavior of the AST module
lpar=[],
rpar=[],
)
```
This came up while I was working on https://github.com/HypothesisWorks/hypothesis/issues/4116, i.e. trying to turn undefined unqualified names in patches into the appropriate attribute access. In that context, it's quite surprising to see `array=array(...)` become `np.array=np.array(...)`!
It was easy enough to work around this once I worked out what was happening, but I think that [the reasoning here](https://libcst.readthedocs.io/en/latest/metadata.html#libcst.metadata.ExpressionContextProvider) (to honor ast implementation, we don’t assign context to attr) suggests that `.keyword` should not be assigned context either.
Contributor guide
Assessment
This issue has not been assessed yet.