Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An internal error occured while type checking file #4552

Closed
xboxone08 opened this issue Jun 28, 2023 · 11 comments
Closed

An internal error occured while type checking file #4552

xboxone08 opened this issue Jun 28, 2023 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@xboxone08
Copy link

xboxone08 commented Jun 28, 2023

Type: Bug

After deleting the first line in both the release and pre-release versions, Pylance underlines the new first word of the file when Type Checking is set to 'basic' and says:

An internal error occurred while type checking file "c:\Users\me\OneDrive\Documents\main.py": TypeError: Cannot read properties of undefined (reading 'type')
    at w (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:184728)
    at E (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:180721)
    at Object.getTypeFromCodeFlow (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:185333)
    at li (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:734202)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:523021
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525219)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525276
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:526061)
    at i.timeOperation (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:1076519)
    at I (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:173673)
    at E (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:178889)
    at Object.getTypeFromCodeFlow (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:185333)
    at li (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:734202)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:523021
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525219)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525276
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:526061)
    at i.timeOperation (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:1076519)
    at I (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:173673)
    at E (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:178889)
    at Object.getTypeFromCodeFlow (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:185333)
    at li (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:734202)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:523021
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525219)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525276
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:526061)
    at c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:525276
    at Re (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:526061)
    at i.timeOperation (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:1076519)
    at I (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:173673)
    at E (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:178889)
    at Object.getTypeFromCodeFlow (c:\Users\me\.vscode-insiders\extensions\ms-python.vscode-pylance-2023.6.31\dist\pyright.bundle.js:1:185333)

This error did not occur until yesterday. It has been accompanied with a tremendous slowdown in getting function/method/class annotations and autocomplete. I am on VS Code Insiders if that has anything to do with it. VS Code Insiders gets daily updates, so that could have screwed something up, and Pylance got an update when I had it in pre-release, but the problem, as I mentioned, stayed when I switched to release (yes, I refreshed VS Code when prompted), so I just switched back to pre-release.

Extension version: 2023.6.31
VS Code version: Code - Insiders 1.80.0-insider (da7f05a70eb760e4ce1fe230ae158ae214433341, 2023-06-27T15:13:35.848Z)
OS version: Windows_NT x64 10.0.22621
Modes:

@debonte debonte added the bug Something isn't working label Jun 28, 2023
@debonte
Copy link
Contributor

debonte commented Jun 28, 2023

According to telemetry the exception is in Pyright's getTypeFromLoopFlowNode. It may be a problem with the management of flowNodeTypeCache as opposed to just failing to test if cacheEntry is undefined.

After deleting the first line...

@xboxone08, can you share your code with us with steps to reproduce this issue? I wasn't able to reproduce it by deleting the first line of an arbitrary .py file.

@debonte
Copy link
Contributor

debonte commented Jun 28, 2023

We're seeing several different stacks resulting in this error. Here's an example:

