Skip to content

fix!: Map int enums and literals to the smallest fitting int, fix fractional integer bounds and bare typing.List - #151

Merged
FBruzzesi merged 9 commits into
mainfrom
fix/dtype-inference
Nov 21, 2026
Merged

FBruzzesi merged 9 commits into
mainfrom
fix/dtype-inference

Conversation

@FBruzzesi

Copy link
Copy Markdown
Owner

Summary

  • Enum and Literal dtypes now follow their values. Before, every enum and literal became nw.Enum, which only takes strings. An IntEnum or Literal[1, 2] gave a schema that failed later: to_polars() raised TypeError and to_duckdb() raised AttributeError.
    • String values give Enum.
    • Int values give the smallest int dtype that fits them (IntEnum 200/404 gives UInt16).
    • Values of any other single type give that type's dtype (Literal[True, False] gives Boolean, a float enum gives Float64).
    • Flag / IntFlag give Int64, because their members combine into values that are not listed.
    • None in a Literal makes the field nullable, as Optional does, unless nullable is set explicitly.
  • Bare typing.List, typing.Sequence and typing.Tuple give List(Object) instead of raising IndexError. Unsupported generics such as set[Foo] are skipped before their arguments are parsed, so the error names set and not Foo.
  • Integer bounds round with floor/ceil instead of truncating. Annotated[int, Gt(-1.5)] allows -1, so it now gives a signed dtype instead of UInt64. Bounds can be int, float, Decimal or Fraction.
  • Integer range selection moves to _utils.int_dtype_from_bounds, which both the enum code and the annotated-types code use.

Breaking changes

  • IntEnum, Literal[1, 2] and plain enums with int values (including auto()) give the smallest fitting int dtype instead of Enum.
  • Other non-string values give their own dtype (Literal[True, False] gives Boolean).
  • Flag / IntFlag give Int64.
  • Mixed value types (Literal["a", 1]), Literal[None] and enums without members raise UnsupportedDTypeError. To keep such a field, set {"anyschema": {"dtype": ...}}.
  • Literal[..., None] makes the field nullable.
  • Fractional integer bounds can give a different int dtype.

Known limits

@FBruzzesi FBruzzesi added the fix label Nov 21, 2026
@FBruzzesi
FBruzzesi merged commit db95999 into main Nov 21, 2026
13 checks passed
@FBruzzesi
FBruzzesi deleted the fix/dtype-inference branch November 21, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant