Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@zilto
Copy link
Collaborator

@zilto zilto commented Jul 16, 2025

This adds tab completion for dlt.Dataset and dlt.Relation. It respectively suggests table names and column names.

Related

I had to modify __getattr__ on ReadableDBAPIDataset class because it was breaking the _ipython_key_completions_. Its implementation effectively prevented any use of getattr() to dynamically retrieve attributes or methods by name. My fix involved manually searching the object's __dict__.

@netlify
Copy link

netlify bot commented Jul 16, 2025

Deploy Preview for dlt-hub-docs canceled.

Name Link
🔨 Latest commit 9bc32f2
🔍 Latest deploy log https://app.netlify.com/projects/dlt-hub-docs/deploys/687786aed10c770008c4a6ce

@zilto zilto self-assigned this Jul 16, 2025
@zilto zilto requested a review from sh-rp July 16, 2025 01:38
@zilto zilto added the enhancement New feature or request label Jul 16, 2025
rudolfix
rudolfix previously approved these changes Jul 16, 2025
Copy link
Collaborator

@rudolfix rudolfix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please read the comment because I added a few fixes.

this is working very well in Jupyter Notebook now and I'll try to merge this ASAP if tests will pass

raise NotImplementedError("Schema may not be set")

@property
def columns(self) -> list[str]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: this is part of implementation and not exposed to the user. we can keep it here but at some point we should decide which props to promote to public interface

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a purposeful addition. .columns is common across pd.DataFrame, pl.DataFrame, pyarrow.Table, etc.


def __getattr__(self, name: str) -> Any:
"""Retrieve a `Relation` via `__getitem__` if standard `__getattr__` returns `None`."""
attribute = self.__dict__.get(name, None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__getattr__ is called only when name is not in __dict__ so you have none here every time. Old implementation was not raising AttributeError but ValueError (via self.table) which IMO was the cause of your problems.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have a suggestion for the fix? Without the line I've added, the completion mechanism fails to call Dataset._ipython_key_completions_().

It has to do with getattr(dataset_obj, "_ipython_key_completions_") failing inside IPython (i.e., code we can't change). I haven't looked into it more

def columns(self) -> list[str]:
return list(self.schema.get("columns", {}).keys())

def _ipython_key_completions_(self) -> list[str]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above, only complete columns should be returned here.

Copy link
Collaborator Author

@zilto zilto Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we're retrieving columns mentioned on the relation, not physical columns. For example, if you have a SELECT foo AS bar FROM table, calling .columns on the relation should return bar despite not existing on table.

It's different from completion on dataset where you made a good point that only want actually available tables

@rudolfix rudolfix merged commit 21dc0bd into devel Jul 16, 2025
49 of 59 checks passed
@rudolfix rudolfix deleted the feat/autocompletion branch July 16, 2025 12:08
@zilto
Copy link
Collaborator Author

zilto commented Jul 16, 2025

...

@zilto zilto restored the feat/autocompletion branch July 16, 2025 12:53
AyushPatel101 pushed a commit to AyushPatel101/dlt that referenced this pull request Jul 17, 2025
* autocompletion added for dataset and relation

* fixed getattr

* raises correct exceptions in __getitem__ and __getattr__ in relation / dataset

* adds more tests to get notebook column completion

* shows only complete tables

---------

Co-authored-by: Marcin Rudolf <[email protected]>
zilto added a commit that referenced this pull request Jul 22, 2025
* autocompletion added for dataset and relation

* fixed getattr

* raises correct exceptions in __getitem__ and __getattr__ in relation / dataset

* adds more tests to get notebook column completion

* shows only complete tables

---------

Co-authored-by: Marcin Rudolf <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants