flake8-simplify icon indicating copy to clipboard operation
flake8-simplify copied to clipboard

SIM401 should not apply to non-constant default values

Open thomasdesr opened this issue 1 year ago • 0 comments

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)

thomasdesr avatar Jan 12 '23 07:01 thomasdesr