0

I have the following piece of code:

for table in list(my_dict.keys()):
    if table in my_other_dict:
        for file in my_dict[table]:
            if file in my_other_dict[table]:
                my_dict[table].remove(file)
                if not my_dict[table]:
                    del my_dict[table]

But the second, third and fourth my_dict reference is underlined with the following error:

Subscript for class "dict" will generate runtime exception; enclose type annotation in quotes

The only more or less relevant internet source I can find about it is this post, but I do not understand how I can apply it to my case.

Indeed, when this little piece of code is executed, there is no runtime exception.

Moreover, the last my_dict reference is underlined with the following error message:

Generic class type cannot be deleted

This dictionary is built and returned by the following function (signature only, content is too big):

def meta_from_iso_to_utf(self, correct_meta_files):

How do I fix my code so that pylance stops complaining about these problems which do not seem to be problems as the code runs as expected ?


Edit 1 (to answer Frank's comment):

my_dict is defined like this:

some_dict, my_dict = object.meta_from_iso_to_utf(
    argument
)

In the method, the my_dict is defined like so in a loop:

my_dict[key].append[value[0]]

table is the variable containing the keys of my_dict during the loop, which is a string. This variable is used only in this loop.

Concerning my editor, it is visual studio code 1.60.1, with the Python extension from microsoft version v2021.9.1246542782

AcK
  • 2,063
  • 2
  • 20
  • 27
Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • How is `my_dict` defined? Which IDE are you using? Are first glance, this code seems perfectly fine. It almost seems like it thinks that table is a type rather than a variable. Is there any code near you that uses `table` as a type? – Frank Yellin Sep 22 '21 at 06:45
  • @FrankYellin Check edit 1 of the question. – Itération 122442 Sep 22 '21 at 07:03
  • Which version of pylance are you using? – wovano Sep 22 '21 at 07:39
  • Possibly related: https://github.com/microsoft/pylance-release/issues/1145 – wovano Sep 22 '21 at 07:43
  • 1
    I don't get these warning using the same setup and pylance v2021.9.2. Is it possible to create a [mre]? I found some possible relevant information, but it does not apply to the code you currently have in your question. The root cause may be in the part you left out though. – wovano Sep 22 '21 at 07:48
  • 1
    @wovano. I agree. It feels like the poster is including some library that defines `table` to be a class. That's what I'm guessing based on the error messages. Obviously the code is fine. – Frank Yellin Sep 22 '21 at 17:24

0 Answers0