Commit Graph

156 Commits

Author SHA1 Message Date
Amin Alaee 962ea8bf21 Add docs for defining upper-case variables on nested setting models (#4153) 2022-07-05 20:04:46 +02:00
Jochen Kupperschmidt 058d4a52ee Tweak documentation (#4143) 2022-07-04 22:53:11 +02:00
Ben Nuttall 33ae990c76 Fix model_config documentation (#4179) 2022-07-03 13:15:54 +02:00
Tetsuo Kiso d7593368c7 Fix typo in models.md (#4145) 2022-07-03 13:11:56 +02:00
Samuel Colvin 8997cc5961 Remove benchmarks completely (#3973)
* removing benchmarks completely

* [no ci] add change
2022-04-04 13:29:17 +01:00
Gary Donovan 7f90b2f342 Remove incorrect comment about lazy evaluation of setting sources (#3806)
* Remove incorrect comment about lazy evaluation of setting sources

It looks like the current implementation always evaluates every source (https://github.com/samuelcolvin/pydantic/blob/9d631a3429a66f30742c1a52c94ac18ec6ba848d/pydantic/env_settings.py#L73) before coalescing them into a single dictionary to pass to `BaseModel`. So the comment about lazy evaluation is incorrect and should be removed.

* Add changelog
2022-04-02 15:25:43 +01:00
Tom Milligan 02eb182db0 fix: clarify that discriminated unions do not support singletons (#3639) 2022-04-02 14:06:11 +01:00
Guilhem C 90628e1c2b docs: fix typo in settings management page (#3781) 2022-04-01 19:47:05 +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
Eric Jolibois edad0dbc46 fix: keep old behaviour of json() by default (#3542)
* fix: handle basemodel fallback for custom encoders

* put back old behaviour and add to_dict

* typo

Co-authored-by: Christian Bundy <christianbundy@fraction.io>

Co-authored-by: Christian Bundy <christianbundy@fraction.io>
2021-12-24 13:14:13 +00:00
Eric Jolibois 81b13ff3b4 fix: rename schema util into schema_of (#3547) 2021-12-22 19:42:56 +00:00
Mark Trifonov be246701c5 Nested env (#3159)
* Environment names for complex types #2304

* nested env disabled by default

* cleanup

* nested env settings: simplified and mypy fixes

* nested env settings: config, test, doc

* nested env settings: changes file

* nested env settings: cleanup

* Apply suggestions from code review

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

* Apply suggested changes from code review

* lint fix

* changes from code review

* simplify explosing env vars

* linting

Co-authored-by: Mark Trifonov <>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:56:14 +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
Marcelo Trylesinski 7eaa582980 Add AmqpDsn class (#3254)
* Add RabbitmqDsn

Will update once finished - haven't looked at the CONTRIB yet. Want to test locally to see if it works.

* added tests; added to docs

* added changes

* fixed import in networks.py

* fixed linting issues; fixed __init__.py import issue

* sorted imports

* added trailing comma on imports

* Merge master

* Change class name from RabbitmqDsn to RabbitMqDsn

* Format code

* Rename change file and prettify content

* Fix RabbitMQ name on documentation

* Add a trivial test

* Address Samuel and Nuno's comments

* Refactor AMQP tests according to Redis tests style

* Update docs/examples/settings_main.py

* cleanup

Co-authored-by: Thomas <thomas@9bitbyte.com>
Co-authored-by: Thomas Crha <tom.crha@dragonflytechnologies.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:40:16 +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
Sebastián Ramírez 61d30aed7a Add support for decimal-specific configs in Field() (#3507)
*  Add support for Decimal-specific configs in Field()

*  Add/update tests for condecimal and variant with Field()

* 📝 Update schema - Field() docs including Decimal-specific configs

* 📝 Add PR changes file
2021-12-11 11:21:06 +00:00
Nuno André 91ecfd651e feat: Add unique items validation to constrained lists (#2618)
* Add unique items validation to constrained list

* add unique_items to field and schema
add failover for unhashable types
check keyword value to call the validator
add some tests

* update unique_items validation

Co-authored-by: Nuno André Novo <nuno.novo@forensic-security.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-10 10:04:06 +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
Eric Jolibois 458f257dcb feat: make JSON serialization of referenced pydantic models possible (#2650)
* feat: make JSON serialization of referenced pydantic models possible

* add docs

* chore: @samuelcolvin remarks
2021-12-07 22:01:11 +00:00
Eric Jolibois c38c463137 feat: add Config.smart_union option (#2092)
* feat: add `Config.smart_union` to prevent coercion in `Union` if possible

* docs: write some documentation

* Update docs/usage/model_config.md

Thanks @djpugh

Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>

* improve doc

* support 3.10

* improve smart_union

* Update docs/usage/types.md

Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>

* put new sentence inside warning block

* docs: reorder

* rename is_union_origin into is_union

* inverse and condition for perf

* fix doc

Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>
2021-12-07 21:58:52 +00:00
Yurii Karabas 415eb54f96 Try to evaluate forward refs after model created (#2588)
* Try to evaluate forward refs after model created

* Upadate docs and remove code duplication

* Update changes/2588-uriyyo.md

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

* Update docs/usage/postponed_annotations.md

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

* Remove unused import

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-12-05 14:20:48 +00:00
Tom Hacohen 8afdaab4ac Schema.md: use consistent (American) spelling (#3207)
Customisation is the British spelling. Below (and elsewhere) the American
spelling (customization) is used.
2021-10-27 01:28:27 +02:00
Josh Smith 7acac76f3c fix minor typo (#3240) 2021-10-27 01:26:52 +02:00
Greg Wilson d2625d6052 docs: small change to wording in note (#3347) 2021-10-27 00:55:04 +02:00
Pax e431604654 clarify docs on validation and strict type checking (#2855)
* clarify docs on validation and strict type checking

Related: Issue#578

Co-authored-by: cknoll <unknown@exampleemail.com>
Co-authored-by: PyAntony <unknown@exampleemail.com>

* add change.md for PR#2855

* docs: explicitly specify _partial_ support

* chore: update formatting of change description

Co-authored-by: cknoll <unknown@exampleemail.com>
2021-09-04 19:58:47 +02:00
Eric Jolibois f9c65c5b88 fix: add/fix BaseConfig type annotations (#2720) 2021-09-04 04:13:20 +02:00
Vasilis Gerakaris 8417b3bb5c add FileUrl type for file:// schemes, add host_required parameter (#2434)
* add `FileUrl` type for `file://` schemes

Also add a `host_required` parameter, True by default,
False in `FileUrl` and `RedisDsn`.

* chore: useless extra in assert statement

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 02:10:25 +02:00
Fernando ike 1a9f59dc1e Support postgresql+asyncpg schema for PostgresDsn (#2567)
* Added suport to postgresql async driver

* Added postgres+asyncpg in the doc.

* Added changes file

* Added postgresql+pg8000 schema

* ran make format

* Update docs/usage/types.md

Co-authored-by: Stephen Brown II <Stephen.Brown2@gmail.com>

* Changed from schema to scheme

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

* fixed typo scheme

* fixed merge schema

* changed to one line description

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

* added others DBAPI dialects.

* Added two # by PrettyWood

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

* fix typo by PrettyWood

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

* Fixed typo by PrettyWood

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

* added postgresql+psycopg2 in changes dir

* Fixed typo by @samuelcolvin

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

* docs: fix typo

* chore: sort alphabetically

Co-authored-by: Fernando Ike <fernando.ike@maburix.com>
Co-authored-by: Stephen Brown II <Stephen.Brown2@gmail.com>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-09-04 01:19:54 +02:00
Christian Clauss 90080ba0de Fix typos discovered by codespell (#3096) 2021-09-04 00:05:36 +02:00
Marcelo Trylesinski a4cb4ee3bc feat: add PastDate and FutureDate types (#2425)
* feat: add PastDate and FutureDate types

* add changes file

* add tests

* fix: json schema

* fix: black format

* less code duplicated

* add dates on success.py

* fix past and future dates

* ♻️ Apply Samuel's comments

* 🚨 Satisfy flake8

* 🔥 Remove _DateValueError

* test: add last explicit case

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-06-04 23:30:25 +02:00
Eric Jolibois c489445cea fix: mypy plugin link in docs (#2869) 2021-06-01 23:01:39 +02:00
Rémi Benoit 920d71b708 Fix mypy plugin link in doc (#2747) 2021-06-01 22:35:10 +02:00
Nuno André b718e8e626 Add GetterDict example (#2463)
* Add GetterDict example

* Update docs/usage/models.md

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

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-05-12 15:25:17 +01:00
Frank Epperlein 362f4a5163 add repr parameter to hide a field from repr() (#2593)
* add repr parameter to hide a field from repr()

* fix styling issues

… found by flake8.

* fix type annotation issues

* correctly name changes file

* test hiding default values from repr() of a Field

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-05-09 14:16:52 +01:00
Eric Jolibois 31c4efb7f0 fix: remove side effect of default_factory to always run it only once (#2566) 2021-05-09 13:35:15 +01:00
Koudai Aono 07908b3846 Add "strict" argument to "conbytes" (#2490)
* Add "strict" argument to "conbytes"

* Add unittest

* Update changes/2489-koxudaxi.md

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-05-09 10:27:29 +01:00
krisaoe 42395056e1 Fix(docs): remove colon to separate unrelated clauses (#2733)
* Fix(docs): remove colon to separate unrelated clauses 

The colon at the end of the line "The fields which were supplied when user was initialised:" suggests that the code following it is related.
Changed it to a period.

* Include change summary readme.
2021-05-03 17:46:37 +01:00
Davis Kirkendall db697cc799 Add exclude as field parameter (#2231)
* Add exclude/include as field parameters

- Add "exclude" / "include" as a field parameter so that it can be
  configured using model config (or fields) instead of purely at
  `.dict` / `.json` export time.
- Unify merging logic of advanced include/exclude fields
- Add tests for merging logic and field/config exclude/include params
- Closes #660

* Precompute include/exclude fields for class

* Increase test coverage
* Remove (now) redundant type checks in Model._iter: New
  exclusion/inclusion algorithms guarantee that no sets are passed further down.

* Add docs for advanced field level exclude/include settings

* Minimal optimization for simple exclude/include export

Running benchmarks this vs. master is at:

this: pydantic best=33.225μs/iter avg=33.940μs/iter stdev=1.120μs/iter version=1.7.3
master: pydantic best=32.901μs/iter avg=33.276μs/iter stdev=0.242μs/iter version=1.7.3

* Apply review comments on exclude/enclude field arguments

* Fix/simplify type annotations
* Allow both ``True`` and ``Ellipsis`` to be used to indicate full field
  exclusion
* Reenable hypothesis plugin (removed by mistake)
* Update advanced include/include docs to use ``True`` instead of ``...``

* Move field info exclude/include updates into FieldInfo class

This way, the model field object does not need to concern itself with
dealing with field into specific fields.
(Same was done for alias in a previous commit).

* remove double back tick in markdown.

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-05-01 16:45:37 +01:00
Edd Salkield ae13be5b8c docs: minor typo correction (#2649) 2021-04-10 11:21:51 +02:00
Eric Jolibois 14f055e743 fix(docs): url schema -> scheme (#2634) 2021-04-05 15:36:21 +02:00
wackazong 7fb77a779a Documentation: Put examples for config to the start of the section (#2615)
* Put examples to the start of the section

I think this is where they belong, they were in the middle of the list of config options somewhere near the end.

* Add "options" in table of contents

* tweak

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-04-05 12:40:13 +02:00
Joe 282d5b847d Fix indent level of "Validating the first value" (#2577) 2021-04-05 11:30:30 +02:00
William Woodruff ec1bf696e9 doc(schema): fix a callout (#2620)
* doc(schema): use boldface to highlight a note

* doc(schema): fix small formatting typo

* doc(schema): attempt to fix the callout

* docs(schema): indent the callout
2021-04-05 11:25:32 +02:00
nikhilraojl 9bd2da7805 fix: docs indentation (#2448)
* fix: docs indentation

* docs: last fixes indentation

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-03-02 10:33:50 +00:00
Samuel Colvin aff4a41f9d preparing for v1.8 (#2414)
* preparing for v1.8 🎉 🚀

* change description for #2415

* tweak change descriptions

* fix nested lists in docs

* remove items in 1.7.3 from 1.8
2021-02-26 16:30:08 +00:00
Tom Cobb 3f849a368f Support Field(default_factory) in validate_arguments (#2176)
* Support Field(default_factory) in validate_arguments

* Added docs for validate_arguments with Field
2021-02-23 12:07:11 +00:00
Raphael Huille d8e8e6a780 Generate a hash function when frozen is True (#1881)
* feature: add a `frozen` parameter to config

For now, `frozen` is a strict duplication of `allow_mutation` parameter
i.e. setting `frozen=True` does everything that `allow_mutation=False` does.

NB: this does not change the behavior of `allow_mutation`.

In next commit, setting `frozen=True` will also make the BaseModel hashable
while the existing behavior of `allow_mutation` will not be updated.

* refactor: factorise immutability tests

* feature: generate a hash function when frozen is True

Now, setting `frozen=True` also generate a hash function for the model
i.e. `__hash__` is not `None`. This makes instances of the model potentially
hashable if all the attributes are hashable. (default: `False`)

* reviewer feedback: use hash of the class instead of the super

* reviewer feedback: fix spelling checks

* reviewer feedback: update changes description

* test: remwork mypy tests in order to catch only frozen related errors

Before: there were errors about other stuff than frozen behavior
After: The modification catch only errot related to the frozen behavior

* test: split test_immutablity in 2 functions

One function tests the behavior: 'the model is mutable'
The other tests the behavior:OC 'the model is immutable'

* test mutability: remove the unnecessary parametrization

* test immutability: remove assertion that do not test frozen behavior
2021-02-23 11:45:40 +00:00
Arseny Boykov ce67660d2f Allow to configure models through class kwargs (#2356)
* add support for class kwargs config

* reformat tests

* add changes file and docs

* fix linting in 'inherit_config'

* tweak docs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-22 17:10:04 +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
Jacob Hayes b742c6f527 Support Annotated type hints and extracting Field from Annotated (#2147)
* Infer root type from Annotated

* Extract Field from Annotated

* Add changelog

* Extend existing get_args/get_origin

* Fix Annotated on py3.6 without typing-extensions

* Handle Ellipsis default

* Fix field reuse after FieldInfo.default mutation

* Fix ci
2021-02-13 16:13:21 +00:00