watch-this
watch-this

Reputation: 1

PyCharm stops at a breakpoint where it shouldn't

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))

Upvotes: 2

Views: 289

Answers (2)

Ghasem
Ghasem

Reputation: 15563

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+

Upvotes: 0

Pavel Karateev
Pavel Karateev

Reputation: 8495

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

Upvotes: 2

Related Questions