-
Notifications
You must be signed in to change notification settings - Fork 196
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 to list, tuple, set, frozenset coersion #191
Comments
Looks like something I would be interested in handling @samuelcolvin |
Question though, sure a little bit of information seems to be lost when converting a generator to a set(order), but the order is very rarely considered when working with generators, as for the most part, you just need a one-time iterable. So is there really any reason(argument) for refusing coercing to frozenset/sets |
Thanks, yes please. I think order is definitely considered when working with generators quite often, but since I'm proposing lists can be converted to sets, I guess it also makes sense to allow generators. Still would love input from others. The only thing here that I'm sure about is preventing sets from being converted to lists/tuples. |
Really? I would have said the opposite. Personally I almost always consider the order.
Yep completely agree. If people want to force coercion, they can always use a validator.
Hmmm yeah I guess it makes sense with json parsing for example. But I would have understood if coercion was only done if
Yeah If we had an |
Well, we can cover JSON parsing separately because of the JSON I think we need to either add two clauses to the cod-philosophy theory of pydantic type coercion:
|
|
@maestro-1 see here, if you want to contribute to pydantic it would be worthwhile reading that whole article. |
@samuelcolvin Already gone through it. Still have it open even for when I need to reference anything |
I didn't mean to cover JSON parsing separately. Better to have one rule for all.
👍 As long as the rule is clear and explicit it's great |
In fact the sentence @samuelcolvin I opened #208 |
Stop coercing
set
/frozenset
tolist
/tuple
?Although this is not "loosing information", the result is not deterministic/repeatable.
E.g. if you have the Field
Tuple[PositiveInt, NegativeInt, str]
then the input set{1, -1, 'a'}
will work sometimes, and fail sometimes - this is pretty confusing.I think we should change this.
Stop coercing
list
/tuple
toset
/frozenset
?Should we allow coercing a list to a set? In this case we are "loosing information" (e.g. order), however creating a set from a list is often desired - e.g. when parsing a format (yaml, toml etc.) that only has a list type.
I think we should not change this
Add coercing
dict_key
toset
/frozenset
?Not that common, but we have it now and I think it kind of makes sense since
dict_key
"feels like" (sorry to be fluffy) aset
.I guess since
dict_key
are ordered, it should be fine to coerce them tolist
andtuple
too.I guess as with currently, we should allow
dict_values
to all these types too?I think we should change this.
Generators?
In pydantic V1 we allow converting a generator to any of these types.
I think we should allow converting a generator to a list or tuple, but not set or frozenset.
@PrettyWood @tiangolo thoughts?
The text was updated successfully, but these errors were encountered: