Skip to content

Conversation

@babymastodon
Copy link

@babymastodon babymastodon commented Jan 21, 2023

What was changed

[Regression fix] Support string subtypes as function arguments in workflows, by handling explicitly in converter.value_to_type

Why?

When we upgraded from b0.1 to v1.0, we experienced a regression in the deserialization of string subtypes. Not sure exactly which diff introduced the bug. As a result, String subtypes were being deserialized as lists of characters, rather than strings.

Checklist

  1. Closes: N/A

  2. How was this tested: Unit test

  3. Any docs updates needed? Not sure

@CLAassistant
Copy link

CLAassistant commented Jan 21, 2023

CLA assistant check
All committers have signed the CLA.

raise TypeError(
f"Cannot convert to {hint}, value not a string, value is {type(value)}"
)
return hint(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrmm, what if they changed the constructor/initializer in the extension? What about extending all other built in types like int, dict, list, etc? Are we sure we want to support everyone's custom class just because it extends a built in we know? It might have just been a coincidence it happened to work before.

At the least, if we do support this, we should update the README explicitly stating we do.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's reasonable to do this for both string and int subtypes. There are two reasons:

  1. class ExampleEnum(str, Enum) was the recommended approach for making json-safe enums prior to the introduction of StrEnum in python 3.11, which is not necessarily deployed everywhere. (Note, by "json-safe", I mean that they are safe to use as keys in a Dict that is serialized to JSON). I've added another failing test case to this PR to illustrate this point.
  2. String subclass is a useful alternative to NewType('TypeName', str), in situations where you are working with libraries that don't play nice with NewType.

I don't really have a strong opinion about Dict/List subtypes. It might make sense to support defaultdict if it's not supported already.

Happy to make more changes if we can agree on the direction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Makes sense and looks good. Let's make a few more changes:

  • Support that int subtype
  • Add an example of a non-string enum failing (just to ensure going forward we don't work with those)
  • Add a bullet "Data Conversion" saying that we support str and int subtypes
  • Format the source

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will do that (though not likely this week).

@cretz
Copy link
Member

cretz commented Feb 17, 2023

@babymastodon - in #269 I added a way to customize JSON type conversion for any particular type including this one. Is that good enough to solve this issue? Can I close the PR?

@cretz
Copy link
Member

cretz commented May 2, 2023

Closing PR after no response. Feel free to reopen as necessary.

@cretz cretz closed this May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants