Skip to content

Commit

Permalink
Use t.Literal instead of pattern if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Sep 17, 2024
1 parent bc260af commit b9e3722
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 40 deletions.
53 changes: 40 additions & 13 deletions src/antsibull_docs/schemas/docs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
class Model(BaseModel):
new_name: str
# Use pydantic.constr so that json-schema validations will also need to match the pattern
type: constr(pattern='^(int|str|float)$')
# Use t.Literal so that json-schema validations will also need to match the pattern
type: t.Literal["int", "str", "float"]
# Use validator on a specific attribute to change the value of the attribute
@validator('type', pre=True)
Expand Down Expand Up @@ -150,11 +150,25 @@ def handle_renamed_attribute(cls, values):
REQUIRED_ENV_VAR_F = p.Field(..., pattern="[A-Z_]+")

#: option types are a set of strings that represent the types handled by argspec.
OPTION_TYPE_F = p.Field(
OPTION_TYPE = t.Literal[
"any",
"bits",
"bool",
"bytes",
"dict",
"float",
"int",
"json",
"jsonarg",
"list",
"path",
"raw",
"sid",
"str",
pattern="^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list"
"|path|raw|sid|str|tmppath|pathspec|pathlist)$",
)
"tmppath",
"pathspec",
"pathlist",
]

#: Constrained string type for version numbers
REQUIRED_VERSION_F = p.Field(..., pattern="^([0-9][0-9.]+)$")
Expand All @@ -172,11 +186,24 @@ def handle_renamed_attribute(cls, values):
REQUIRED_COLLECTION_NAME_OR_EMPTY_STR_F = p.Field("", pattern="^([^.]+\\.[^.]+)?$")

#: Constrained string listing the possible types of a return field
RETURN_TYPE_F = p.Field(
RETURN_TYPE = t.Literal[
"any",
"bits",
"bool",
"bytes",
"complex",
"dict",
"float",
"int",
"json",
"jsonarg",
"list",
"path",
"sid",
"str",
pattern="^(any|bits|bool|bytes|complex|dict|float|int|json"
"|jsonarg|list|path|sid|str|pathspec|pathlist)$",
)
"pathspec",
"pathlist",
]


def is_json_value(value: t.Any) -> bool:
Expand Down Expand Up @@ -467,9 +494,9 @@ class OptionsSchema(BaseModel):
aliases: list[str] = []
choices: t.Union[list[t.Any], dict[t.Any, list[str]]] = []
default: t.Any = None # JSON value
elements: str = OPTION_TYPE_F
elements: OPTION_TYPE = "str"
required: bool = False
type: str = OPTION_TYPE_F
type: OPTION_TYPE = "str"
version_added: str = "historical"
version_added_collection: str = COLLECTION_NAME_F

Expand Down Expand Up @@ -576,7 +603,7 @@ class AttributeSchemaBase(BaseModel, metaclass=abc.ABCMeta):
# Without this base class, we would hit https://github.com/samuelcolvin/pydantic/issues/1259
description: list[str]
details: list[str] = []
support: str = p.Field("str", pattern="^(full|partial|none|N/A)$")
support: t.Literal["full", "partial", "none", "N/A"]
version_added: str = "historical"
version_added_collection: str = COLLECTION_NAME_F

Expand Down
6 changes: 2 additions & 4 deletions src/antsibull_docs/schemas/docs/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2020, Ansible Project
"""Schemas for the plugin DOCUMENTATION data."""

import pydantic as p
import typing as t

from .base import BaseModel
from .plugin import (
Expand All @@ -15,8 +15,6 @@
PluginReturnSchema,
)

REQUIRED_CALLBACK_TYPE_F = p.Field(..., pattern="^(aggregate|notification|stdout)$")


class InnerCallbackDocSchema(InnerDocSchema):
"""
Expand All @@ -26,7 +24,7 @@ class InnerCallbackDocSchema(InnerDocSchema):
having separate types.
"""

type: str = REQUIRED_CALLBACK_TYPE_F
type: t.Literal["aggregate", "notification", "stdout"]


class CallbackDocSchema(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions src/antsibull_docs/schemas/docs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
COLLECTION_NAME_F,
REQUIRED_CLI_F,
REQUIRED_ENV_VAR_F,
RETURN_TYPE_F,
RETURN_TYPE,
BaseModel,
DeprecationSchema,
DocSchema,
Expand Down Expand Up @@ -87,10 +87,10 @@ class ReturnSchema(BaseModel):

description: list[str]
choices: t.Union[list[t.Any], dict[t.Any, list[str]]] = []
elements: str = RETURN_TYPE_F
elements: RETURN_TYPE = "str"
returned: str = "success"
sample: t.Any = None # JSON value
type: str = RETURN_TYPE_F
type: RETURN_TYPE = "str"
version_added: str = "historical"
version_added_collection: str = COLLECTION_NAME_F

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,6 @@ The errors were:

Unable to normalize foo3: return due to: 2 validation errors for PluginReturnSchema
return -> bar -> type
String should match pattern '^(any|bits|bool|bytes|complex|dict|float|int|json|jsonarg|list|path|sid|str|pathspec|pathlist)$' (type=string_pattern_mismatch; pattern=^(any|bits|bool|bytes|complex|dict|float|int|json|jsonarg|list|path|sid|str|pathspec|pathlist)$)
Input should be 'any', 'bits', 'bool', 'bytes', 'complex', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'sid', 'str', 'pathspec' or 'pathlist' (type=literal_error; expected='any', 'bits', 'bool', 'bytes', 'complex', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'sid', 'str', 'pathspec' or 'pathlist')
return -> baz
Input should be a valid dictionary or instance of OuterReturnSchema (type=model_type; class_name=OuterReturnSchema)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The errors were:
doc -> options -> bar -> description -> 1
Input should be a valid string (type=string_type)
doc -> options -> bar -> type
String should match pattern '^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list|path|raw|sid|str|tmppath|pathspec|pathlist)$' (type=string_pattern_mismatch; pattern=^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list|path|raw|sid|str|tmppath|pathspec|pathlist)$)
Input should be 'any', 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str', 'tmppath', 'pathspec' or 'pathlist' (type=literal_error; expected='any', 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str', 'tmppath', 'pathspec' or 'pathlist')
doc -> options -> foo
Input should be a valid dictionary or instance of ModuleOptionsSchema (type=model_type; class_name=ModuleOptionsSchema)
doc -> options -> subfoo -> bam
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,6 @@ The errors were:

Unable to normalize foo3: return due to: 2 validation errors for PluginReturnSchema
return -> bar -> type
String should match pattern '^(any|bits|bool|bytes|complex|dict|float|int|json|jsonarg|list|path|sid|str|pathspec|pathlist)$' (type=string_pattern_mismatch; pattern=^(any|bits|bool|bytes|complex|dict|float|int|json|jsonarg|list|path|sid|str|pathspec|pathlist)$)
Input should be 'any', 'bits', 'bool', 'bytes', 'complex', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'sid', 'str', 'pathspec' or 'pathlist' (type=literal_error; expected='any', 'bits', 'bool', 'bytes', 'complex', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'sid', 'str', 'pathspec' or 'pathlist')
return -> baz
Input should be a valid dictionary or instance of OuterReturnSchema (type=model_type; class_name=OuterReturnSchema)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The errors were:
doc -> options -> bar -> description -> 1
Input should be a valid string (type=string_type)
doc -> options -> bar -> type
String should match pattern '^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list|path|raw|sid|str|tmppath|pathspec|pathlist)$' (type=string_pattern_mismatch; pattern=^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list|path|raw|sid|str|tmppath|pathspec|pathlist)$)
Input should be 'any', 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str', 'tmppath', 'pathspec' or 'pathlist' (type=literal_error; expected='any', 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str', 'tmppath', 'pathspec' or 'pathlist')
doc -> options -> foo
Input should be a valid dictionary or instance of ModuleOptionsSchema (type=model_type; class_name=ModuleOptionsSchema)
doc -> options -> subfoo -> bam
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,6 @@ The errors were:

Unable to normalize foo3: return due to: 2 validation errors for PluginReturnSchema
return -> bar -> type
String should match pattern '^(any|bits|bool|bytes|complex|dict|float|int|json|jsonarg|list|path|sid|str|pathspec|pathlist)$' (type=string_pattern_mismatch; pattern=^(any|bits|bool|bytes|complex|dict|float|int|json|jsonarg|list|path|sid|str|pathspec|pathlist)$)
Input should be 'any', 'bits', 'bool', 'bytes', 'complex', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'sid', 'str', 'pathspec' or 'pathlist' (type=literal_error; expected='any', 'bits', 'bool', 'bytes', 'complex', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'sid', 'str', 'pathspec' or 'pathlist')
return -> baz
Input should be a valid dictionary or instance of OuterReturnSchema (type=model_type; class_name=OuterReturnSchema)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The errors were:
doc -> options -> bar -> description -> 1
Input should be a valid string (type=string_type)
doc -> options -> bar -> type
String should match pattern '^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list|path|raw|sid|str|tmppath|pathspec|pathlist)$' (type=string_pattern_mismatch; pattern=^(any|bits|bool|bytes|dict|float|int|json|jsonarg|list|path|raw|sid|str|tmppath|pathspec|pathlist)$)
Input should be 'any', 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str', 'tmppath', 'pathspec' or 'pathlist' (type=literal_error; expected='any', 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str', 'tmppath', 'pathspec' or 'pathlist')
doc -> options -> foo
Input should be a valid dictionary or instance of ModuleOptionsSchema (type=model_type; class_name=ModuleOptionsSchema)
doc -> options -> subfoo -> bam
Expand Down
Loading

0 comments on commit b9e3722

Please sign in to comment.