Skip to content

Commit

Permalink
Add missing typing to example (#4596) (#4605)
Browse files Browse the repository at this point in the history
* Add missing typing to example

* Update example typing.

Co-authored-by: Rémy HUBSCHER <hubscher.remy@gmail.com>
  • Loading branch information
github-actions[bot] and Natim committed Oct 10, 2022
1 parent 5f777a0 commit 4e9115a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions docs/examples/schema_with_field.py
@@ -1,24 +1,26 @@
# output-json
from typing import Optional
from typing import Any, Callable, Dict, Generator, Optional

from pydantic import BaseModel, Field
from pydantic.fields import ModelField


class RestrictedAlphabetStr(str):
@classmethod
def __get_validators__(cls):
def __get_validators__(cls) -> Generator[Callable, None, None]:
yield cls.validate

@classmethod
def validate(cls, value, field: ModelField):
def validate(cls, value: str, field: ModelField):
alphabet = field.field_info.extra['alphabet']
if any(c not in alphabet for c in value):
raise ValueError(f'{value!r} is not restricted to {alphabet!r}')
return cls(value)

@classmethod
def __modify_schema__(cls, field_schema, field: Optional[ModelField]):
def __modify_schema__(
cls, field_schema: Dict[str, Any], field: Optional[ModelField]
):
if field:
alphabet = field.field_info.extra['alphabet']
field_schema['examples'] = [c * 3 for c in alphabet]
Expand Down

0 comments on commit 4e9115a

Please sign in to comment.