2

I'm experiencing this behavior in PyCharm Build #PY-222.4345.23 on macOS Monterey 12.6.1, which happens in both python 3.10 and 3.11.

def example(i):
    match i % 4:
        case 0:
            if i > 10:
                return 0
        case 1:
            if i > 10:
                return 1
        case 2:
            if i > 10:
                print(f'{i}, {i > 10}')
                return 2
        case 3:
            if i > 10:
                return 3


if __name__ == '__main__':
    print(example(2))

In both python versions, debugger stops at 3rd return statement (unexpected, block should be unreachable) but doesn't actually return anything which is the expected behavior for the given input 2

python 3.11 (1 breakpoint to reproduce)

enter image description here

python 3.10 (2 breakpoints to reproduce)

enter image description here

If switch and cases were replaced with if/elif blocks, the very same breakpoint is never reached which is exactly the behavior I'm expecting for the switch blocks.

def example(i):
    j = i % 4
    if j == 0:
        if i > 10:
            return 0
    elif j == 1:
        if i > 10:
            return 1
    elif j == 2:
        if i > 10:
            print(f'{i}, {i > 10}')
            return 2
    elif j == 3:
        if i > 10:
            return 3


if __name__ == '__main__':
    print(example(2))
5
  • I do not understand what do you expect. Break point just pause execution before that line, so before return 2. If you continue (e.g. line by line), the debugger will execute the return and so you will have the 2. But on your code, return 2 should never be executed. You call return 2 only if i > 10, but you have i = 2, so no return. Please specify better what do you expect and what you get. Commented Oct 31, 2022 at 10:17
  • Breakpoint should be unreachable hence it should never stop for i = 2.
    – watch-this
    Commented Oct 31, 2022 at 10:31
  • I've tried this on exactly the same PyCharm build (Community Edition), Python 3.10, but Windows 10 and I can't reproduce the reported behaviour. I only get the breakpoint hit for example(14) and not for example(2)
    – quamrana
    Commented Oct 31, 2022 at 14:07
  • How many breakpoints did you use? If not 2, try placing a second as demonstrated in the screenshot above.
    – watch-this
    Commented Oct 31, 2022 at 14:29
  • Ok, yes, I see. I needed both breakpoints set to reproduce. It hits the second breakpoint now.
    – quamrana
    Commented Nov 2, 2022 at 11:28

2 Answers 2

2

IDE bug. I've filed a ticket in PyCharm's issue tracker - https://youtrack.jetbrains.com/issue/PY-57125/PyCharm-stops-on-non-hit-breakpoint-inside-pattern-matching-block

2
  • I forgot to mention that I already filed a ticket with the issue.
    – watch-this
    Commented Nov 2, 2022 at 11:43
  • OK, I closed mine as a duplicate and updated yours. Commented Nov 2, 2022 at 16:25
0

In my case I was running the Python application using uvicorn. So a quick fix for me was to put the reload value to False

uvicorn.run("main:app", host="0.0.0.0", port=8007, reload=False)

Also, If you're using uvicorn via a shell, remove the --reload parameter.

This is hopefully a temporary fix until they fix the issue with Pycharm and Python 3.10+

Not the answer you're looking for? Browse other questions tagged or ask your own question.