7

I have a dictionary for which the key "name" is initialized to None (as this be easily used in if name: blocks) if a name is read in it is then assigned to name.

All of this works fine but Pycharm throws a warning when "name" is changed due to the change in type. While this isn't the end of the world it's a pain for debugging (and could be a pain for maintaining the code). Does anyone know if there is a way either to provide something akin to a type hint to the dictionary or failing that to tell Pycharm the change of type is intended?

code replicating issue:

from copy import deepcopy

test = {
    "name": None,
    "other_variables": "Something"
}


def read_info():
    test_2 = deepcopy(test)
    test_2["name"] = "this is the name"  # Pycharm shows warning
    return test_2["name"]

ideal solution:

from copy import deepcopy

test = {
    "name": None type=str,
    "other_variables": "Something"
}


def read_info():
    test_2 = deepcopy(test)
    test_2["name"] = "this is the name"  # no warning
    return test_2["name"]

Note: I know that setting the default value to "" would behave the same but a) it's handy having it print out "None" if name is printed before assignment and b) I find it slightly more readable to have None instead of "".

Note_2: I am unaware why (it may be a bug or intended for some reason I don't understand) but Pycharm only gives a wanrning if the code shown above is found within a function. i.e. replacing the read_info() function with the lines:

test_2 = deepcopy(test)
test_2["name"] = "this is the name"  # Pycharm shows warning

Does not give a warning

3
  • 1
    btw, it should be if name is not None: (or rather the opposite for your case if name is None:) since it's None, it's conventional to check the type this way (when using None) instead of using boolean logic
    – Matiiss
    Jul 27, 2022 at 11:24
  • You might be looking for TypedDict Jul 27, 2022 at 11:27
  • 2
    you can't easily annotate type of individual values in a dict (except going to a docs.python.org/3/library/typing.html#typing.TypedDict which makes the dict structure more rigid) ... if all the values are str you could annotate the whole dict as test: dict[str, Optional[str]] = { ... } to allow some values to be None
    – Anentropic
    Jul 27, 2022 at 11:28

1 Answer 1

3

Type hinting that dictionary with dict[str, None | str] (Python 3.10+, older versions need to use typing.Dict[str, typing.Optional[str]]) seems to fix this:

from copy import deepcopy

test: dict[str, None | str] = {
    "name": None,
    "other_variables": "Something"
}


def read_info():
    test_2 = deepcopy(test)
    test_2["name"] = "this is the name"  # no warning
    return test_2["name"]

As noticed by @Tomerikoo simply type hinting as dict also works (this should work on all? Python versions that support type hints too):

test: dict = {
    "name": None,
    "other_variables": "Something"
}
8
  • I think that may be a 3.10 thing as I'm running 3.9 and your code throws an error but I will check out typed dicts, thanks
    – Pioneer_11
    Jul 27, 2022 at 11:54
  • Hey can you please explain what does the syntax dict[str, None | str] mean? Jul 27, 2022 at 11:54
  • oh, I was wondering when those cool type hints were introduced, since I never have used 3.9 (used 3.8 before switching to 3.10)
    – Matiiss
    Jul 27, 2022 at 11:55
  • 1
    @OmarAlSuwaidi It's a type hint saying the object is a dict, with str typed keys, and values that can be either None or str
    – Tomerikoo
    Jul 27, 2022 at 11:55
  • 1
    Even more interesting: simply typing as test: dict removes the warning...
    – Tomerikoo
    Jul 27, 2022 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.