-
-
Notifications
You must be signed in to change notification settings - Fork 154
Permit covariance of key type in read_csv converters argument #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| List, | ||
| Mapping, | ||
| Tuple, | ||
| TypedDict, | ||
| TypeVar, | ||
| Union, | ||
| ) | ||
|
|
@@ -1278,6 +1279,46 @@ def test_read_csv() -> None: | |
| pd.DataFrame, | ||
| ) | ||
|
|
||
| # Allow a variety of dict types for the converters parameter | ||
| converters1 = {"A": lambda x: str, "B": lambda x: str} | ||
|
||
| check( | ||
| assert_type(pd.read_csv(path, converters=converters1), pd.DataFrame), | ||
| pd.DataFrame, | ||
| ) | ||
| converters2 = {"A": lambda x: str, "B": lambda x: float} | ||
| check( | ||
| assert_type(pd.read_csv(path, converters=converters2), pd.DataFrame), | ||
| pd.DataFrame, | ||
| ) | ||
| converters3 = {0: lambda x: str, 1: lambda x: str} | ||
| check( | ||
| assert_type(pd.read_csv(path, converters=converters3), pd.DataFrame), | ||
| pd.DataFrame, | ||
| ) | ||
| converters4 = {0: lambda x: str, 1: lambda x: float} | ||
| check( | ||
| assert_type(pd.read_csv(path, converters=converters4), pd.DataFrame), | ||
| pd.DataFrame, | ||
| ) | ||
| converters5: dict[int | str, Callable[[str], Any]] = { | ||
| 0: lambda x: str, | ||
| "A": lambda x: float, | ||
| } | ||
| check( | ||
| assert_type(pd.read_csv(path, converters=converters5), pd.DataFrame), | ||
| pd.DataFrame, | ||
| ) | ||
|
|
||
| class ReadCsvKwargs(TypedDict): | ||
| converters: dict[int, Callable[[str], Any]] | ||
|
|
||
| read_csv_kwargs: ReadCsvKwargs = {"converters": {0: int}} | ||
|
|
||
| check( | ||
| assert_type(pd.read_csv(path, **read_csv_kwargs), pd.DataFrame), | ||
| pd.DataFrame, | ||
| ) | ||
|
|
||
|
|
||
| def test_groupby_series_methods() -> None: | ||
| df = pd.DataFrame({"x": [1, 2, 2, 3, 3], "y": [10, 20, 30, 40, 50]}) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you try
Mapping[int | str, Callable[[str], Any]]instead of the union?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that first. But it seems
Mappingisn't covariant over the key, only the value: python/typing#445There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But mapping may work for that the
Type[str]issue since that is the value...will try thatI also wonder if creating a covariant
TypeVarand then aMapping[T_co, ...]will work...I will also try that.