Skip to content
Open
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
4 changes: 2 additions & 2 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,15 @@ def tracer(
finally:
self._tracer = prev_tracer

def __getitem__(self, table_name: str) -> Union["Table", "View"]:
def __getitem__(self, table_name: str) -> "Table":
"""
``db[table_name]`` returns a :class:`.Table` object for the table with the specified name.
If the table does not exist yet it will be created the first time data is inserted into it.

:param table_name: The name of the table
"""
if table_name in self.view_names():
return self.view(table_name)
return cast("Table", self.view(table_name))
return self.table(table_name)

def __repr__(self) -> str:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import typing

from sqlite_utils.db import Database, Table, View


def test_database_getitem_return_type_hint_is_table():
assert typing.get_type_hints(Database.__getitem__)["return"] is Table


def test_database_getitem_still_returns_view_for_views(fresh_db):
fresh_db["items"].insert({"name": "one"})
fresh_db.create_view("items_view", "select * from items")
assert isinstance(fresh_db["items_view"], View)