Skip to content

Commit

Permalink
Validate all values for csv.QUOTE (pandas-dev#991)
Browse files Browse the repository at this point in the history
* Validate all values for csv.QUOTE

4,5 added in python/cpython/67230
ref: https://docs.python.org/3/library/csv.html#csv.QUOTE_NOTNULL
ref: https://github.com/python/cpython/blob/a8bc03696c7c2c03e1d580633151ec7b850366f3/Modules/_csv.c#L88-L106

* Do runtime version check

* PROPERLY sort imports LOL
  • Loading branch information
refack authored Sep 7, 2024
1 parent 3299527 commit 5bf4b58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from collections.abc import (
)
import datetime
from os import PathLike
import sys
from typing import (
Any,
Literal,
Expand Down Expand Up @@ -738,7 +739,20 @@ JsonSeriesOrient: TypeAlias = Literal["split", "records", "index", "table"]
TimestampConvention: TypeAlias = Literal["start", "end", "s", "e"]

CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"]
CSVQuoting: TypeAlias = Literal[0, 1, 2, 3]
# [pandas-dev/pandas-stubs/991]
# Ref: https://github.com/python/cpython/blob/5a4fb7ea1c96f67dbb3df5d4ccaf3f66a1e19731/Modules/_csv.c#L88-L91
# QUOTE_MINIMAL = 0
# QUOTE_ALL = 1
# QUOTE_NONNUMERIC = 2
# QUOTE_NONE = 3
# Added in 3.12:
# QUOTE_STRINGS = 4
# QUOTE_NOTNULL = 5
CSVQuotingCompat: TypeAlias = Literal[0, 1, 2, 3]
if sys.version_info < (3, 12):
CSVQuoting: TypeAlias = CSVQuotingCompat
else:
CSVQuoting: TypeAlias = CSVQuotingCompat | Literal[4, 5]

HDFCompLib: TypeAlias = Literal["zlib", "lzo", "bzip2", "blosc"]
ParquetEngine: TypeAlias = Literal["auto", "pyarrow", "fastparquet"]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import itertools
from pathlib import Path
import string
import sys
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -172,6 +173,13 @@ def test_types_to_csv() -> None:
# Testing support for binary file handles, added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
df.to_csv(io.BytesIO(), quoting=csv.QUOTE_ALL, encoding="utf-8", compression="gzip")

if sys.version_info >= (3, 12):
with ensure_clean() as path:
df.to_csv(path, quoting=csv.QUOTE_STRINGS)

with ensure_clean() as path:
df.to_csv(path, quoting=csv.QUOTE_NOTNULL)


def test_types_to_csv_when_path_passed() -> None:
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
Expand Down

0 comments on commit 5bf4b58

Please sign in to comment.