You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TYP: Correct type annotation for to_dict. (#55130)
* Correct type annotation for to_dict.
The `into` argument of DataFrame.to_dict and Series.to_dict can be
either a class or instance of a class of dict; this is covariant -
subclasses of dict can also be used. The argument was annotated as
`type[dict]` though, so type checkers marked passing initialized objects
(required for collections.defaultdict) as an incorrect argument type.
Fix by annotating `into` to take either a subclass of dict or an
initialized instance of a subclass of dict.
* Use generic MutableMapping type for to_dict method.
Unfortunately a generic type annotation with a default triggers an
existing mypy limitation (python/mypy#3737).
The current workaround is to use overloads and then not annotate the
implementation containing the default parameter; this still enables mypy
to deduce correct return types.
Two overloads are added for Series.to_dict, even though they could be
combined using a Union type, as at least two overloads are required for
a single method.
* Fix formatting
* return annotation for non-overload
* no keyword should return dict
* swap overload order to work for dict subclasses that are passed as keywords
* fix tests
---------
Co-authored-by: Torsten Wörtwein <[email protected]>
0 commit comments