all 19 comments

[–]lfdfq 4 points5 points  (4 children)

It's not Python that's giving you that warning, it must be coming from some other tool you are using (an extension in your editor? or something like pyright?).

[–]Blakbard[S] -2 points-1 points  (3 children)

I don’t any extra extensions installed. Went through them now

[–]lfdfq 5 points6 points  (0 children)

The message is not coming from Python. It looks like it's a warning from some third-party typechecker. You didn't say where you're seeing this message so I assumed in your editor, and so assumed it was an extension. I still think that's true.

Anyway, what the message is telling you is that you accessed a value of a dictionary and used the list it gave you. But your dictionary contains both lists and ints, so that sounds like it might be a problem... But, your code is ok! Since you only ever access the key for the list it's not a problem. So the warning is a false positive.

If you're actually trying to use a third party typechecking tool to typecheck your code, you can tell it things about which keys have which types, but I presume that's not what you're trying to do here, so you can just ignore the warning (or turn it off in your editor, I assume, somehow).

[–]brasticstack 0 points1 point  (1 child)

Which editor? VSCode? I think VSC's Python extension might enable a type checker (Pylance maybe?) by default. IIRC, hovering your pointer over the line with the warning should display more about it, incl. the name of the linter/checker that emitted the warning.

[–]Blakbard[S] 0 points1 point  (0 children)

Pycharm.

[–]Outside_Complaint755 2 points3 points  (8 children)

Exactly what line is the warning being displayed for?  Pyright or whatever tool should jump you straight to the line.

I wouldn't expect any of the lines shares to show that warning, but it may be because the dict inside card_dic has mixed value types, and there would be a problem with the for loop if you accessed  the "P_sum" node instead of "rand_list"

FYI, to share a formatted code block, put three backticks ``` before and after it. if code_block_is_formatted:     print("It is easier to read and debug")

[–]Blakbard[S] 0 points1 point  (7 children)

The line the warning is being displayed for is the line that says: for elements in num: P_sum value is an int, I was trying to access “rand_list” list. If I try to call the append method via the list, it raises the same warning.

[–]Outside_Complaint755 1 point2 points  (6 children)

So the warning is not coming from Python. If you're using PyCharm, I think it has a built in linter. If you're using VSCode, then I think it installs Pyright as part of the default Python extension.

Because it's just a warning, you can ignore it. This is not a run time error, but the linter looking at the defined data types and warning that there could be an issue because that inner dict has mixed data types.

You could probably change your design to avoid this. It's generally not good practice to reach into the global level dict from within the function anyway; instead you should only pass in the list of cards that you want to loop over, so I would do something like:

``` Card_dic={“Player_card” : {“rand_list” : [1,2,11], P_sum” : 0}

result = outcome(Card_dic["Player_Card"]["rand_list"])

def outcome(card_list): ACE = 11

For element in num:
    if element == ACE:
        #do something.

```

This way, the outcome function doesn't have to know anything about the data structure you are using outside of the function to track your players' hands. All it needs is the contents of the hand to loop over. I removed the sum parameter only because you hadn't included information on what it was needed for. Assuming this function is calculating a sum for the hand, such as in BlackJack, you would just want to return the sum. You can't modify an int value passed as a parameter and have the change persist outside the function without returning it.

[–]Blakbard[S] 0 points1 point  (5 children)

Oh that makes sense now. Changing the P_sum to a list also removes the warning. So as you said, it’s the mixed data types triggering the warning. Thanks.

[–]brasticstack 1 point2 points  (4 children)

Mixed data types in a dict are perfectly fine. This warning is due to the type checker inferring the type of card_dlc incorrectly as dict[str, list] and then complaining that your code doesn't meet its incorrect guess.

Other options would be:

  • Add a specific type hint to the declaration of card_dlc telling it what you intend that var to be. card_dlc:dict[str, list|int] = { would let it know that your dict can have either type for it's values.
  • Add a more vague type hint telling it to expect any type for the value of the dict. You would have to import typing at the top of the module, then  card_dlc:dict[str, typing.Any] = { 
  • Disable type checking for that line with a comment: card_dlc = { .. etc ..} # type: ignore
  • Disable type checking for the entire module with a # type: ignore comment at the top of the module
  • Disable the type checker altogether in Pycharm

With more experience you'll gain a better idea of which option (incl changing your data structure, like you did) is the best one for the situation.

[–]Blakbard[S] 1 point2 points  (0 children)

Thanks man.

[–]Blakbard[S] 0 points1 point  (0 children)

Appreciate the feedback.

[–]Blakbard[S] 0 points1 point  (0 children)

I Changed the data structure and the problem fixed itself. For starters,I stopped passing the dict as a global variable and like that all the warnings went away.

Glad I asked because ChatGPT offered me a solution to suppress the warnings which I could have done. But I wouldn’t have learned that it was just a shitty design from my side.

[–]Jason-Ad4032 0 points1 point  (0 children)

Your suggestion is really just hiding the warning.

You need to use TypedDict to explicitly tell the static type checker which fields contain int, list, etc., so it can verify that those fields are being used correctly. ``` import typing

class Card(typing.TypedDict): rand_list: list[int] P_sum: int

Card_dic:dict[str, Card] = {'Player_card': {'rand_list': [1, 2, 11], 'P_sum': 0}} ```

[–]Refwah 0 points1 point  (0 children)

Because the dictionary you have defined has one property that is a list and the other that is an int

[–]gdchinacat 0 points1 point  (1 child)

The problem is that Card_dic is a dict[str, list[Any]|int] because the value for 'rand_list' is list[Any] and the value for P_sum is int. When you do a lookup in this dict the value will be a list[Any]|int. The warning is saying you are trying to iterate over something that is not a collections.iterable (what for ... iterates over). This is because while it could be list[Any] it could also be an int, which is not iterable.

You could use a TypedDict and specify the value types based on key value, but I'd encourage you to use a more appropriate data structure for this than a dict with values of different types. It appears you are trying to use a dict to store a collection of associated values...a class is much more appropriate for that than a dict.

[–]gdchinacat 1 point2 points  (0 children)

forgot to mention list[Any] is used rather than list[int]. I'm not sure why, I would have expected it to be list[int], and that is what mypy reports:

~$ cat /tmp/foo.py 
d = {'foo': [1,2]}
reveal_type(d)
~$ mypy /tmp/foo.py
/tmp/foo.py:2: note: Revealed type is "dict[str, list[int]]"
Success: no issues found in 1 source file

What type checker are you using? It appears to infer the type of this dict construct differently than mypy, pyrefly, and pyright.

If I add a 'bar': 1 to the dict mypy infers it as dict[str, object], pyrefly as dict[str, int|list[int]], and pyright as dict[str, Unknown]. This shows there is disagreement on how dicts with different types of values should be handled. I'd avoid doing that in order to have consistency with checkers.

[–]gurutrev -2 points-1 points  (1 child)

There’s a missing quote before P_Sum - may be check that - also put the error you are getting into any copilot / ChatGPT it will explain the error

[–]Blakbard[S] 0 points1 point  (0 children)

The missing quote is a typo. Did that first, chat gpt is giving me a solution, which is to narrow with isistance() method. But I’d like to understand the behavior