Skip to content

Commit

Permalink
Add namedtuple test
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jun 24, 2024
1 parent bbc9505 commit 62cc5d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/_utils/construction/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def _sequence_of_tuple_to_pydf(
orient: Orientation | None,
infer_schema_length: int | None,
) -> PyDataFrame:
# infer additional meta information if named tuple
# infer additional meta information if namedtuple
if is_namedtuple(first_element.__class__):
if schema is None:
schema = first_element._fields # type: ignore[attr-defined]
Expand Down
16 changes: 15 additions & 1 deletion py-polars/tests/unit/datatypes/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from datetime import date, datetime
from typing import TYPE_CHECKING, Any, ForwardRef, Optional, Union
from typing import TYPE_CHECKING, Any, ForwardRef, NamedTuple, Optional, Union

import pytest

Expand Down Expand Up @@ -108,3 +108,17 @@ def test_parse_union_type_into_dtype(input: Any, expected: PolarsDataType) -> No
def test_parse_union_type_into_dtype_invalid(input: Any) -> None:
with pytest.raises(TypeError):
_parse_union_type_into_dtype(input)


def test_parse_dtype_namedtuple_fields() -> None:
# Utilizes ForwardRef parsing

class MyTuple(NamedTuple):
a: str
b: int
c: str | None = None

schema = {c: parse_into_dtype(a) for c, a in MyTuple.__annotations__.items()}

expected = pl.Schema({"a": pl.String(), "b": pl.Int64(), "c": pl.String()})
assert schema == expected

0 comments on commit 62cc5d2

Please sign in to comment.