Skip to content

Forbid complex grammar in slice #3427

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

Open
MIXgleb opened this issue May 21, 2025 · 2 comments · May be fixed by #3433
Open

Forbid complex grammar in slice #3427

MIXgleb opened this issue May 21, 2025 · 2 comments · May be fixed by #3433
Labels
rule request Adding a new rule

Comments

@MIXgleb
Copy link
Contributor

MIXgleb commented May 21, 2025

Rule request

Allow only a name, constant, and attribute in slice args.

Thesis

Correct:
array[3:7]
array[start_index:end_index]
array[index.start:index.end]

Wrong:
array[my_dict["start_index"]:my_list[-1]]
array[start_index():index.end()]

Should be:

start_index = my_dict["start_index"]
end_index = my_list[-1]
new_array = array[start_index:end_index]
start_index = start_index()
end_index = index.end()
new_array = array[start_index:end_index]

Reasoning

Complex grammar are difficult to read and should be stored in separate vars.

It's really difficult to review such many dependent constructions:
new_array = array[my_dict[":"]::get_step()]

It might also be worth forbidding an attribute, but I don't think it's a complex grammar.

class Slice:
    class MyArray:
        start = 1
        end = 1

new_array = array[Slice.MyArray.start:]

But not
new_array = array[Slice(arg_1).MyArray(arg_2, arg_3).start:]

@MIXgleb MIXgleb added the rule request Adding a new rule label May 21, 2025
@sobolevn
Copy link
Member

Thanks for the idea. I will need to think about this and analyze our / real-world use-cases.

@MIXgleb
Copy link
Contributor Author

MIXgleb commented May 21, 2025

Let's me make a PR and you will review my ideas and updates in detail.

MIXgleb added a commit to MIXgleb/wemake-python-styleguide that referenced this issue May 26, 2025
@MIXgleb MIXgleb linked a pull request May 26, 2025 that will close this issue
4 tasks
MIXgleb added a commit to MIXgleb/wemake-python-styleguide that referenced this issue May 27, 2025
MIXgleb added a commit to MIXgleb/wemake-python-styleguide that referenced this issue May 29, 2025
MIXgleb added a commit to MIXgleb/wemake-python-styleguide that referenced this issue May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule request Adding a new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants