mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
fix pydantic import error + rename 'pattern' to pydantic's 'regex'
This commit is contained in:
parent
a850a9caab
commit
bae7e5b30a
4 changed files with 19 additions and 14 deletions
|
@ -69,7 +69,7 @@ class Interface(BaseInterface):
|
||||||
override_params = []
|
override_params = []
|
||||||
body_fields = {}
|
body_fields = {}
|
||||||
for param, field in self.build_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"):
|
if field and field.extra.get("file"):
|
||||||
|
@ -117,7 +117,7 @@ class Interface(BaseInterface):
|
||||||
# FIXME replace dummy error information
|
# FIXME replace dummy error information
|
||||||
raise fastapi.exceptions.RequestValidationError(
|
raise fastapi.exceptions.RequestValidationError(
|
||||||
[
|
[
|
||||||
pydantic.errors.ErrorWrapper(
|
pydantic.error_wrappers.ErrorWrapper(
|
||||||
ValueError(e.strerror), ("query", "test")
|
ValueError(e.strerror), ("query", "test")
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
|
@ -117,7 +117,6 @@ class BaseInterface:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def build_fields(
|
def build_fields(
|
||||||
cls,
|
|
||||||
params: Iterable[inspect.Parameter],
|
params: Iterable[inspect.Parameter],
|
||||||
annotations: dict[str, Any],
|
annotations: dict[str, Any],
|
||||||
doc: dict[str, str],
|
doc: dict[str, str],
|
||||||
|
@ -135,7 +134,7 @@ class BaseInterface:
|
||||||
if get_origin(field) is PrivateParam:
|
if get_origin(field) is PrivateParam:
|
||||||
field = None
|
field = None
|
||||||
elif isinstance(field, pydantic.fields.FieldInfo):
|
elif isinstance(field, pydantic.fields.FieldInfo):
|
||||||
update_field_from_annotation(
|
field = update_field_from_annotation(
|
||||||
field,
|
field,
|
||||||
param.default,
|
param.default,
|
||||||
name=param.name,
|
name=param.name,
|
||||||
|
@ -175,7 +174,7 @@ def Field(
|
||||||
deprecated: bool = False,
|
deprecated: bool = False,
|
||||||
description: Optional[str] = None,
|
description: Optional[str] = None,
|
||||||
hidden: bool = False,
|
hidden: bool = False,
|
||||||
pattern: Optional[Union[str, tuple[str, str]]] = None,
|
regex: Optional[Union[str, tuple[str, str]]] = None,
|
||||||
ask: Union[str, bool] = False,
|
ask: Union[str, bool] = False,
|
||||||
confirm: bool = False,
|
confirm: bool = False,
|
||||||
redac: bool = False,
|
redac: bool = False,
|
||||||
|
@ -207,9 +206,9 @@ def Field(
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> pydantic.fields.FieldInfo:
|
) -> pydantic.fields.FieldInfo:
|
||||||
|
|
||||||
pattern_name = None
|
pattern_name = kwargs.get("pattern_name", None)
|
||||||
if isinstance(pattern, tuple):
|
if isinstance(regex, tuple):
|
||||||
pattern_name, pattern = pattern
|
pattern_name, regex = regex
|
||||||
|
|
||||||
return pydantic.fields.Field(
|
return pydantic.fields.Field(
|
||||||
default=... if default is inspect.Parameter.empty else default,
|
default=... if default is inspect.Parameter.empty else default,
|
||||||
|
@ -234,7 +233,7 @@ def Field(
|
||||||
# min_length=min_length,
|
# min_length=min_length,
|
||||||
# max_length=max_length,
|
# max_length=max_length,
|
||||||
# allow_mutation=allow_mutation,
|
# allow_mutation=allow_mutation,
|
||||||
regex=pattern, # type: ignore
|
regex=regex, # type: ignore
|
||||||
# discriminator=discriminator,
|
# discriminator=discriminator,
|
||||||
# repr=repr,
|
# repr=repr,
|
||||||
# Yunohost custom
|
# Yunohost custom
|
||||||
|
@ -264,6 +263,10 @@ def update_field_from_annotation(
|
||||||
description: Optional[str] = None,
|
description: Optional[str] = None,
|
||||||
positional: bool = False,
|
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
|
field.default = ... if default is inspect.Parameter.empty else default
|
||||||
if name:
|
if name:
|
||||||
field.extra["name"] = name
|
field.extra["name"] = name
|
||||||
|
@ -272,3 +275,5 @@ def update_field_from_annotation(
|
||||||
if positional:
|
if positional:
|
||||||
field.extra["positional"] = positional
|
field.extra["positional"] = positional
|
||||||
field.extra["ask"] = False
|
field.extra["ask"] = False
|
||||||
|
|
||||||
|
return field
|
||||||
|
|
|
@ -102,7 +102,7 @@ class Interface(BaseInterface):
|
||||||
override_params = []
|
override_params = []
|
||||||
|
|
||||||
for param, field in self.build_fields(
|
for param, field in self.build_fields(
|
||||||
Interface, params, annotations, doc, positional_params
|
params, annotations, doc, positional_params
|
||||||
):
|
):
|
||||||
forward_params.append(param)
|
forward_params.append(param)
|
||||||
|
|
||||||
|
|
|
@ -63,15 +63,15 @@ FIELDS_FOR_IMPORT = {
|
||||||
ADMIN_ALIASES = ["root", "admin", "admins", "webmaster", "postmaster", "abuse"]
|
ADMIN_ALIASES = ["root", "admin", "admins", "webmaster", "postmaster", "abuse"]
|
||||||
|
|
||||||
DepreciatedField = Field(deprecated=True)
|
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(
|
FullnameField = Field(
|
||||||
param_decls=["-F"],
|
param_decls=["-F"],
|
||||||
ask=True,
|
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",
|
example="Camille Dupont",
|
||||||
)
|
)
|
||||||
DomainField = Field(
|
DomainField = Field(
|
||||||
pattern=(
|
regex=(
|
||||||
"pattern_domain",
|
"pattern_domain",
|
||||||
r"^([^\W_A-Z]+([-]*[^\W_A-Z]+)*\.)+((xn--)?[^\W_]{2,})$",
|
r"^([^\W_A-Z]+([-]*[^\W_A-Z]+)*\.)+((xn--)?[^\W_]{2,})$",
|
||||||
),
|
),
|
||||||
|
@ -81,7 +81,7 @@ PasswordField = Field(
|
||||||
ask=True,
|
ask=True,
|
||||||
confirm=True,
|
confirm=True,
|
||||||
redac=True,
|
redac=True,
|
||||||
pattern=("pattern_password", r"^.{3,}$"),
|
regex=("pattern_password", r"^.{3,}$"),
|
||||||
example="secret_password",
|
example="secret_password",
|
||||||
)
|
)
|
||||||
MailboxQuotaField = Field(pattern=("pattern_mailbox_quota", r"^(\d+[bkMGT])|0$"))
|
MailboxQuotaField = Field(pattern=("pattern_mailbox_quota", r"^(\d+[bkMGT])|0$"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue