flytekit icon indicating copy to clipboard operation
flytekit copied to clipboard

explicitly handle integers for untyped dict type

Open cosmicBboy opened this issue 1 year ago • 2 comments

fixes #3158

Signed-off-by: Niels Bantilan [email protected]

TL;DR

Protobuf <> json will auto-cast integers to floats when serializing dict values when the type is annotated with a bare dict or typing.Dict annotation. This will break code that relies on precise types, such as sklearn, which explicitly checks the types of hyperparameter values in Estimator constructor.

Type

  • [X] Bug Fix
  • [ ] Feature
  • [ ] Plugin

Are all requirements met?

  • [X] Code completed
  • [X] Smoke tested
  • [X] Unit tests added
  • [ ] Code documentation added
  • [ ] Any pending items have an associated Issue

Complete description

Because the interchange between protobuf and json casts ints to floats, this PR introduces a workaround to handle converting floats to integers by parsing the protobug struct_pb2.Value with regex and inferring integers based on its __repr__, which actually preserves the integer representation, e.g. number_value: 100, but accessing the actual number_value attribute will still return a float assert value.number_value == 100.0.

See here in the protobuf codebase where ints and floats are treated the same.

Tracking Issue

https://github.com/flyteorg/flyte/issues/3158

Follow-up issue

NA

cosmicBboy avatar Feb 01 '23 01:02 cosmicBboy

Codecov Report

Merging #1428 (4c8cccf) into master (dee4804) will increase coverage by 0.02%. The diff coverage is 96.55%.

@@            Coverage Diff             @@
##           master    #1428      +/-   ##
==========================================
+ Coverage   69.45%   69.48%   +0.02%     
==========================================
  Files         305      305              
  Lines       28574    28595      +21     
  Branches     2700     2703       +3     
==========================================
+ Hits        19845    19868      +23     
+ Misses       8211     8209       -2     
  Partials      518      518              
Impacted Files Coverage Δ
flytekit/core/type_engine.py 60.28% <90.00%> (+0.48%) :arrow_up:
tests/flytekit/unit/core/test_local_cache.py 100.00% <100.00%> (ø)
tests/flytekit/unit/core/test_type_engine.py 98.61% <100.00%> (+0.02%) :arrow_up:
tests/flytekit/unit/core/test_type_hints.py 96.24% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Feb 01 '23 02:02 codecov[bot]

yeah, the regex approach doesn't actually work... will need to create some kind of special encoding of this in the flyte literal

cosmicBboy avatar Feb 01 '23 16:02 cosmicBboy