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

SIM401 should not apply to non-constant default values #166

Open
thomasdesr opened this issue Jan 12, 2023 · 0 comments
Open

SIM401 should not apply to non-constant default values #166

thomasdesr opened this issue Jan 12, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@thomasdesr
Copy link

Version:

flake8-simplify==0.19.3

Desired change

  • Rule(s): SIM401
  • Adjustment: SIM401 should not apply to non-constant default values.

Explanation

When the default value in SIM401 statement isn't constant, it may be arbitrarily expensive to calculate if moved into a get. This is not something that can be simplified because python will eagerly evaluate the value in order to execute the get

Example

import asyncio


async def some_async_func():
    await asyncio.sleep(10)


async def foo():
    d = {"key": "value"}
    key = "key"

    if key in d:
        value = d[key]
    else:
        value = await some_async_func()

    return value

Believes it can be simplified to:

import asyncio


async def some_async_func():
    await asyncio.sleep(10)


async def foo():
    d = {"key": "value"}
    key = "key"

    value = d.get(key, await some_async_func())

    return value

However these two are not interchangeable as some_async_func() may be arbitrarily expensive as demonstrated here by the asyncio.sleep(10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants