pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

`Sequence` Validator

Open samuelcolvin opened this issue 3 years ago • 11 comments

Like the other sequence like validators, but keep the type.

What do we do about str? I guess we have to allow it, but there are regularly scenarios where you want "any sequence other than a string".

samuelcolvin avatar Jun 28 '22 22:06 samuelcolvin

I reckon adding a field allow_string: bool with default true ?

PrettyWood avatar Jul 11 '22 23:07 PrettyWood

I'm amazed by what I wrote. I almost never want to allow str for something like this.

Up to you, but I would suggestion default false.

samuelcolvin avatar Jul 12 '22 05:07 samuelcolvin

I've thought about this and made a start on it in #182, but I don't actually think a Sequence type is very useful.

To match python's definition of Sequence (which it kind of has to do to make any sense) it needs to allow:

  • list
  • tuple
  • range
  • str
  • bytes
  • bytearray

But not allow (among other things):

  • set
  • frozenset

I don't think this type is actually very helpful and would be very complicated to implement correctly.

I'll leave this open to continue the discussion when someone (inevitably) demands this.

samuelcolvin avatar Jul 19 '22 15:07 samuelcolvin

@samuelcolvin maybe this calls for a new set of types that makes sense for Pydantic

caniko avatar Aug 23 '22 15:08 caniko

@caniko can you give an example of what you mean?

samuelcolvin avatar Aug 23 '22 16:08 samuelcolvin

I feel like I might not be invested enough to the discussion, but from what I gather the point of using Sequence in the first place is to loosely accept classes that fit the category. Sequence, however, is not designed for pydantic, which is why it doesn't make sense here.

Hence my idea to implement a Sequence-like type, and other types that makes sense, for Pydantic.

caniko avatar Aug 23 '22 17:08 caniko

We could do, that but really that would effectively be a union of list | tool | set or whatever. In which situation, it would be better to be explicit and just use that union.

it's not quite the same as the union because it would be marginally faster to have an "any of these list-like types" type than use the standard union syntax, but I'm not sure, given how much it would be used, that it would be worth ti.

samuelcolvin avatar Aug 23 '22 17:08 samuelcolvin

I think those using Sequence would appreciate it :sweat_smile:

FYI: I use it, but not with pydantic

caniko avatar Aug 23 '22 17:08 caniko

it

I'm not very clear, appreciate what?

I find it hard to imagine many scenarios where I would really want Sequence[T].

samuelcolvin avatar Aug 23 '22 17:08 samuelcolvin

I'm not very clear, appreciate what?

Appreciate adding something that is homologous to Sequence designed for pydantic.

I find it hard to imagine many scenarios where I would really want Sequence[T].

What about those few scenarios?

This is not my fight, I'll let someone with a real problem pick up the Yea side arguments from here.

Opinion: I am sure there is some niche use of these loose types for pydantic; I haven't stumbled over this yet. Moreover, typing.Sequence is also quite niche IMHO.

caniko avatar Aug 24 '22 08:08 caniko