I want add a type annotation to the following function:
def dict_not_none(**kwargs: Any) -> ???:
return {k: v for k, v in kwargs.items() if v is not None}
Which allows static type checkers like mypy and pyright to understand the function, particularly in regards to TypedDict
s.
Using an example from some real code, I have:
class BoolSchema(TypedDict, total=False):
type: Required[Literal['bool']]
strict: bool
ref: str
extra: Any
def bool_schema(
strict: bool | None = None, ref: str | None = None
) -> BoolSchema:
return dict_not_none(type='bool', strict=strict, ref=ref)
It should be possible to make this typing safe, but I don’t know how?
I’d be willing to change dict_not_none
e.g. to take a dict as an argument if that can fix typing, or even remove the function and use a comprehension or similar if that would fix the problem.
(Currently I’m just returning setting the return type to Any
which avoids errors but effectively removes type checking)