Skip to content

Commit 2f39bc4

Browse files
committed
Improve typing for Database.__getitem__
1 parent 8d74ffc commit 2f39bc4

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

sqlite_utils/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,15 @@ def tracer(
445445
finally:
446446
self._tracer = prev_tracer
447447

448-
def __getitem__(self, table_name: str) -> Union["Table", "View"]:
448+
def __getitem__(self, table_name: str) -> "Table":
449449
"""
450450
``db[table_name]`` returns a :class:`.Table` object for the table with the specified name.
451451
If the table does not exist yet it will be created the first time data is inserted into it.
452452
453453
:param table_name: The name of the table
454454
"""
455455
if table_name in self.view_names():
456-
return self.view(table_name)
456+
return cast("Table", self.view(table_name))
457457
return self.table(table_name)
458458

459459
def __repr__(self) -> str:

tests/test_typing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import typing
2+
3+
from sqlite_utils.db import Database, Table, View
4+
5+
6+
def test_database_getitem_return_type_hint_is_table():
7+
assert typing.get_type_hints(Database.__getitem__)["return"] is Table
8+
9+
10+
def test_database_getitem_still_returns_view_for_views(fresh_db):
11+
fresh_db["items"].insert({"name": "one"})
12+
fresh_db.create_view("items_view", "select * from items")
13+
assert isinstance(fresh_db["items_view"], View)

0 commit comments

Comments
 (0)