Skip to content

read_sql_*() methods have incorrect typing for dtype argument. #53046

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

Closed
Dr-Irv opened this issue May 2, 2023 · 3 comments
Closed

read_sql_*() methods have incorrect typing for dtype argument. #53046

Dr-Irv opened this issue May 2, 2023 · 3 comments
Assignees
Labels
IO SQL to_sql, read_sql, read_sql_query Typing type annotations, mypy/pyright type checking

Comments

@Dr-Irv
Copy link
Contributor

Dr-Irv commented May 2, 2023

The read_sql() docs say for the new dtype argument:

dtype Type name or dict of columns
Data type for data or columns. E.g. np.float64 or {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}. The argument is ignored if a table is passed instead of a query.

But in the typing in the source code, the argument is DtypeArg | None which doesn't allow something like {"c": "str"} . The implementation just passes dtype to DataFrame.astype().

See pandas-dev/pandas-stubs#676

NOTE: In pandas-stubs we did work to create all the possible strings and dtypes for astype(), so maybe that should be copied over to the pandas source??

@Dr-Irv Dr-Irv added IO SQL to_sql, read_sql, read_sql_query Typing type annotations, mypy/pyright type checking labels May 2, 2023
@Dr-Irv
Copy link
Contributor Author

Dr-Irv commented May 4, 2023

For pandas, I think the solution is to redefine

DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]
to use Mapping instead of Dict

@teacherc
Copy link

take

@twoertwein
Copy link
Member

For pandas, I think the solution is to redefine

DtypeArg = Union[Dtype, Dict[Hashable, Dtype]]

to use Mapping instead of Dict

Closing this issue as the suggested change has been made:

DtypeArg = Union[Dtype, Mapping[Hashable, Dtype]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

No branches or pull requests

3 participants