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

Incorrect reportOptionalSubscript when accessing List with checked length. #3077

Closed
NielsKorschinsky opened this issue Jul 20, 2022 · 11 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@NielsKorschinsky
Copy link

NielsKorschinsky commented Jul 20, 2022

Environment data

  • Language Server version: 2022.7.30
  • OS and version: win32 x64
  • Python version (and distribution if applicable, e.g. Anaconda):
  • python.analysis.indexing: null
  • python.analysis.typeCheckingMode: strict

Code Snippet

group_tags: Optional[List[str]] = ["hello", "\"anotherVal\""]
tag_dict: Optional[Dict[str, str]] = {"hello": "wantedId", "anotherVal": "wantedName"}
insert_tags: Dict[str, Optional[str]] = {}

if group_tags and tag_dict:
    if "something" in group_tags:
        pass
    else:
        insert_tags["id_value"] = tag_dict[group_tags[0].replace("\"", "")]
        if len(group_tags) > 1:
            insert_tags["name_value"] = tag_dict[group_tags[1].replace("\"", "")]

Error on the part group_tags (last line, accessing [1])

(parameter) group_tags: List[str]
Object of type "None" is not subscriptablePylance[reportOptionalSubscript](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)

Logs

I will add it if requested.

This setting cannot be applied in this workspace. It will be applied when you open the containing workspace folder directly.
@NielsKorschinsky
Copy link
Author

Deleting the replace removes this bug, though the marker is only on the group_tags-part.

@erictraut
Copy link
Contributor

I'm not able to repro the problem you're seeing. Can you please verify that the above code snippet is correct? It contains a bug on the second line. Even if I fix that bug, I don't see any errors reported by pylance.

@NielsKorschinsky
Copy link
Author

image

I just copy pasted the segment.
I only added the first three lines to show off the variable declaration.

Copied again from the linting:
insert_tags: Dict[str, Optional[str]]
tag_dict: Dict[str, str] | None
group_tags: List[str] | None

@NielsKorschinsky
Copy link
Author

NielsKorschinsky commented Jul 21, 2022

I have fixed the error in line 2 now, please try again to reproduce.

@erictraut
Copy link
Contributor

To confirm, you're able to see the error with the self-contained minimal code sample above? I don't see any error with this code. Please try pasting this code into its own ".py" file and make sure you can repro the problem there. If you can, we'll need to figure out what's different between your configuration and mine.

@judej judej added the waiting for user response Requires more information from user label Jul 21, 2022
@NielsKorschinsky
Copy link
Author

NielsKorschinsky commented Jul 21, 2022

I tried and I am not able to reproduce either.
I need to add a tuple-unpacking in a loop to reproduce it.

I cannot attach the file, therefore here is the full code 1-1:

from typing import Optional, List, Dict, Tuple, Any

group_tags: Optional[List[str]] = ["hello", "\"anotherVal\""]
insert_tags: Dict[str, Optional[str]] = {}

tag_data_tuple: List[Tuple[
                    Optional[Dict[str, str]],
                    List[Dict[str, Any]]]] = [
    ({"hello": "wantedId", "anotherVal": "wantedName"}, [{}])
]

for (tag_dict, data) in tag_data_tuple:

    if group_tags and tag_dict:
        if "something" in group_tags:
            pass
        else:
            insert_tags["id_value"] = tag_dict[group_tags[0].replace("\"", "")]
            if len(group_tags) > 1:
                insert_tags["name_value"] = tag_dict[group_tags[1].replace("\"", "")]

With this code the bug is reliable reproduced.
Sorry for the inconvenience.

@erictraut
Copy link
Contributor

OK, thanks. I'm able to repro the problem with that code sample.

This is a core type checker issue, so I've created a tracking bug in the pyright repo.

@erictraut erictraut added bug Something isn't working and removed waiting for user response Requires more information from user labels Jul 21, 2022
@NielsKorschinsky
Copy link
Author

Thanks - I am surprised, I though that pyright was integrated into pylance.

@erictraut
Copy link
Contributor

Yes, pyright is integrated into pylance. Or perhaps it would be more accurate to say that pylance is built on top of pyright, at least from an architectural layering perspective. All of the core type analysis logic is in pyright, and pylance builds features on top of that capability. This particular bug is in the pyright logic.

@erictraut
Copy link
Contributor

This bug will be fixed in the next release of pyright and will be picked up by pyright in some future release.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Jul 22, 2022
@heejaechang
Copy link
Contributor

This issue has been fixed in version 2022.7.42, which we've just released. You can find the changelog here: CHANGELOG.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants