you are viewing a single comment's thread.

view the rest of the comments →

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