Repository navigation
Fix type hint propagation in Latest, ApproximateUnique, and ApproximateQuantiles PerKey (#36775) - #41884
Fix type hint propagation in Latest, ApproximateUnique, and ApproximateQuantiles PerKey (#36775)#41884Pratik1326 wants to merge 2 commits into
Conversation
…teQuantiles PerKey (apache#36775)
There was a problem hiding this comment.
📋 Review Summary
This PR fixes type hint propagation for Latest.PerKey, Latest.Globally, ApproximateUnique.PerKey, and ApproximateQuantiles.PerKey (#36775) by supplying input type hints to inner ParDo transforms in Latest and defining CombineFn type hints on ApproximateUniqueCombineFn and ApproximateQuantilesCombineFn. The approach directly addresses the root cause where inner composite transforms lacked input type patterns for binding type variables.
🔍 General Feedback
- The fixes for
Latest.Globally,Latest.PerKey, andApproximateUniqueCombineFnare clean and well-tested. - In
ApproximateQuantilesCombineFn.default_type_hints, the combinedinput_batched=Trueandweighted=Truecase currently falls back toAny/List[Any], which still drops element type hints for typed(elements_batch, weights_batch)tuples and disables input type checking for that mode.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
| def default_type_hints(self): | ||
| t = typehints.TypeVariable('T') | ||
| if self._input_batched and self._spec.weighted: | ||
| input_type = Any |
There was a problem hiding this comment.
🟡 Medium: When both input_batched=True and weighted=True are set, setting input_type = Any and output_type = typehints.List[Any] causes ApproximateQuantiles.PerKey and ApproximateQuantiles.Globally to lose the element type T (inferring Tuple[K, List[Any]] / List[Any] instead of Tuple[K, List[T]] / List[T]) for the documented (elements_batch, weights_batch) tuple input format, and allows invalid non-batched value types (e.g., Tuple[str, int]) to bypass static type checking.
Using typehints.Union[typehints.Tuple[typehints.Iterable[t], typehints.Iterable[float]], typehints.List[typehints.Iterable[Any]]] with output_type = typehints.List[t] preserves T for typed (elements_batch, weights_batch) tuples while still accepting 2-element list batches (as used in test_batched_weighted_quantiles) and rejecting non-batched inputs.
|
Assigning reviewers: R: @tvalentyn for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR preserves the input value type T as List[T] in the output type hints for ApproximateQuantiles.Globally, ApproximateQuantiles.PerKey, Sample.FixedSizeGlobally, and Sample.FixedSizePerKey. It also replaces the manual _check_pcollection_kv_type helper in ApproximateQuantiles.PerKey with the @with_input_types(KV[K, T]) decorator.
🔍 General Feedback
- In
ApproximateQuantiles.PerKey, replacing_check_pcollection_kv_typewith@with_input_types(KV[K, T])drops the compatibility with un-typed/unknown element types (element_type is None) and stops catching non-KV inputs when--no_pipeline_type_checkis passed. See the inline comment for details. - The
List[T]output type hint propagation onApproximateQuantilesFn,SampleCombineFn, and theGlobally/PerKeytransforms is clean, accurate, and well-tested.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
💬 Inline Findings
sdks/python/apache_beam/transforms/stats.py(line 227): 🟡 Medium: Removing_check_pcollection_kv_type(pcoll)and relying solely on@with_input_types(KV[K, T])introduces two behavioral changes:
pcoll.element_type is None: Previously,_check_pcollection_kv_typeraised aTypeCheckErrorwhenpcoll.element_type != Anyand was not aTupleConstraint/UnionConstraint(including whenpcoll.element_type is None, which is expressly tested intest_per_key_type_hintsviapc.element_type = None). With only@with_input_types,PTransform.type_check_inputs_or_outputsskips checking wheneverpvalue_.element_type is None(if pvalue_.element_type is None: continue), andCombinePerKeysubsequently crashes withValueError: Input to CombinePerKey must be compatible with KV[Any, Any]. Found None.insidetypehints.trivial_inference.key_value_types.--no_pipeline_type_check: When pipeline type checking is disabled (pipeline._options.view_as(TypeOptions).pipeline_type_check == False),type_check_inputsis bypassed, so invalid non-KV inputs (likeint) will now raise an internalValueErrorfromkey_value_typesduringCombinePerKey.expandrather than aTypeCheckError.
If TypeCheckError is still desired in these scenarios, consider keeping _check_pcollection_kv_type(pcoll) in expand alongside the @with_input_types decorator.
Fixes #36775 where
PerKeytransforms incombinersandstatslost concrete element types (falling back toAnyor partial tuple types) when applied to typedPCollections.Summary of Changes
Latest.PerKey&Latest.Globally(sdks/python/apache_beam/transforms/combiners.py): Added input type hints on innerParDo(self.add_timestamp)transforms (tuple[K, V]forPerKeyandTforGlobally) and used consistentVinLatest.PerKey's inner output tuple type soLatest.PerKeypreserves concrete key/value element types.ApproximateUnique.PerKey(sdks/python/apache_beam/transforms/stats.py): Added@typehints.with_input_types(Any)and@typehints.with_output_types(int)onApproximateUniqueCombineFn.ApproximateQuantiles.PerKey(sdks/python/apache_beam/transforms/stats.py): ImplementedApproximateQuantilesCombineFn.default_type_hints()across standard, weighted, and batched modes soApproximateQuantiles.PerKeyinfers concrete tuple/list element types.combiners_test.py,stats_test.py): Added type-inference andTypeCheckErrorunit tests forLatest,ApproximateUnique.PerKey, andApproximateQuantiles.PerKey.Verification
yapf --diffacross modified files.pytestoncombiners_test.py,stats_test.py,ptransform_test.py, andtyped_pipeline_test.py(432 passed, 6 skipped).