Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected ExpressionContext.LOAD for names of keyword arguments #1217

Open
Zac-HD opened this issue Oct 6, 2024 · 0 comments
Open

Unexpected ExpressionContext.LOAD for names of keyword arguments #1217

Zac-HD opened this issue Oct 6, 2024 · 0 comments

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Oct 6, 2024

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)
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 HypothesisWorks/hypothesis#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 (to honor ast implementation, we don’t assign context to attr) suggests that .keyword should not be assigned context either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant