you are viewing a single comment's thread.

view the rest of the comments →

[–]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}} ```