TypeError: Cannot read properties of undefined (reading 'type')
at type pyright-internal/src/analyzer/codeFlowEngine.ts:988:51
at getTypeFromLoopFlowNode pyright-internal/src/analyzer/codeFlowEngine.ts:592:31
at getTypeFromFlowNode pyright-internal/src/analyzer/codeFlowEngine.ts:1080:19
at getTypeFromCodeFlow pyright-internal/src/analyzer/typeEvaluator.ts:18777:24
at node pyright-internal/src/analyzer/typeEvaluator.ts:967:43
at node pyright-internal/src/analyzer/typeEvaluator.ts:972:51
at node pyright-internal/src/analyzer/typeEvaluator.ts:977:44
at node pyright-internal/src/analyzer/typeEvaluator.ts:972:51
at getTypeOfExpression pyright-internal/src/analyzer/typeEvaluator.ts:14846:38
at evaluateTypesForAssignmentStatement pyright-internal/src/analyzer/typeEvaluator.ts:18595:24
at callback pyright-internal/src/analyzer/typeEvaluator.ts:18709:8
at evaluateTypeForSubnode pyright-internal/src/analyzer/typeEvaluator.ts:20217:31
at getInferredTypeOfDeclaration pyright-internal/src/analyzer/typeEvaluator.ts:20534:35
at func ()
at forEach pyright-internal/src/analyzer/typeEvaluator.ts:20478:14
at node pyright-internal/src/analyzer/typeEvaluator.ts:967:43
at node pyright-internal/src/analyzer/typeEvaluator.ts:972:51
at node pyright-internal/src/analyzer/typeEvaluator.ts:972:51
at callback pyright-internal/src/common/timing.ts:40:19
at getTypeOfExpression pyright-internal/src/analyzer/codeFlowEngine.ts:1449:45
at isCallNoReturn pyright-internal/src/analyzer/codeFlowEngine.ts:1160:43
at isFlowNodeReachableRecursive pyright-internal/src/analyzer/codeFlowEngine.ts:1184:28
at isFlowNodeReachableRecursive pyright-internal/src/analyzer/codeFlowEngine.ts:1184:28
at isFlowNodeReachableRecursive pyright-internal/src/analyzer/codeFlowEngine.ts:1184:28

@xboxone08
Copy link
Author

xboxone08 commented Jun 28, 2023

@debonte

@xboxone08, can you share you code with us with steps to reproduce this issue? I wasn't able to reproduce it by deleting the first line of an arbitrary .py file.

The first line was always an import statement and after deleting, the first word was "from" in another import statement. It didn't matter what was being imported though.

@debonte
Copy link
Contributor

debonte commented Jun 28, 2023

I'm still not able to repro this. The crash location implies that the issue has something to do with a while/for loop in your code.

@xboxone08
Copy link
Author

Yes, my code is a pygame game so there are while and for loops. Interestingly I just pulled it up and without deleting the first line the underline is already there and the first word is always underlined, not just if it is a from.
image
image
image

@xboxone08
Copy link
Author

xboxone08 commented Jun 28, 2023

I tried making a new file with a basic game loop and event polling for loop but the error did not occur, so here's my code for Pylance debugging purposes only lol:

import pygame
from typing import Literal
from win10toast import ToastNotifier
from bot import Bot

notifier = ToastNotifier()

pygame.init()
screen = pygame.display.set_mode((800, 450))
pygame.display.set_caption("Portal")
clock = pygame.time.Clock()

running: bool = True

# Joystick instance_id-s and instances
joysticks: dict[int, pygame.joystick.JoystickType] = {}
# Cell inhabitants' ids mapped to index cell
cells: list[int | Literal['k']] = []
# Robot mapped to cell inhabitant id
bots: dict[int | Literal['k'], Bot] = {}
# Which character is to be typed and where mapped to cell inhabitant id
typing: dict[int | Literal['k'], str] = {'k': ''}
controller_typing = pygame.transform.smoothscale(
    pygame.image.load('controller_typing.png'), (140, 140)).convert()

