Skip to content

Commit 5bf4b58

Browse files
authored
Validate all values for csv.QUOTE (#991)
* 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
1 parent 3299527 commit 5bf4b58

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas-stubs/_typing.pyi

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from collections.abc import (
99
)
1010
import datetime
1111
from os import PathLike
12+
import sys
1213
from typing import (
1314
Any,
1415
Literal,
@@ -738,7 +739,20 @@ JsonSeriesOrient: TypeAlias = Literal["split", "records", "index", "table"]
738739
TimestampConvention: TypeAlias = Literal["start", "end", "s", "e"]
739740

740741
CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"]
741-
CSVQuoting: TypeAlias = Literal[0, 1, 2, 3]
742+
# [pandas-dev/pandas-stubs/991]
743+
# Ref: https://github.com/python/cpython/blob/5a4fb7ea1c96f67dbb3df5d4ccaf3f66a1e19731/Modules/_csv.c#L88-L91
744+
# QUOTE_MINIMAL = 0
745+
# QUOTE_ALL = 1
746+
# QUOTE_NONNUMERIC = 2
747+
# QUOTE_NONE = 3
748+
# Added in 3.12:
749+
# QUOTE_STRINGS = 4
750+
# QUOTE_NOTNULL = 5
751+
CSVQuotingCompat: TypeAlias = Literal[0, 1, 2, 3]
752+
if sys.version_info < (3, 12):
753+
CSVQuoting: TypeAlias = CSVQuotingCompat
754+
else:
755+
CSVQuoting: TypeAlias = CSVQuotingCompat | Literal[4, 5]
742756

743757
HDFCompLib: TypeAlias = Literal["zlib", "lzo", "bzip2", "blosc"]
744758
ParquetEngine: TypeAlias = Literal["auto", "pyarrow", "fastparquet"]

tests/test_frame.py

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import itertools
1616
from pathlib import Path
1717
import string
18+
import sys
1819
from typing import (
1920
TYPE_CHECKING,
2021
Any,
@@ -172,6 +173,13 @@ def test_types_to_csv() -> None:
172173
# Testing support for binary file handles, added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html
173174
df.to_csv(io.BytesIO(), quoting=csv.QUOTE_ALL, encoding="utf-8", compression="gzip")
174175

176+
if sys.version_info >= (3, 12):
177+
with ensure_clean() as path:
178+
df.to_csv(path, quoting=csv.QUOTE_STRINGS)
179+
180+
with ensure_clean() as path:
181+
df.to_csv(path, quoting=csv.QUOTE_NOTNULL)
182+
175183

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

0 commit comments

Comments
 (0)