pandas-dev / pandas-stubs

Public type stubs for pandas
BSD 3-Clause "New" or "Revised" License
228 stars 120 forks source link

Invalid typing for `df.to_csv` #962

Open JensHeinrich opened 1 month ago

JensHeinrich commented 1 month ago

There probably should be a switch like

if sys.version_info >= (3, 12):
    CSVQuoting: TypeAlias = Literal[0, 1, 2, 3, 4,5]
else:
    CSVQuoting: TypeAlias = Literal[0, 1, 2, 3]

at https://github.com/pandas-dev/pandas-stubs/blob/48ca4b0d70f0695da6d452f7e203f05ad983913a/pandas-stubs/_typing.pyi#L741

as QUOTE_STRINGS and QUOTE_NOTNULL options have been added in python 3.12

Dr-Irv commented 1 month ago

Sounds reasonable. Interestingly, typeshed for csv.writer() has this declaration for the quoting argument: quoting: _QuotingType. Then when defining QuotingType, there is this:

UOTE_ALL: Literal[1]
QUOTE_MINIMAL: Literal[0]
QUOTE_NONE: Literal[3]
QUOTE_NONNUMERIC: Literal[2]
if sys.version_info >= (3, 12):
    QUOTE_STRINGS: Literal[4]
    QUOTE_NOTNULL: Literal[5]

# Ideally this would be `QUOTE_ALL | QUOTE_MINIMAL | QUOTE_NONE | QUOTE_NONNUMERIC`
# However, using literals in situations like these can cause false-positives (see #7258)
_QuotingType: TypeAlias = int

That links to this issue: https://github.com/python/typeshed/issues/7258

Nevertheless, I'm open to a PR with your suggested changes, although we may want to consider referring to the constants in the csv package rather than the explicit numbers 0, 1, 2, 3, etc.