Reputation: 6181
I'm using python 3.6, and wanted to use typing, because it's nice, you get better linting and you can catch errors before runtime, also autocomplete will be available.
I have this method :
def add_to_dynamic_dict(filenames_by_satustag: Dict[str, List[str]], status_tag: str, filename: str) -> Dict[str, List[str]]:
So before calling it I created a dictionary like this :
filenames_by_satustag = Dict[str, List[str]]
But when running the script I get
TypeError: typing.Dict[str, typing.List[str]] is not a generic class
on that line.
But it is correct according to the documentation :
https://docs.python.org/3.6/library/typing.html (26.1.1 type aliases, second bloc)
What did I do wrong?
Thanks.
Upvotes: 0
Views: 629
Reputation: 2048
I think you are confused with type aliases. The method you declare expects a first argument, which you called filenames_by_satustag
with type Dict[str, List[str]]
. However, if I'm not mistaken, you call your method with a variable filenames_by_satustag
which has value Dict[str, List[str]]
. Hence, its type is the type of Dict[str, List[str]]
, that is typing._GenericAlias
. If you want to create a new type called filenames_by_satustag
, you should do the following:
filenames_by_satustag = Dict[str, List[str]]
# The function return can also be replaced by filenames_bu_satustag
def add_to_dynamic_dict(some_variable: filenames_by_satustag, status_tag: str, filename: str) -> Dict[str, List[str]]:
If what you wanted, however, was not to create a type alias, but just to create a variable filenames_by_satustag
, you could do it like this:
filenames_by_satustag: Dict[str, List[str]] = {}
Upvotes: 2