Skip to content

gh-149951 Use def instead of lambda assignments in asyncio.subprocess#149952

Closed
deadlovelll wants to merge 1 commit into
python:mainfrom
deadlovelll:gh-149951-subprocess-lambda-to-def
Closed

gh-149951 Use def instead of lambda assignments in asyncio.subprocess#149952
deadlovelll wants to merge 1 commit into
python:mainfrom
deadlovelll:gh-149951-subprocess-lambda-to-def

Conversation

@deadlovelll
Copy link
Copy Markdown

@deadlovelll deadlovelll commented May 17, 2026

Replace the two protocol_factory = lambda: ... assignments in
asyncio.subprocess with proper def statements, as recommended by
PEP 8 (E731):

Affected functions: create_subprocess_shell and create_subprocess_exec.

Before:

protocol_factory = lambda: SubprocessStreamProtocol(limit=limit, loop=loop)

After:

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

This change also gives the factories a meaningful __name__ and __qualname__.

Reproducer
import traceback


def make_with_lambda():
  limit = 1024
  loop = "fake_loop"
  protocol_factory = lambda: SubprocessStreamProtocol(limit=limit, loop=loop)
  return protocol_factory


def make_with_def():
  limit = 1024
  loop = "fake_loop"

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

  return protocol_factory


class SubprocessStreamProtocol:
  def __init__(self, limit, loop):
      raise RuntimeError("simulated init failure")


def show(label, factory):
  print(f"=== {label} ===")
  print(f"  __name__     = {factory.__name__!r}")
  print(f"  __qualname__ = {factory.__qualname__!r}")
  print(f"  repr         = {factory!r}")
  print()
  print("  Traceback when factory raises:")
  try:
      factory()
  except RuntimeError:
      tb = traceback.format_exc()
      for line in tb.splitlines():
          print(f"    {line}")
  print()


show("lambda-traceback", make_with_lambda())
show("def-traceback", make_with_def())

Output:

=== lambda-traceback ===
  __name__     = '<lambda>'
  __qualname__ = 'make_with_lambda.<locals>.<lambda>'
  repr         = <function make_with_lambda.<locals>.<lambda> at 0x1010156d0>

File "/Users/timofeiivankov/Desktop/untitled folder/cpython/lws.py", line 7, in <lambda>
     protocol_factory = lambda: SubprocessStreamProtocol(limit=limit, loop=loop)
                              ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
=== def-traceback ===
  __name__     = 'protocol_factory'
  __qualname__ = 'make_with_def.<locals>.protocol_factory'
  repr         = <function make_with_def.<locals>.protocol_factory at 0x100e413d0>

 File "/Users/timofeiivankov/Desktop/untitled folder/cpython/lws.py", line 16, in protocol_factory
       return SubprocessStreamProtocol(limit=limit, loop=loop)

Cleanup only, no behavior change.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 17, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 17, 2026

Sorry but this is code churn.

@picnixz picnixz closed this May 17, 2026
@deadlovelll
Copy link
Copy Markdown
Author

Got it, thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants