Skip to content

Replace lambda assignments with def in asyncio.subprocess (PEP 8 E731) #149951

@deadlovelll

Description

@deadlovelll

Feature or enhancement

Proposal:

Lib/asyncio/subprocess.py contains two protocol_factory = lambda: ...
assignments that violate PEP 8 recommendation E731:

Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier

# Lib/asyncio/subprocess.py:209
async def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
                                limit=streams._DEFAULT_LIMIT, **kwds):
    loop = events.get_running_loop()
    protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
                                                        loop=loop)
    ...

# Lib/asyncio/subprocess.py:222
async def create_subprocess_exec(program, *args, stdin=None, stdout=None,
                                stderr=None, limit=streams._DEFAULT_LIMIT,
                                **kwds):
    loop = events.get_running_loop()
    protocol_factory = lambda: SubprocessStreamProtocol(limit=limit,
                                                        loop=loop)
    ...

Proposed change:

Replace both with proper def statements:

def protocol_factory():
    return SubprocessStreamProtocol(limit=limit, loop=loop)

Sources:

  1. https://peps.python.org/pep-0008/#programming-recommendations:~:text=Always%20use%20a%20def%20statement%20instead%20of%20an%20assignment%20statement%20that%20binds%20a%20lambda%20expression%20directly%20to%20an%20identifier%3A
  2. https://docs.astral.sh/ruff/rules/lambda-assignment/

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions