fix pydantic import error + rename 'pattern' to pydantic's 'regex'

This commit is contained in:
axolotle 2023-01-19 01:46:24 +01:00
parent a850a9caab
commit bae7e5b30a
4 changed files with 19 additions and 14 deletions

View file

@ -69,7 +69,7 @@ class Interface(BaseInterface):
override_params = []
body_fields = {}
for param, field in self.build_fields(
Interface, params, annotations, doc, positional_params
params, annotations, doc, positional_params
):
if field and field.extra.get("file"):
@ -117,7 +117,7 @@ class Interface(BaseInterface):
# FIXME replace dummy error information
raise fastapi.exceptions.RequestValidationError(
[
pydantic.errors.ErrorWrapper(
pydantic.error_wrappers.ErrorWrapper(
ValueError(e.strerror), ("query", "test")
)
]

View file

@ -117,7 +117,6 @@ class BaseInterface:
@staticmethod
def build_fields(
cls,
params: Iterable[inspect.Parameter],
annotations: dict[str, Any],
doc: dict[str, str],
@ -135,7 +134,7 @@ class BaseInterface:
if get_origin(field) is PrivateParam:
field = None
elif isinstance(field, pydantic.fields.FieldInfo):
update_field_from_annotation(
field = update_field_from_annotation(
field,
param.default,
name=param.name,
@ -175,7 +174,7 @@ def Field(
deprecated: bool = False,
description: Optional[str] = None,
hidden: bool = False,
pattern: Optional[Union[str, tuple[str, str]]] = None,
regex: Optional[Union[str, tuple[str, str]]] = None,
ask: Union[str, bool] = False,
confirm: bool = False,
redac: bool = False,
@ -207,9 +206,9 @@ def Field(
**kwargs: Any,
) -> pydantic.fields.FieldInfo:
pattern_name = None
if isinstance(pattern, tuple):
pattern_name, pattern = pattern
pattern_name = kwargs.get("pattern_name", None)
if isinstance(regex, tuple):
pattern_name, regex = regex
return pydantic.fields.Field(
default=... if default is inspect.Parameter.empty else default,
@ -234,7 +233,7 @@ def Field(
# min_length=min_length,
# max_length=max_length,
# allow_mutation=allow_mutation,
regex=pattern, # type: ignore
regex=regex, # type: ignore
# discriminator=discriminator,
# repr=repr,
# Yunohost custom
@ -264,6 +263,10 @@ def update_field_from_annotation(
description: Optional[str] = None,
positional: bool = False,
):
# FIXME proper copyy?
copy = {attr: getattr(field, attr) for attr in field.__slots__}
field = Field(**copy | copy.pop("extra"))
field.default = ... if default is inspect.Parameter.empty else default
if name:
field.extra["name"] = name
@ -272,3 +275,5 @@ def update_field_from_annotation(
if positional:
field.extra["positional"] = positional
field.extra["ask"] = False
return field

View file

@ -102,7 +102,7 @@ class Interface(BaseInterface):
override_params = []
for param, field in self.build_fields(
Interface, params, annotations, doc, positional_params
params, annotations, doc, positional_params
):
forward_params.append(param)

View file

@ -63,15 +63,15 @@ FIELDS_FOR_IMPORT = {
ADMIN_ALIASES = ["root", "admin", "admins", "webmaster", "postmaster", "abuse"]
DepreciatedField = Field(deprecated=True)
UsernameField = Field(pattern=("pattern_username", r"^[a-z0-9_]+$"), example="username")
UsernameField = Field(regex=("pattern_username", r"^[a-z0-9_]+$"), example="username")
FullnameField = Field(
param_decls=["-F"],
ask=True,
pattern=("pattern_fullname", r"^([^\W\d_]{1,30}[ ,.'-]{0,3})+$"),
regex=("pattern_fullname", r"^([^\W\d_]{1,30}[ ,.'-]{0,3})+$"),
example="Camille Dupont",
)
DomainField = Field(
pattern=(
regex=(
"pattern_domain",
r"^([^\W_A-Z]+([-]*[^\W_A-Z]+)*\.)+((xn--)?[^\W_]{2,})$",
),
@ -81,7 +81,7 @@ PasswordField = Field(
ask=True,
confirm=True,
redac=True,
pattern=("pattern_password", r"^.{3,}$"),
regex=("pattern_password", r"^.{3,}$"),
example="secret_password",
)
MailboxQuotaField = Field(pattern=("pattern_mailbox_quota", r"^(\d+[bkMGT])|0$"))