Commit Graph

91 Commits

Author SHA1 Message Date
Eric Jolibois ea870115b7 fix: allow submodels to overwrite extra field info (#3935)
* fix: allow submodels to overwrite extra field info

* fix(deps): pin jinja2 for mkdocs
2022-08-08 15:01:39 +01:00
Hasan Ramezani 6ceff2eea2 Add Pyupgrade (#4214) 2022-07-06 08:52:01 +02:00
Samuel Colvin 8718db581d Fix JSON Schema generation for Discriminated Unions within lists. (#4071)
* Fix JSON Schema generation for Discriminated Unions within lists.

* linting

* fix mypy
2022-05-14 07:26:34 +01:00
Luis R faee3301eb Fix regression in handling of nested dataclasses in get_flat_models_from_field (#3819)
* add test for nested python dataclass schema generation

* fix handling of dataclasses in `get_flat_models_from_field`

* add change note
2022-05-11 19:02:37 +01:00
dependabot[bot] bf5fdfc618 build(deps): bump black from 21.12b0 to 22.3.0 (#3950)
* build(deps): bump black from 21.12b0 to 22.3.0

Bumps [black](https://github.com/psf/black) from 21.12b0 to 22.3.0.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](https://github.com/psf/black/commits/22.3.0)

---
updated-dependencies:
- dependency-name: black
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* apply new black styles, fix docs

* try upgrading pip before fastapi tests

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-04-02 08:35:55 +01:00
Samuel Colvin 6f46a5a146 drop python3.6 support (#3605)
* drop python3.6 support

* revert small change

* fix 3.7 failures

* more cases and cleanup

* add change description
2022-01-02 13:53:45 +00:00
Jaakko Moisio 63337fbadc Add optional field argument to __modify_schema__() (#3434)
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:55:22 +00:00
Eric Jolibois c834f3419d Support discriminated union (#2336)
* feat: add discriminated union

* feat: add OpenAPI spec schema

* test: add basic example for generated schema

* test: add validation tests

* docs: add basic documentation

* fix: support ForwardRef

* test: add ForwardRef case

* fix: false positive lint error

https://github.com/PyCQA/pyflakes/pull/600

* improve error

* add schema/schema_json utils

* fix tests after merge

* refactor: add `discriminator` attribute to `FieldInfo`

* refactor: @cybojenix remarks

* fix schema with forward ref

* start nested

* feat: add allowed values in error message

* fix wrong check

Same example with

```
class FooDomainA(BaseModel):
    __root__: Union[FooDomainAA, FooDomainAB]
```

(without a discriminator)
should obviously be valid

* test: add nested examples

* remove uncovered code as we don't need it

* docs: add nested example

* fix: support properly Annotated Field syntax

* support naked annotated

* fix: handle TypeError

* make error loc more explicit

* fix behaviour with basemodel instance as value

* support schema for dataclasses

* tweak examples

* refactor: context manager just around code that fails

* refactor: add docstring + tweak on `get_sub_types`

* refactor: move `get_discriminator_values` in `utils.py`

* refactor: create `MissingDiscriminator` and `InvalidDiscriminator`

* refactor: move logic in `_validate_discriminated_union`

* refactor: remove `DiscriminatedUnionConfig`

* docs: schema/schema_json

* tests: add tests with other `Literal` types

* update 3.10

* add schema docstring

* weird bug on 3.8 with `Literal[None]`

* bump to view docs & coverage

* bump to prompt tests

* move tests in dedicated file

* chore: rewording

* refactor: replace property by direct slot

* refactor: faster check

* refactor: missing discriminator

* refactor: change error to ConfigError

* refactor: use display_as_type

* fix: mypy

* fix: duplicate

* feat: handle alias

* feat: handle nested unions

* tweak first example

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 16:31:35 +00:00
João M. Martins dc4710b56c Replacing encode_default instance check with strict type check (#3197)
* Replacing encode_default instance check with strict type check

* Adding change notes

* Changing to dictionary indexing in tests

* Adding explicit Enum check and returning its value
2021-12-08 21:52:25 +00:00
Eric Jolibois 8c2c602c1b feat: add confrozenset() (#2672)
* feat: add `confrozenset()`

* fix: add tests for allowed_schemas
2021-12-07 22:11:42 +00:00
Sam McHardy eef4ac505e Fix missing enum extra (#2818)
* Allow passing schema_overrides to get_field_info_schema

This function was resetting schema_overrides which prevents **extras on Enum type Fields
from being saved to their json schema

* Add changes file

* Update changes/2697-sammchardy.md

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>

Co-authored-by: Sam McHardy <sam.mchardy@psma.com.au>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-12-07 15:10:53 +01:00
Eric Jolibois a35cde90af feat(schema): set minItems and maxItems in JSON schema for tuples (#2497)
* feat(schema): enforce length in generated JSON schema for tuple type

* docs: add change file

* docs: update documentation

* simplify a bit

* always set array
2021-12-05 13:20:07 +00:00
sergejkozin 257908628f fix(schema): fix schema generation for Deque field (#2811)
* fix(schema): fix schema generation for `Deque` field

* add change description
2021-09-04 22:45:40 +02:00
Eric Jolibois f5a9f4fbe3 feat: handle object type (#3062) 2021-09-04 16:40:14 +02:00
maximberg 93aed51183 Advanced use of generic fields in schema generation (#2375)
* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* tested on python 3.6, 3.7, 3.8, 3.9

* tested on python 3.6, 3.7, 3.8, 3.9

* restore formatting

* fix mistakes

* formatting

* formatting

* formatting

* fixed coverage

* changes file

* changes file

* remove redundant len

* Update pydantic/schema.py

Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>

* Generic fields with params

* Generic fields with params

* Fix my previous test

* formatting

* merging with nested models-generics

* formatting

* formatting

* rewording

Co-authored-by: Maxim Berg <berg@petrostyle.com>
Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>
Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 02:58:43 +02:00
Eric Jolibois a6154514c1 fix(schema): namedtuple with default (#2711)
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-05-11 15:13:54 +01:00
Eric Jolibois 4753901151 fix(schema): JSON schema generation with a Literal of an enum member (#2536)
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-05-09 12:20:54 +01:00
Danielle Madeley 82fb6ebfc9 Generate schema for generic models (#2364) 2021-05-09 09:23:56 +01:00
Eric Jolibois ededd3ebf8 refactor(schema): support properly Literal in generated JSON schema (#2348)
* test: improve example

* refactor: use enum for Literal in JSON schema

* test: update test with new schema generation

* chore: add change file
2021-02-25 19:42:17 +00:00
Samuel Colvin b7a8ef25c6 fix coverage and make typing-extensions a required dependency (#2368)
* fixing coverage by simplifying Annotated import logic, fix #2367

* avoid checking against annotated if it's None

* make typing-extensions required WIP

* more making typing-extensions required

* fix docs and get_origin for python 3.6

* fix mypy test

* fix docs

* update docs, cleanup and add change

* clean docs/examples/schema_annotated.py

* move AnnotatedTypeNames
2021-02-17 19:17:30 +00:00
Scott Black 13928e5b98 introduce allow_mutation Field constraint (#2196)
* introduce read_only Field constraint

* add changes markdown for read_only constraint

* add readOnly property to json schema generation

* Revert "add readOnly property to json schema generation"

This reverts commit dad3d3e23a7336aa978dcdedc157559ea102fa54.

* change read_only field constraint to allow_mutation

* Update change notes for allow_mutation

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* allow field constraints defaults to be not None

* remove unnecessary test after constraint refactor

* push used constraints check back to schema functions

* use tuple item name descriptions instead of indexes

* move get_constraints function to method on FieldInfo

* address code review comments for minor changes

* Apply suggestions from code review

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* fix merge conflict

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-02-13 17:22:32 +00:00
maximberg c3870b611e Added schema generation for Generic fields (#2262)
* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* tested on python 3.6, 3.7, 3.8, 3.9

* tested on python 3.6, 3.7, 3.8, 3.9

* restore formatting

* fix mistakes

* formatting

* formatting

* formatting

* fixed coverage

* changes file

* changes file

* remove redundant len

* Update pydantic/schema.py

Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>

Co-authored-by: Maxim Berg <berg@petrostyle.com>
Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>
2021-02-13 17:04:49 +00:00
Justin Sexton 688107ec8e Fixed KeyError when creating schema from models with same names declared within different classes (#2170)
* Fixed issue causing KeyError to be raised when building schema from multiple `BaseModel` with the same names declared in separate classes (#1912)

* Update test

* Removed redundant f-string

* Fixed formatting

* Fixed lint
2021-02-11 17:39:59 +00:00
Eric Jolibois d0baf0f156 fix(fields): handle properly default value for type Callable (#2094)
* fix(fields): handle properly default value for type `Callable`

closes #1596

* chore: move test in test_dataclasses

* chore: add comments

* test: rewrite test

* chore: remove useless variable

* fix(schema): add support when callable field has default value

closes #2086
2021-02-11 11:39:09 +00:00
Eric Jolibois 13a5c7d676 fix(schema): fix schema generation with multiple Enums having the same name (#2226)
closes #1857
2021-01-02 09:33:36 +00:00
Ben Martineau 0b7a570afd Fixes a regression where Enum fields would not propagate keyword arguments to the schema (#2109)
fix #2108

* Fix schema extra not being included when field type is Enum

* Code format

* More code format

* Add changes file

Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>
2020-11-29 23:57:42 +00:00
Matthew Davis 9cb7ca744b add NonPositive/NonNegative Int/Float (#1987)
fix #1975

* add NonPositive/NonNegative Int/Float

* delete data.json file
2020-11-29 16:07:51 +00:00
Gabriel d14731f16c schema ref_template (#1480)
* ignore Pipfile, .lock files

* add ref_template option

use a string.Template instead of a ref_prefix to allow for more varied`$ref`s to be created.
Template string is expected to have $model_name `identifier `

* formatting / linting

* add changes

* typo

* use string.format instead of string.Template

* remove ref_prefix default

if no `ref_prefix` provided, use the `template_default`

* use ref_template in field_singleton_schema

* fix test_schema_with_ref_template

* add parameters for `test_schema_with_refs`

test name change 
test for key error

* provide ref_template default argument

* fix linting

* fix mypy and coverage

* docs and correct model_schema usage

* fastAPI tests actualy caught an error

* linting

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 16:24:37 +00:00
PrettyWood 3aacec4e17 feat(schema): support custom title, description and default for enums (#1749)
* refactor(schema): put schema data from fieldinfo in dedicated function

* feat(schema): support custom title, description and default for enums

closes #1748

* refactor: replace $ref by allOf + $ref to be supported by doc generation tools

* fix: do not set title by default for enums

* refactor: make code more explicit

* fix: run linter
2020-10-18 20:23:06 +01:00
Patrick Wang dca98559d5 Add conset (#1627)
* Add conset

* Add changes doc

* Fix too-long lines

* bump

* Update conset change doc formatting

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Robustify test_conset

* Move conlist and conset tests to test_types

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-07-03 20:51:23 +01:00
John Carter ff20477521 Apply __modify_schema__ on enum schema rather than fields that use it (#1581)
* Apply __modify_schema__ on enum schema rather than fields that use it

Resolves #1576

* tweak test

* correct linting :-(

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-06-29 13:22:27 +01:00
PrettyWood 7ac9faf031 (fix) use right type check when modifying schema (#1562)
fix #1552
2020-06-27 14:12:36 +01:00
wangpeibao 5e82689c79 Change schema frozenset (#1560)
* add frozenset to field_class_to_schema

* add change.md

* add my test

* format my test

* remove print()

* just test

* return to original code

Co-authored-by: wangpeibao <wangpeibao@troila.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-06-02 11:03:12 +01:00
Calvin Young 5195e55c10 Enums as separate models (#1432)
* Updates schema() to generate enums as separate models

* Fixes mypy annotations

* Adds changes file

* Fixes comment

* Removes unused import

* Fixes test case

* Fixes missing partial branch in test coverage

* Resolves PR comments

* 🐛 Include enums in flat model schema handling

as they now have independent schemas, they kinda behave like top-level models, and should be taken into account for top level definitions

*  Add test for coverage

* 🐛 Use Type[Enum] as type for consistency

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2020-05-23 19:11:34 +01:00
Samuel Colvin 5067508eca fix tests and coverage for #1209 2020-05-01 10:32:39 +01:00
Mostapha Sadeghipour Roudsari a4adf892dc Remove extra allOf keys in schema (#1438)
resolves #1209
2020-04-30 18:56:11 +01:00
Atheuz 9238e3d98f include format with secret types (#1424)
* include format with secret types

* fix: fix pr comments.

Co-authored-by: Lasse Gravesen <lasse.gravesen@falcon.io>
2020-04-30 13:09:45 +01:00
Jesse VanderWees 833d33dc0f ConstrainedFloat schema: differences between IEEE floats and json (#1422)
* ConstrainedFloat schema: account for differences between IEEE floats and json

* add changes

* Update changes/1417-vdwees.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update changes/1417-vdwees.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* fixup

* Update pydantic/types.py

Difference of styles :)

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* merge _schema_ieee_compatibility_transform into parent method

* capitalize

* use type_, not outer_type_

Co-authored-by: Jesse VanderWees <jesse.vanderwees@kisters-bv.nl>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-04-30 13:08:23 +01:00
laevilgenius 7bebf2bdc3 Allow schema_extra to be a classmethod (#1306)
* Remove FunctionType check

* Fix mypy

* Add classmethod test

* Add instance method test

* Fix linting

* model_class

* Remove __call__ implementation

* Fix linting for py3.8

* noqa tweak

* undo mistake sorry, :-/

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-15 22:06:25 +01:00
PrettyWood aaec3c9c15 Remove typing-extension dependency on python 3.8 (#1342)
* Remove typing-extension dependency on python 3.8

* retrigger CI

* retrigger CI bis
2020-03-26 15:44:11 +00:00
Samuel Colvin d495710303 Cleanup ci & coverage (#1329)
* fix badges and deploy check

* fix coverage and correct check_tag.py
2020-03-21 18:14:17 +00:00
Samuel Colvin 943a8a06e5 change alias priority logic (#1178)
* fix alias priority so alias_generators don't take priority

* improve test names

* remove debugs

* Apply suggestions from code review

* more tests and allow custom alias_priority on fields

* precedence tests and docs

* tweaks and add change

* suggestions
2020-01-24 10:31:16 +00:00
Sebastián Ramírez 496551c854 Implement support for declaring infinite generators (#1152)
*  Implement support for infinite generators with Iterable

*  Add tests for infinite generators

* 🎨 Fix format

* 📝 Add docs for infinite generators

* 📝 Add changes file

*  Store sub_field with original type parameter to allow custom validation

* 📝 Add example for validating first value in infinite generators

* 🔥 Remove unused import in example

*  Add test for infinite generator with first-value validation

* ♻️ Update fields with code review

* 📝 Update example from code review

* 📝 Update docs and format from code review
2020-01-13 11:50:16 +00:00
John Carter cd8b504568 Pass model_class to schema_extra staticmethod (#1125)
* Pass model_class to schema_extra staticmethod

Resolves #1122

* Add changelog

* Apply suggestions from code review

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Fix import after rebase

* Fix test bug

* Use TypeError instead of assert as per review

* Rename var so declaration fits one one line

* tiny tweaks

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-01-06 12:01:03 +00:00
Samuel Colvin 31cac4783b improve pydantic import time (#1132)
* improve pydantic import time, fix #1127, fix #1039

* more tweaks

* tweak utils.py

* defering inspect
2020-01-02 14:35:03 +00:00
Samuel Colvin 065ae2e4c4 fix modify schema (#1103)
* modify schema, fix #1102

* remove DSN from schema table

* fix tests

* add outer_type_ to ModelField, fix coverage

* simplify ModelField._type_display

* remove debugs

* revert change to _type_display

* test fix
2019-12-21 15:46:11 +00:00
Samuel Colvin e1ae058afb Consistent checks for sequence like objects (#1111) 2019-12-20 15:08:45 +00:00
Samuel Colvin bd72e80e94 Prevent type attributes being added to schema unintentionally (#1065)
* Prevent type attributes being added to schema unintentionally, fix #1064

* simpler boolean check

* change to __modify_schema__

* better docs

* Update docs/usage/schema.md

Co-Authored-By: dmontagu <35119617+dmontagu@users.noreply.github.com>
2019-12-11 12:01:41 +00:00
Selim Belhaouane bdc3973d90 Allow schema_extra to be a callable (#1054)
* fixes for Windows development

* Allow Config.schema_extra to be a callable
2019-12-09 11:01:17 +00:00
Aviram Hassan 1b9f08bead Dataclass is now compatible with schema methods (#1045) 2019-12-02 12:44:21 +00:00