1

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.

2
  • 1
    Can you post the full error and a minimal reproducible example of the code raising it? I am not getting any errors from that line... Commented Nov 25, 2020 at 14:38
  • filenames_by_satustag = Dict[str, List[str]] does not create a dictionary... Commented Nov 25, 2020 at 14:39

1 Answer 1

3

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]] = {}
Sign up to request clarification or add additional context in comments.

1 Comment

oooh, I get it now, what I did was say : now you can use filenames_by_satustag as a Dict[str, List[str]] type Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.