Which is the correct type-hint for a byte encoded dictionary?

Viewed 1494

I have a dictionary were the keys are dictionaries encoded in bytestrings:

mydict = {
   b'{"key 1": 3, "key 2": 6}': 'my first value',
   b'{"key 1": 2, "key 2": 7}': 'my second value',
}

And I would like to know how to write the type hint.
In my head it would make sense something like:

from typing import Dict, ByteString

mydict: Dict[ByteString[Dict[str, int]], str] = {
   b'{"key 1": 3, "key 2": 6}': 'my first value',
   b'{"key 1": 2, "key 2": 7}': 'my second value',
}

But I get TypeError: typing.ByteString is not a generic class
Any suggestion?

1 Answers

It's simply Dict[Bytes, str] - Python doesn't understand the structure of the bytes.

In the same vein, the type of "6" is string, not "string that contains an integer", and "(1, 2)" is a simple string as well, not a string that contains a tuple of integers.

Maybe the closest thing to "typing a string" are template literal strings in TypeScript, but it's still not the same. A string is just a string - it can't be parametrised.

Related