Skip to content

Infer column dtypes with columns and schema_overrides - #5102

Open
tonyyuyiding wants to merge 2 commits into
facebook:mainfrom
tonyyuyiding:feat/read-csv-without-schema
Open

tonyyuyiding wants to merge 2 commits into
facebook:mainfrom
tonyyuyiding:feat/read-csv-without-schema

Conversation

@tonyyuyiding

Copy link
Copy Markdown
Contributor

Summary

Fixes #5036

  • I am a human, and I fully understand the code I am submitting.

Motivation

Originally, in pl.read_csv, Pyrefly only infers the column dtypes if schema is provided. However, in the case below, where we have both columns and schema_overrides, there is enough information to infer the precise schema.

from typing import reveal_type
import polars as pl

df = pl.read_csv(
    'data.csv',
    columns=['DATE', 'TT BUY'],
    schema_overrides={'DATE': pl.Datetime, 'TT BUY': pl.String},
)
reveal_type(df) # Opaque dataframe. Desired: DataFrame['DATE': Date, 'TT BUY': String]

With this PR, Pyrefly will try to infer the schema as precisely as possible with columns and schema_overrides, even though schema is not given.

Approach

The function polars_csv_options is used to parse the options of read_csv and scan_csv. In the old implementation, it assumed schema always exists and schema_overrides is either a sequence or absent, because the checker relied on schema to perform inference. To add the new feature, I have extended relevant enums and updated some old functions accordingly to handle more possible options. Then I added a new branch in infer_read_csv_schema to handle the cases where schema is missing, while the old branch was left unchanged.

Test Plan

Tests have been added in pyrefly/lib/test/polars/dataframe.rs.

@meta-cla meta-cla Bot added the cla signed label Oct 5, 2026
@github-actions github-actions Bot added the size/l label Oct 5, 2026
@meta-codesync

meta-codesync Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D123393717. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions github-actions Bot added size/l and removed size/l labels Oct 5, 2026
@tonyyuyiding
tonyyuyiding marked this pull request as ready for review October 5, 2026 10:11
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type inference in polars read_csv with schema-overrides but without schema

2 participants