You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by fo...@apache.org on 2023/02/13 15:51:27 UTC
[iceberg] branch master updated: Python: TypeVar for bounding TableScan (#6819)
This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/master by this push:
new 96dc0c6a6e Python: TypeVar for bounding TableScan (#6819)
96dc0c6a6e is described below
commit 96dc0c6a6e2be2f32714acba94299511b8948e95
Author: Guillem Orellana Trullols <gu...@gmail.com>
AuthorDate: Mon Feb 13 16:51:19 2023 +0100
Python: TypeVar for bounding TableScan (#6819)
* Properly use TypeVar for bounding TableScan return
* Fix isort and mypy error
* Update __init__.py
---------
Co-authored-by: Fokko Driesprong <fo...@apache.org>
---
python/pyiceberg/table/__init__.py | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/python/pyiceberg/table/__init__.py b/python/pyiceberg/table/__init__.py
index dc9cc9e036..df2e09ab3a 100644
--- a/python/pyiceberg/table/__init__.py
+++ b/python/pyiceberg/table/__init__.py
@@ -26,7 +26,6 @@ from typing import (
Any,
Callable,
Dict,
- Generic,
Iterator,
List,
Optional,
@@ -101,7 +100,7 @@ class Table:
case_sensitive: bool = True,
snapshot_id: Optional[int] = None,
options: Properties = EMPTY_DICT,
- ) -> TableScan[Any]:
+ ) -> DataScan:
return DataScan(
table=self,
row_filter=row_filter,
@@ -174,9 +173,6 @@ class Table:
)
-S = TypeVar("S", bound="TableScan", covariant=True) # type: ignore
-
-
def _parse_row_filter(expr: Union[str, BooleanExpression]) -> BooleanExpression:
"""Accepts an expression in the form of a BooleanExpression or a string
@@ -190,7 +186,10 @@ def _parse_row_filter(expr: Union[str, BooleanExpression]) -> BooleanExpression:
return parser.parse(expr) if isinstance(expr, str) else expr
-class TableScan(Generic[S], ABC):
+S = TypeVar("S", bound="TableScan", covariant=True)
+
+
+class TableScan(ABC):
table: Table
row_filter: BooleanExpression
selected_fields: Tuple[str]
@@ -246,7 +245,7 @@ class TableScan(Generic[S], ABC):
"""Creates a copy of this table scan with updated fields."""
return type(self)(**{**self.__dict__, **overrides})
- def use_ref(self, name: str) -> S:
+ def use_ref(self: S, name: str) -> S:
if self.snapshot_id:
raise ValueError(f"Cannot override ref, already set snapshot id={self.snapshot_id}")
if snapshot := self.table.snapshot_by_name(name):
@@ -254,15 +253,15 @@ class TableScan(Generic[S], ABC):
raise ValueError(f"Cannot scan unknown ref={name}")
- def select(self, *field_names: str) -> S:
+ def select(self: S, *field_names: str) -> S:
if "*" in self.selected_fields:
return self.update(selected_fields=field_names)
return self.update(selected_fields=tuple(set(self.selected_fields).intersection(set(field_names))))
- def filter(self, expr: Union[str, BooleanExpression]) -> S:
+ def filter(self: S, expr: Union[str, BooleanExpression]) -> S:
return self.update(row_filter=And(self.row_filter, _parse_row_filter(expr)))
- def with_case_sensitive(self, case_sensitive: bool = True) -> S:
+ def with_case_sensitive(self: S, case_sensitive: bool = True) -> S:
return self.update(case_sensitive=case_sensitive)
@@ -299,7 +298,7 @@ def _open_manifest(io: FileIO, manifest: ManifestFile, partition_filter: Callabl
return [FileScanTask(file) for file in matching_partition_data_files]
-class DataScan(TableScan["DataScan"]):
+class DataScan(TableScan):
def __init__(
self,
table: Table,