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

Incorrect removal of brackets #2668

Open
nikonikolov opened this issue Jan 2, 2025 · 0 comments
Open

Incorrect removal of brackets #2668

nikonikolov opened this issue Jan 2, 2025 · 0 comments

Comments

@nikonikolov
Copy link

code = '''
if attn_output.size() != (size := (batch_size, self.num_heads, query.shape[1], self.head_dim)):
     raise ValueError(f"`attn_output` should be of size {size}, but is {attn_output.size()}")
'''
print(astroid.extract_node(code).as_string())

Running the above code produces the output

if attn_output.size() != size := (batch_size, self.num_heads, query.shape[1], self.head_dim):
    raise ValueError(f'`attn_output` should be of size {size}, but is {attn_output.size()}')

This is incorrect since the brackets in the assignment operator are dropped and the syntax becomes incorrect.

Another example of dropping brackets:

code = '''
collections.OrderedDict(
    {
        'linear': torch.nn.Linear(in_features, out_features, bias=bias),
        **(
            {
                'norm': TorchNorm(out_features),
                'act': TorchActivation(),
            }
            if not (i == len(layer_sizes) - 2 and not activate_final)
            else {}
        ),
    }
)
'''
print(astroid.extract_node(code).as_string())

outputs:

collections.OrderedDict({'linear': torch.nn.Linear(in_features, out_features, bias=bias), **{'norm': TorchNorm(out_features), 'act': TorchActivation()} if not (i == len(layer_sizes) - 2 and not activate_final) else {}})

Brackets following the ** operator are missing and syntax becomes incorrect

Python version: 3.10
astroid version: 3.3.6

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