Skip to content

Validate all values for csv.QUOTE #991

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

Merged
merged 3 commits into from
Sep 7, 2024
Merged

Validate all values for csv.QUOTE #991

merged 3 commits into from
Sep 7, 2024

Conversation

refack
Copy link
Contributor

@refack refack commented Sep 3, 2024

4 and 5 added in 3.12 (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

  • Tests added: Please use assert_type() to assert the type of any return value

@refack
Copy link
Contributor Author

refack commented Sep 3, 2024

I didn't consider the fact that this will fail in python < 3.12 since the functionality is just not there...
I can put this test under a version check condition, but then should there be and if in the stub as well, one branch allowing literals up to 3 and the other up to 5?

@@ -738,7 +738,7 @@ 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]
CSVQuoting: TypeAlias = Literal[0, 1, 2, 3, 4, 5]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to use the references to csv.MINIMAL, csv.QUOTE_ALL, etc., You can do tests on the python version in a stub, e.g.,

if sys.version_info >= (3, 12):
    CSVQuoting: TypeAlias = Literal[csv.MINIMAL, #rest of values ]
else:
   CSVQuoting: TypeAlias = Literal[csv.MINIMAL, #rest of values]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Literal[csv.MINIMAL] fails static analysis since csv.MINIMAL is not a python language literal (in hindsight it's obviously a language module member), but rather a runtime int using the constant naming convention

image

AFAICT the best we can do is document the values at runtime...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so maybe we should use Union[csv.MINIMAL, csv.QUOTE, # rest of values] instead.

@refack
Copy link
Contributor Author

refack commented Sep 6, 2024

Sorry I'm both dyslexic and ESL 🤣
image

@refack
Copy link
Contributor Author

refack commented Sep 6, 2024

Can we depend on typeshed? They have a stub for the compiled _csv code
https://github.com/python/typeshed/pull/12332/files#diff-c58bcb0a60afcd3168bd383c480870da81f23b9cec15e0890cf77d2306323240
(Even they recently moved from Literal to Final)

Never mind, the type checkers still don't want to infer the Type at static time, and the only importable Type is just _QuotingType: TypeAlias = int
https://github.com/python/typeshed/blob/e8e9291c76f50c3bcde79e7bb61060f5c24c054e/stdlib/_csv.pyi#L17-L19

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Sep 7, 2024

Never mind, the type checkers still don't want to infer the Type at static time, and the only importable Type is just _QuotingType: TypeAlias = int
https://github.com/python/typeshed/blob/e8e9291c76f50c3bcde79e7bb61060f5c24c054e/stdlib/_csv.pyi#L17-L19

Yes, I see that. I tried some other ideas and they just didn't work.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @refack , especially for trying the various workarounds.

@Dr-Irv Dr-Irv merged commit 5bf4b58 into pandas-dev:main Sep 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants