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

WPS111 too strict inside comprehensions #1321

Closed
webknjaz opened this issue Apr 8, 2020 · 8 comments
Closed

WPS111 too strict inside comprehensions #1321

webknjaz opened this issue Apr 8, 2020 · 8 comments
Labels
bug Something isn't working

Comments

@webknjaz
Copy link
Contributor

webknjaz commented Apr 8, 2020

Bug report

I think that WPS111 is too restrictive with vars that are only used in comprehension-like expressions.

What's wrong

It says that s is too short here:

[s.encode() for s in cythonize_args]

How is that should be

it doesn't make sense for throwaway vars in list/dict/set comprehensions or generator expressions to be long.

@webknjaz webknjaz added the bug Something isn't working label Apr 8, 2020
@sobolevn
Copy link
Member

sobolevn commented Apr 8, 2020

Thanks a lot for the report, @webknjaz! I get your point.

But. Sometimes these comprehensions can grow. [s.encode() for s in cythonize_args] can become [s.encode() for s in cythonize_args if s is not None] and it can become [normalize(s.encode()) for s in cythonize_args if s is not None].

And its final form can be [normalize(s.encode()) for key, s in cythonize_args if s is not None and key in allowed_keys] Boom!

Moreover, I still don't know what s is (from your example). Let me guess, that it is something like [byte_arg.encode() for byte_arg in cythonize_args]. Which is better in my opinion.

Sadly, I cannot draw a good line to separate really simple cases from not so simple ones. And that's why I don't think that we will be adding a support for comprehensions in this rule.

Do you agree with me on this one? 🙂

@webknjaz
Copy link
Contributor Author

webknjaz commented Apr 8, 2020

I agree that comprehensions can grow but in such cases, Jones Complexity rule hits me instead. So I'd say this case is covered.
Also, here it's a comprehension of style "i don't care what the items are, i just want them converted".

I can easily rewrite this as list(map(str.encode, cythonize_args)) and it will pass the linter but will be ultimately less readable. I mean, I'd be okay if there was an option to prefix the var with an underscore but the rule doesn't seem to care if I declare it unimportant (e.g. _s).

@webknjaz
Copy link
Contributor Author

webknjaz commented Apr 8, 2020

Also, this rule forces me to multiline the expression just because naming vars cause the line to not fit within limits while the cognitive complexity of the original line doesn't really require it.

@sobolevn
Copy link
Member

sobolevn commented Apr 8, 2020

I agree that comprehensions can grow but in such cases, Jones Complexity rule hits me instead.

Not really, if you use \n to separate for and if on new lines (like a lot of people do)

list(map(str.encode, cythonize_args)) and it will pass the linter but will be ultimately less readable

Yes, I agree. map is controversial. Let's ignore it for now.

Anyway, does two letter names work for you in this case? Like xy? Or ss?

@webknjaz
Copy link
Contributor Author

webknjaz commented Apr 8, 2020

Well, I think that most of the time unimportant var should be one-char. Sometimes it's _ (I actually forgot to check it against this rule).

xy/ss seem to add some confusion visually because if after the first char there's the second one, it creates an impression that this var is a part of series of similarly-named vars (su, st, ss / xx, xy, xz) when its name doesn't resemble words.

@orsinium
Copy link
Collaborator

Related:

Short variable names work well when the distance between their declaration and last use is short.

@sobolevn
Copy link
Member

sobolevn commented Feb 6, 2021

I am going to decline this feature. I really don't like short variable names. Sorry 😓

@sobolevn sobolevn closed this as completed Feb 6, 2021
@FyZzyss
Copy link

FyZzyss commented Aug 1, 2022

Hello! You have a very good plugin! I fell in love with this at first sight, but your uncooperative approach is frustrating. I think it would be possible to make a parameter whether to check a variable in a loop that defaults to True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants