Skip to content
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

feat: Support top-level pl.col autocompletion for iPython #17080

Merged
merged 8 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion py-polars/docs/source/reference/expressions/col.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the class documentation below for examples and further documentation.
-----

.. currentmodule:: polars.functions.col
.. autoclass:: ColumnFactory
.. autoclass:: Col
:members: __call__, __getattr__
:noindex:
:autosummary:
Expand Down
42 changes: 10 additions & 32 deletions py-polars/polars/functions/col.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import contextlib
from typing import TYPE_CHECKING, Any, Iterable, Protocol, cast
from typing import TYPE_CHECKING, Any, Iterable

from polars._utils.wrap import wrap_expr
from polars.datatypes import is_polars_dtype
Expand Down Expand Up @@ -67,25 +67,7 @@ def _create_col(
raise TypeError(msg)


# appease lint by casting `col` with a protocol that conforms to the factory interface
class Column(Protocol):
def __call__(
self,
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr: ...

def __getattr__(self, name: str) -> Expr: ...


# handle attribute lookup on the metaclass (we use the factory uninstantiated)
class ColumnFactoryMeta(type):
def __getattr__(self, name: str) -> Expr:
return _create_col(name)


# factory that creates columns using `col("name")` or `col.name` syntax
class ColumnFactory(metaclass=ColumnFactoryMeta):
class Col:
"""
Create Polars column expressions.

Expand Down Expand Up @@ -142,8 +124,8 @@ class ColumnFactory(metaclass=ColumnFactoryMeta):
└─────┴─────┴─────┘
"""

def __new__( # type: ignore[misc]
cls,
def __call__(
self,
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr:
Expand Down Expand Up @@ -291,14 +273,6 @@ def __new__( # type: ignore[misc]
"""
return _create_col(name, *more_names)

# appease sphinx; we actually use '__new__'
def __call__(
self,
name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType],
*more_names: str | PolarsDataType,
) -> Expr:
return _create_col(name, *more_names)

def __getattr__(self, name: str) -> Expr:
"""
Create a column expression using attribute syntax.
Expand Down Expand Up @@ -331,7 +305,11 @@ def __getattr__(self, name: str) -> Expr:
│ 6 │
└─────┘
"""
return getattr(type(self), name)
# For autocomplete to work with iPython
if name.startswith("__wrapped__"):
return getattr(type(self), name)

return _create_col(name)


col = cast(Column, ColumnFactory)
col: Col = Col()