# Game loop
while running:
    clock.tick(124)  # FPS

    for e in pygame.event.get():
        if e.type == pygame.JOYAXISMOTION:
            if e.axis == 0 and e.instance_id in bots:
                bots[e.instance_id].turn = round(e.value - 0.04, 1)
            elif e.axis == 1 and e.instance_id in bots:
                bots[e.instance_id].forward = round(e.value - 0.04, 1)

            elif len(typing[e.instance_id].replace(' ', '')) < 12:
                if e.axis == 4 and round(e.value, 1) == 1:
                    typing[e.instance_id] += '5 '
                elif e.axis == 5 and round(e.value, 1) == 1:
                    typing[e.instance_id] += '6 '

        # Keyboard can play (press "enter" to join and "esc" to leave)
        elif e.type == pygame.KEYUP:
            if (e.key == pygame.K_w or e.key == pygame.K_s) and 'k' in bots:
                bots['k'].forward = 0
            elif (e.key == pygame.K_a or e.key == pygame.K_d) and 'k' in bots:
                bots['k'].turn = 0

        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_w and 'k' in bots:
                bots['k'].forward = 1
            elif e.key == pygame.K_s and 'k' in bots:
                bots['k'].forward = -1
            elif e.key == pygame.K_a and 'k' in bots:
                bots['k'].turn = -1
            elif e.key == pygame.K_d and 'k' in bots:
                bots['k'].turn = 1

            elif e.key == pygame.K_BACKSPACE:
                typing['k'] = typing['k'][:-2]

            elif (e.key == pygame.K_RETURN or e.key == pygame.K_KP_ENTER):
                if 'k' not in cells and len(cells) < 8:
                    cells.append('k')
                elif len(typing['k'].replace(' ', '')) == 12 and 'k' not in bots:
                    text = typing['k'].replace(' ', '')
                    ip = text[:3].lstrip('0') + '.' + text[3:6].lstrip('0') + \
                        '.' + text[6:9].lstrip('0') + \
                        '.' + text[9:12].lstrip('0')
                    while ip.find('..') != -1:
                        ip = ip[:ip.find('..') + 1] + '0' + \
                            ip[ip.find('..') + 1:]
                    try:
                        bots[e.instance_id] = Bot(ip)
                    except OSError:
                        notifier.show_toast(
                            "IP Not Found", f"Couldn't find a robot with that IP ({ip}). Are you in the same LAN as it?", threaded=True)
            elif e.key == pygame.K_ESCAPE and 'k' in cells:
                cells.remove('k')
                bots.pop('k', None)
                typing.pop('k', None)

        elif e.type == pygame.TEXTINPUT and e.text in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') and len(typing['k'].replace(' ', '')) < 12 and 'k' in cells:
            typing['k'] += e.text + ' '

        elif e.type == pygame.JOYHATMOTION:
            if e.hat == 0 and len(typing[e.instance_id].replace(' ', '')) < 12:
                if e.value == (0, 1):
                    typing[e.instance_id] += '1 '
                elif e.value == (-1, 0):
                    typing[e.instance_id] += '2 '
                elif e.value == (0, -1):
                    typing[e.instance_id] += '3 '
                elif e.value == (1, 0):
                    typing[e.instance_id] += '4 '
        elif e.type == pygame.JOYBUTTONDOWN:
            if len(typing[e.instance_id].replace(' ', '')) < 12:
                if e.button == 4:
                    typing[e.instance_id] += '7 '
                elif e.button == 5:
                    typing[e.instance_id] += '8 '
                elif e.button == 3:
                    typing[e.instance_id] += '9 '
                elif e.button == 2:
                    typing[e.instance_id] += '0 '

            # Backspace
            if e.button == 1:
                typing[e.instance_id] = typing[e.instance_id][:-2]

            # Enter
            elif e.button == 7:
                if len(typing[e.instance_id].replace(' ', '')) == 12 and 'k' not in bots:
                    text = typing[e.instance_id].replace(' ', '')
                    ip: str = text[:3].lstrip('0') + '.' + text[3:6].lstrip('0') + \
                        '.' + text[6:9].lstrip('0') + \
                        '.' + text[9:12].lstrip('0')
                    while ip.find('..') != -1:
                        ip = ip[:ip.find('..') + 1] + '0' + \
                            ip[ip.find('..') + 1:]
                    try:
                        bots[e.instance_id] = Bot(ip)
                    except OSError:
                        notifier.show_toast(
                            "IP Not Found", f"Couldn't find a robot with that IP ({ip}). Are you in the same LAN as it?", threaded=True)

        # Update player list dynamically
        elif e.type == pygame.JOYDEVICEADDED and len(cells) < 8:
            joystick = pygame.joystick.Joystick(e.device_index)
            joysticks[joystick.get_instance_id()] = joystick
            cells.append(joystick.get_instance_id())
            typing[joystick.get_instance_id()] = ''
        elif e.type == pygame.JOYDEVICEREMOVED:
            del joysticks[e.instance_id]
            bots.pop(e.instance_id, None)
            cells.remove(e.instance_id)

        elif e.type == pygame.QUIT:  # Close cleanly
            running = False

    for bot in bots.values():
        bot.drive()

    screen.fill((255, 255, 255))

    pygame.draw.line(screen, (0, 0, 0), (200, 0), (200, 450), 2)
    pygame.draw.line(screen, (0, 0, 0), (400, 0), (400, 450), 2)
    pygame.draw.line(screen, (0, 0, 0), (600, 0), (600, 450), 2)
    pygame.draw.line(screen, (0, 0, 0), (0, 225), (800, 225), 2)
    pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(
        0, 0, 800, 450), 2, border_bottom_left_radius=5, border_bottom_right_radius=5)

    font = pygame.font.Font(None, 18)
    small_font = pygame.font.Font(None, 14)

    # Coords of index # cell
    cell_coords: tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int],
                       tuple[int, int], tuple[int, int]] = ((5, 25), (205, 25), (405, 25), (605, 25), (5, 250), (205, 250), (405, 250), (605, 250))

    for cell in cells:
        cell_name = "Keyboard" if cell == 'k' else "Controller #" + \
            str(cell + 1)  # type: ignore
        screen.blit(font.render(cell_name, True, (0, 0, 0)),
                    cell_coords[cells.index(cell)])

        if bots.get(cell) is None:
            screen.blits(((font.render("Enter IP", True, (0, 0, 0)), (cell_coords[cells.index(cell)][0], cell_coords[cells.index(cell)][1] + font.get_linesize())), (font.render(
                "_ _ _._ _ _._ _ _._ _ _", True, (0, 0, 0)), (cell_coords[cells.index(cell)][0], cell_coords[cells.index(cell)][1] + 2 * font.get_linesize() + 2)),
                (font.render(typing[cell], True, (0, 0, 0)), (cell_coords[cells.index(cell)][0], cell_coords[cells.index(cell)][1] + 2 * font.get_linesize()))))
            if cell_name != "Keyboard":
                screen.blit(controller_typing, (cell_coords[cells.index(
                    cell)][0] + 25, cell_coords[cells.index(cell)][1] + 4 * font.get_linesize()))
        else:
            bots.get(cell)

    for i in range(len(cells), 8):
        k = "or press Enter/Return on your keyboard" if 'k' not in cells else ""
        screen.blits(((small_font.render("Connect another controller", True, (0, 0, 0)), cell_coords[i]), (small_font.render(
            k, True, (0, 0, 0)), (cell_coords[i][0], cell_coords[i][1] + small_font.get_linesize()))))

    pygame.display.flip()

for c in joysticks.values():  # type: ignore
    c.quit()
pygame.joystick.quit()
pygame.quit()

@xboxone08
Copy link
Author

I removed the event-polling for loop with a different construct and the error went away.

@StellaHuang95
Copy link
Contributor

Thanks, I am able to repro the issue with the code you provided. Might need to transfer to pyright repo.

@erictraut
Copy link
Contributor

Please try switching to the pylance insiders release. I'm pretty sure this crash is explained by a bug that I introduced in pyright 1.1.315 and later fixed in pyright 1.1.316.

@debonte, if you think it's worth cherry picking the fix to publish a hot fix for the pylance production release, it involves two commits: c8a25b2d6141a23907e3c6c0f188ec45122ec421 and 1340dfac2239b9bb086780bfdb20dee9bd791367.

@debonte
Copy link
Contributor

debonte commented Jun 28, 2023

Those commits are included in the latest Pylance pre-release build -- 2023.6.41. This issue is only impacting ~30 users at the moment, so hotfixing stable to include the fix seems unnecessary.

@xboxone08, can you upgrade to 2023.6.41 and see if the issue is fixed?

@xboxone08
Copy link
Author

Yes the error is fixed 👍

@debonte debonte closed this as completed Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants