Skip to content
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

Simplified length limited string #2104

Closed
samuelcolvin opened this issue Nov 9, 2020 · 5 comments
Closed

Simplified length limited string #2104

samuelcolvin opened this issue Nov 9, 2020 · 5 comments

Comments

@samuelcolvin
Copy link
Member

I often want constr(max_length=XXX, strip_whitespace=True). Could we add a shortcut for this that worked better with mypy?

E.g. something like

class Foobar(BaseModel):
    name: StrLimit[63]

This would be equivalent to constr(max_length=63, strip_whitespace=True), e.g a field with a max length of 63, no min limit, strip_whitespace enabled.

The name StrLimit is debatable. I think using ConStr might be confusing, but perhaps would make more sense?

We could also allow StrLimit[10, 63] which would be equivalent to constr(min_length=10, max_length=63, strip_whitespace=True). However this would not be very pythonic in that the first argument meaning would change. Also I find myself wanting this very rarely.

thoughts?

@layday
Copy link
Contributor

layday commented Nov 12, 2020

You can't really do this with typing in Python, I don't think. The issue here is that to parametrise ('subscribe') a class for type checkers, it needs to be made generic. But a generic type can only be parametrised with other types - 63 is an instance of int and would be invalid. The only exception to this is Annotated.

@JacobHayes
Copy link
Contributor

JacobHayes commented Nov 27, 2020

Perhaps this would be possible/clean/generic with PR #2147 and FR #2129 (working prototype)?

class Foobar(BaseModel):
    name: Annotated[str, MaxLen(63), Trim()]

@antonagestam
Copy link
Contributor

@layday StrLimit doesn't need to be a class, it can be a subscribable object.

@samuelcolvin If we want to support both upper and lower bounds, perhaps StrLimit[:63] makes sense as syntax?

@samuelcolvin
Copy link
Member Author

Perhaps this would be possible/clean/generic with PR #2147 and FR #2129 (working prototype)?

class Foobar(BaseModel):
    name: Annotated[str, MaxLen(63), Trim()]

This should work now, although we need to add Trim().

@layday StrLimit doesn't need to be a class, it can be a subscribable object.

@samuelcolvin If we want to support both upper and lower bounds, perhaps StrLimit[:63] makes sense as syntax?

We had this in annotated-types but removed it because the upper value of a slice is max-exclusive which makes no sense, see annotated-types/annotated-types#23.


The point with this feature is to keep it simple and cover the most common case which is having an upper length limit - e.g. to be equivalent-to/compatible-with varchar() in sql.

@adriangb
Copy link
Member

I don't think this is necessary any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants