Weird inspection error in PyCharm 2025.2.4 Build #PY-252.27397.106

I've stumbled across what I believe is a bug in the inspection logic:

Description:

 PyCharm's code inspection suggests removing the frame parameter from Python signal handler functions, claiming it's unused. However, this parameter is required 
 by Python's signal.signal() protocol, and removing it causes a runtime crash.

 Environment:
 - PyCharm version: [Your version]
 - Python version: 3.10+
 - Type checker: Pylance/PyCharm bundled

 Steps to Reproduce:

 1. Create a signal handler with the required (signum, frame) signature:
 def handler(signum: int, frame: object) -> None:
     print(f"Signal received: {signum}")

 2. PyCharm shows a warning: "Parameter 'frame' value is not used"
 3. PyCharm suggests: "Remove parameter 'frame'"

 Expected Behavior:

 PyCharm should recognize that signal handlers require both parameters per Python's callback protocol, even if the frame is unused in the function body. The
 inspection should either:
 - Not suggest removal, OR
 - Suggest renaming to _frame (unused parameter convention)

 Actual Behavior:

 PyCharm suggests removing the parameter entirely, which breaks the code at runtime.

 Proof of Issue:

 Test case demonstrating the crash:

 #!/usr/bin/env python3
 import signal
 import os
 import time
 from types import FrameType

 def test_with_frame(signum: int, frame: FrameType | None) -> None:
     print(f"WITH frame param: Received signal {signum}")

 def test_without_frame(signum: int) -> None:
     print(f"WITHOUT frame param: Received signal {signum}")

 # Test 1: With frame parameter (works)
 print("Test 1: Handler WITH frame parameter")
 signal.signal(signal.SIGUSR1, test_with_frame)
 os.kill(os.getpid(), signal.SIGUSR1)
 time.sleep(0.1)

 # Test 2: Without frame parameter (crashes)
 print("\nTest 2: Handler WITHOUT frame parameter")
 signal.signal(signal.SIGUSR1, test_without_frame)
 os.kill(os.getpid(), signal.SIGUSR1)
 print("If you see this, test 2 succeeded")

 Runtime Result:
 Test 1: Handler WITH frame parameter
 WITH frame param: Received signal 10

 Test 2: Handler WITHOUT frame parameter
 Traceback (most recent call last):
   File "test.py", line 26, in <module>
     os.kill(os.getpid(), signal.SIGUSR1)
 TypeError: test_without_frame() takes one positional argument, but two were given

 Type System Evidence:

 When the frame parameter is removed, the type checker correctly flags an error:
 Expected type '(int, FrameType | None) → Any | int | Handlers | None'
 got '(signum: int) → None' instead

 This confirms that Python's signal.signal() requires handlers to accept both parameters.

 Why This Matters:

 Signal handlers are callbacks where Python's signal module controls the signature. When a signal fires, Python always passes two arguments:
 1. signum - The signal number
 2. frame - The current execution frame

 Even if the handler doesn't use frame, it must accept it, or Python crashes when calling the handler.

0

Hi tony , this issue requires thorough investigation, please create an issue on YouTrack (https://youtrack.jetbrains.com/issues/PY) and attach all relevant information for quicker resolution.

0

帖子评论已关闭。