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

Question about WPS324 — Enforce consistent return statements. #2903

Closed
Day0Dreamer opened this issue Mar 30, 2024 · 2 comments
Closed

Question about WPS324 — Enforce consistent return statements. #2903

Day0Dreamer opened this issue Mar 30, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Day0Dreamer
Copy link

Day0Dreamer commented Mar 30, 2024

What's wrong

Good day, please help me to understand the implications and logic of that rule.

For example, I have a most basic getter function. Let's say it does a requests.get('url'). We want html content. Simple.

I did the simplest thing:

def get_html():
  response = requests.get('google.com')
  if response.code == 200:
    return response.text
  if response.code != 200:
    return None

This goes against the rule.

Right before now, I was just doing the easiest if statement, to check if there is something returned and then continued on the happy path. Can't do it now, this goes against the rule.

...
text = gethtml()
if text:
  print(text)
else:
  sys.exit(0)
...

What improvement course is there?

  • Am I supposed to wrap that function call into try-except and rise an exception?
  • Is there some object I can return that signifies that the response was not 200?
  • Any cool other insight?

How it should be

I don't know!

Raising exceptions seems like heavy overengineering.

Flake8 version and plugins

7.0.0 (darglint: 1.8.1, flake8-bandit: 4.1.1, flake8-broken-line: 1.0.0, flake8-bugbear: 24.2.6, flake8-commas: 2.1.0, flake8-comprehensions: 3.14.0, flake8-debugger: 4.1.2, flake8-docstrings: 1.7.0, flake8-eradicate:
1.5.0, flake8-isort: 6.1.1, flake8-quotes: 3.4.0, flake8-rst-docstrings: 0.3.0, flake8-string-format: 0.3.0, mccabe: 0.7.0, pep8-naming: 0.13.3, pycodestyle: 2.11.1, pyflakes: 3.2.0, wemake-python-styleguide: 0.19.2)
CPython 3.12.0 on Windows

pip information

using pipx, can't tell.

OS information

Win11

@Day0Dreamer Day0Dreamer added the bug Something isn't working label Mar 30, 2024
@sobolevn
Copy link
Member

This code:

def get_html():
  response = requests.get('google.com')
  if response.code == 200:
    return response.text
  if response.code != 200:
    return None

is the same as:

def get_html():
  response = requests.get('google.com')
  if response.code == 200:
    return response.text
  return None

So, this is what this rule tries to say :)

@sobolevn
Copy link
Member

Please, reopen if that does not work for you :)

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

2 participants