microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.71k stars 765 forks source link

Non-local variable modification in generator factory function results in incorrect unreachable code analysis #6587

Open ilanbiala opened 8 hours ago

ilanbiala commented 8 hours ago

Environment data

Code Snippet

import can  # 4.4.2
import pytest  # 8.3.3

@pytest.fixture()
def connect_lnk_can_bus(lnk_can_filters: CANFilterList) -> Generator[Callable[[], can.bus.BusABC]]:
    """Access LNK CAN bus."""
    bus: can.bus.BusABC | None = None

    def _connect_lnk_can_bus() -> can.bus.BusABC:
        nonlocal bus
        bus = can.interface.Bus(channel="link-can", can_filters=lnk_can_filters, interface="socketcan", bitrate=500_000)
        return bus

    yield _connect_lnk_can_bus
    if isinstance(bus, can.bus.BusABC):
        bus.shutdown()

if the returned factory function is executed, bus is defined and this follows pytest's factory-as-fixture pattern with cleanup (https://docs.pytest.org/en/8.3.x/how-to/fixtures.html#factories-as-fixtures).

Logs

2024-10-22 14:00:09.880 [info] [Info  - 2:00:09 PM] (3401138) Pylance language server 2024.10.1 (pyright version 1.1.382, commit e46efc6b) starting
2024-10-22 14:00:09.914 [info] [Info  - 2:00:09 PM] (3401138) Starting service instance "av"
2024-10-22 14:00:09.971 [info] [Info  - 2:00:09 PM] (3401138) Loading pyproject.toml file at /home/ibiala/av/pyproject.toml
2024-10-22 14:00:09.999 [info] [Info  - 2:00:09 PM] (3401138) Setting pythonPath for service "av": "/bin/python3"
2024-10-22 14:00:10.017 [info] (Client) Pylance async client (2024.10.1) started with python extension (2024.16.1)
2024-10-22 14:00:10.045 [info] [Info  - 2:00:10 PM] (3401138) Assuming Python version 3.10.12.final.0
2024-10-22 14:00:13.822 [info] [Info  - 2:00:13 PM] (3401138) Found 3932 source files
2024-10-22 14:00:13.870 [info] [Info  - 2:00:13 PM] (3401138) Background analysis(1) root directory: file:///home/ibiala/.vscode-server/extensions/ms-python.vscode-pylance-2024.10.1/dist
2024-10-22 14:00:13.870 [info] [Info  - 2:00:13 PM] (3401138) Background analysis(1) started
2024-10-22 14:00:14.960 [info] [Info  - 2:00:14 PM] (3401138) Indexer background runner(2) root directory: file:///home/ibiala/.vscode-server/extensions/ms-python.vscode-pylance-2024.10.1/dist (index)
2024-10-22 14:00:14.961 [info] [Info  - 2:00:14 PM] (3401138) Indexing(2) started
2024-10-22 14:00:21.997 [info] [Info  - 2:00:21 PM] (3401138) [IDX(2)] Long operation: scan packages file:///home/ibiala/av (7267ms)
2024-10-22 14:00:21.998 [info] [Info  - 2:00:21 PM] (3401138) scanned(2) 375 files over 1 exec env
2024-10-22 14:00:22.587 [info] [Info  - 2:00:22 PM] (3401138) indexed(2) 375 files over 1 exec env
2024-10-22 14:00:22.767 [info] [Info  - 2:00:22 PM] (3401138) Indexing finished(2).
2024-10-22 14:01:01.996 [info] [Warn  - 2:01:01 PM] (3401138) Workspace indexing has hit its upper limit: 2000 files
2024-10-22 14:19:28.073 [info] [Info  - 2:19:28 PM] (3401138) Starting service instance "<default>"
2024-10-22 14:19:28.150 [info] [Info  - 2:19:28 PM] (3401138) Setting environmentName for service "<default>": "3.10.12 (global)"
2024-10-22 14:19:28.151 [info] [Info  - 2:19:28 PM] (3401138) Setting pythonPath for service "<default>": "/bin/python3"
2024-10-22 14:19:28.152 [info] [Info  - 2:19:28 PM] (3401138) No include entries specified; assuming /<default workspace root>
2024-10-22 14:19:28.153 [info] [Info  - 2:19:28 PM] (3401138) Auto-excluding **/node_modules
2024-10-22 14:19:28.153 [info] [Info  - 2:19:28 PM] (3401138) Auto-excluding **/__pycache__
2024-10-22 14:19:28.153 [info] [Info  - 2:19:28 PM] (3401138) Auto-excluding **/.*
2024-10-22 14:19:28.218 [info] [Info  - 2:19:28 PM] (3401138) Assuming Python version 3.10.12.final.0
2024-10-22 14:19:28.325 [info] [Error - 2:19:28 PM] (3401138) File or directory "/<default workspace root>" does not exist.
2024-10-22 14:19:28.326 [info] [Info  - 2:19:28 PM] (3401138) No source files found.
2024-10-22 14:19:28.342 [info] [Info  - 2:19:28 PM] (3401138) [id_0] Logging started
2024-10-22 14:19:31.768 [info] [Info  - 2:19:31 PM] (3401138) Found 3932 source files
2024-10-22 14:19:31.847 [info] [Error - 2:19:31 PM] (3401138) File or directory "/<default workspace root>" does not exist.
2024-10-22 14:19:31.847 [info] [Info  - 2:19:31 PM] (3401138) No source files found.
2024-10-22 14:19:31.851 [info] [Info  - 2:19:31 PM] (3401138) Background analysis(3) root directory: file:///home/ibiala/.vscode-server/extensions/ms-python.vscode-pylance-2024.10.1/dist
2024-10-22 14:19:31.851 [info] [Info  - 2:19:31 PM] (3401138) Background analysis(3) started
2024-10-22 14:19:38.618 [info] [Info  - 2:19:38 PM] (3401138) [id_0] Logging stopped at /tmp/pyright-3401138-W23cCY02Qirm/pylance_2024.10.1_id_0.txt
2024-10-22 14:19:42.711 [info] [Info  - 2:19:42 PM] (3401138) [id_1] Logging started
2024-10-22 14:19:45.829 [info] [Info  - 2:19:45 PM] (3401138) Found 3932 source files
2024-10-22 14:19:45.902 [info] [Error - 2:19:45 PM] (3401138) File or directory "/<default workspace root>" does not exist.
2024-10-22 14:19:45.902 [info] [Info  - 2:19:45 PM] (3401138) No source files found.
2024-10-22 14:20:30.384 [info] [Warn  - 2:20:30 PM] (3401138) Workspace indexing has hit its upper limit: 2000 files
erictraut commented 8 hours ago

Could you please provide a self-contained, minimal code sample that exhibits the problem? Your code sample above references a bunch of symbols that are not defined and are not part of the stdlib. If your code sample requires third-party libraries to repro the problem, please provide the names and versions of these packages. For example, I don't know what package can refers to above. There is a package named can in the pypi registry, but I don't think that's what you're assuming here.