All Questions
Tagged with mypy dictionary
21 questions
0
votes
1
answer
125
views
Python: return type hint of arbitrary depth dictionary
I am using a defaultdict collection to easily build an arbitrary depth python dictionary as follows:
from collections import defaultdict
from datetime import datetime
def recursive_dict() -> ...
2
votes
0
answers
541
views
Infer type hints for dictionary structure from class attributes definition
I'm trying to figure out a way to create precise types for a function that returns class attributes as an immutable dictionary.
For the sake of example, let's say we have a foreign class called "...
1
vote
1
answer
1k
views
mypy "Incompatible return value type" with dict comprehension and isinstance
I have an issue with mypy giving the following error:
Incompatible return value type (got "Dict[str, Pin]", expected "Union[Dict[str, Input], Dict[str, Output]]") [return-value]...
0
votes
2
answers
4k
views
Mypy error Item "None" of "Optional[Dict[str, str]]" has no attribute "get"
The below code runs fine -
from typing import List, Dict, Any
def func1(input_list: List[Dict[str, Dict[str, str]]],):
for config_dict in input_list:
table_1 = ".".join(
...
2
votes
1
answer
568
views
How to annotate `dictview` type to satisfy mypy?
Let's consider an instance check for dict_keys type :
dict_keys = type({}.keys())
print(dict_keys) # <class 'dict_keys'>
assert isinstance({}.keys(), dict_keys)
According to Python ...
0
votes
1
answer
136
views
How to append a list of dictionary inside of a dictionary
I have an issue that I am scratching my head over.
I have dictionary as follow:
my_dict = {'key': ['string', [{'id': 'id_value', 'number' : 'number_value'}]]}
and in the processing of this dictionary ...
0
votes
1
answer
565
views
Python typed dict with 1 type A value, and all others values of type B?
After looking at the TypedDictionary documentation of MyPy, I learned one can specify the types of values of specific keys with:
from typing_extensions import TypedDict
Movie = TypedDict('Movie', {'...
1
vote
2
answers
897
views
Override __hash__ in python dict mypy error
I am writing a class that represents a hashable dictionary in Python, following what has been suggested in here: hashable dict. I am doing this because I need it to be hashable for other ...
4
votes
1
answer
939
views
python dict where key and value depends the same generic type
I want to define a dict whose key and value are share the same generic type and has some constraint with it. The following is the example of such a situation. However, applying mypy to the following ...
1
vote
1
answer
2k
views
A dictionary with a given key type cannot be assigned as a dictionary of objects
It looks like a dictionary of SomeKeyType cannot be assigned as a dictionary of objects, even though all types are subtypes of object:
x: dict[str, str] = {"a": "b"}
y: dict[object,...
1
vote
1
answer
4k
views
How do I type annotate using any type as a key in a Python dictionary?
In Python, types are first class objects. As such, I can create a dictionary with types as the keys. For example:
mydict = {
int: "Hello!",
str: 12,
SomeClassName: "Hello ...
0
votes
1
answer
2k
views
mypy doesn't recognize Dictionary of Dictionary
I'm trying to check my type annotations using mypy, but this error keeps ocurring:
Script.py:201: error: Item "Dict[str, Union[float, int]]" of "Union[Dict[str, Union[float, int]], str, ...
6
votes
1
answer
800
views
Why can a Final dictionary not be used as a literal in TypedDict?
I am trying to accomplish the below (see mypy playground):
from typing import TypedDict, Final
account_schema: Final = {"name": str, "email": str}
Account = TypedDict("...
4
votes
1
answer
2k
views
Function for getting type of TypedDict value that works with static type checker
I am looking to make a Python (3.8+) function that is:
Input: key of a TypedDict
Output:
Returns the value (easy)
Is appropriately type hinted (where I am stuck)
Here is a code sample to help ...
1
vote
1
answer
3k
views
Python is using dict.update not type safe if passing in a TypedDict?
From reading through mypy issues, it seems as if calling dict.update(), and supplying a TypedDict is not type safe. This does not make sense to me.
My question is (particularly from the 2nd issue, ...