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

Forbid returning too long tuples #1731

Closed
orsinium opened this issue Nov 17, 2020 · 6 comments · Fixed by #1939
Closed

Forbid returning too long tuples #1731

orsinium opened this issue Nov 17, 2020 · 6 comments · Fixed by #1939
Assignees
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Milestone

Comments

@orsinium
Copy link
Collaborator

Rule request

Thesis

Forbid returning a too long constant-length tuple from a function:

# bad
def hello():
  return 1, 2, 3, 4

When there are more than 2 values must be returned, use dataclasses.dataclass, typing.NamedTuple, or attrs instead.

Reasoning

Code readability and maintainability: the caller code must know exact order of all components which is easy to mess up. A real-world example:

time, phase, spacing, size, version, error, series_type = read_result_log()

😩

@orsinium orsinium added the rule request Adding a new rule label Nov 17, 2020
@sobolevn
Copy link
Member

We already have:

class TooLongYieldTupleViolation(ASTViolation):

and

class TooLongTupleUnpackViolation(ASTViolation):

Maybe it is a good idea to simplify them into a single rule.

On the other hand, I like to define long immutable sequences as tuples. I guess that this should be allowed.

@orsinium
Copy link
Collaborator Author

Hmm. I tried the snippet above on flakehell.orsinium.dev and it hasn't found anything 🤔 I'm wondering what I did wrong.

image

@orsinium
Copy link
Collaborator Author

Oh, I see, it's only about yield, not return

On the other hand, I like to define long immutable sequences as tuples. I guess that this should be allowed.

Sure. This is why I said "constant-length". When you use a tuple as an immutable list (when the length is unknown in advance), it is fine.

@sobolevn sobolevn added this to the Version 0.15 aka Python3.9 milestone Feb 6, 2021
@sobolevn sobolevn added help wanted Extra attention is needed level:starter Good for newcomers labels Feb 6, 2021
@uhbif19
Copy link
Contributor

uhbif19 commented Mar 5, 2021

@sobolevn So am I understanding correctly, that the issue realization must just add full TooLongYieldTupleViolation analog but working with return?

@orsinium
Copy link
Collaborator Author

orsinium commented Mar 5, 2021

Exactly! I don't see a reason why return is any different from yield from this perspective :)

@uhbif19
Copy link
Contributor

uhbif19 commented Mar 10, 2021

Please assign me, I am finishing PR for this task.

uhbif19 added a commit to uhbif19/wemake-python-styleguide that referenced this issue Mar 10, 2021
uhbif19 added a commit to uhbif19/wemake-python-styleguide that referenced this issue Mar 11, 2021
uhbif19 added a commit to uhbif19/wemake-python-styleguide that referenced this issue Mar 11, 2021
sobolevn pushed a commit to uhbif19/wemake-python-styleguide that referenced this issue Dec 13, 2021
sobolevn added a commit that referenced this issue Dec 13, 2021
* Closes #1731

* Update CHANGELOG.md

Co-authored-by: Nikita Sobolev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants