-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[BEAM-9296] Clean up and add type-hints to SDF API #10935
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,9 @@ | |
| from apache_beam.utils.windowed_value import WindowedValue | ||
|
|
||
| if TYPE_CHECKING: | ||
| from apache_beam.io.iobase import RestrictionProgress | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our linters do not enforce alphanumeric order for modules inside the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks for mentioning that! |
||
| from apache_beam.io.iobase import RestrictionTracker | ||
| from apache_beam.io.iobase import WatermarkEstimator | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -113,6 +115,7 @@ def check_done(self): | |
| return self._restriction_tracker.check_done() | ||
|
|
||
| def current_progress(self): | ||
| # type: () -> RestrictionProgress | ||
| with self._lock: | ||
| return self._restriction_tracker.current_progress() | ||
|
|
||
|
|
@@ -158,6 +161,7 @@ class RestrictionTrackerView(object): | |
| restriction_tracker. | ||
| """ | ||
| def __init__(self, threadsafe_restriction_tracker): | ||
| # type: (ThreadsafeRestrictionTracker) -> None | ||
| if not isinstance(threadsafe_restriction_tracker, | ||
| ThreadsafeRestrictionTracker): | ||
| raise ValueError( | ||
|
|
@@ -180,6 +184,7 @@ class ThreadsafeWatermarkEstimator(object): | |
| mechanism to guarantee multi-thread safety. | ||
| """ | ||
| def __init__(self, watermark_estimator): | ||
| # type: (WatermarkEstimator) -> None | ||
| from apache_beam.io.iobase import WatermarkEstimator | ||
| if not isinstance(watermark_estimator, WatermarkEstimator): | ||
| raise ValueError('Initializing Threadsafe requires a WatermarkEstimator') | ||
|
|
@@ -200,19 +205,13 @@ def get_estimator_state(self): | |
| with self._lock: | ||
| return self._watermark_estimator.get_estimator_state() | ||
|
|
||
| def current_watermark_with_lock(self): | ||
| # The caller should hold the lock before entering this function. | ||
| if not self._lock.locked(): | ||
| raise RuntimeError( | ||
| 'Expected lock to be held to guarantee thread-safe ' | ||
| 'access.') | ||
| return self._watermark_estimator.current_watermark() | ||
|
|
||
| def current_watermark(self): | ||
| # type: () -> Timestamp | ||
| with self._lock: | ||
| return self.current_watermark_with_lock() | ||
| return self._watermark_estimator.current_watermark() | ||
|
|
||
| def observe_timestamp(self, timestamp): | ||
| # type: (Timestamp) -> None | ||
| if not isinstance(timestamp, Timestamp): | ||
| raise ValueError( | ||
| 'Input of observe_timestamp should be a Timestamp ' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slight refactor here will avoid introducing a new mypy error:
This avoids having to declare the
restriction_codervariable asOptional[TupleCoder].There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I think the return type should still be
Optional[TupleCoder]given that it also returnsNone.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct. my comment about avoiding the declaration of
Optional[TupleCoder]refers to the variable (which my edit does away with), not the the return type.