Commit Graph

172 Commits

Author SHA1 Message Date
PrettyWood 0cee311be5 feat(settings): allow custom encoding for dotenv files (#1620)
closes #1615
2020-06-11 11:04:08 +01:00
PrettyWood 329b1d3e7b fix: type hints on BaseConfig (#1618)
closes #1614
2020-06-09 10:32:07 +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
Koudai Aono 6e29848608 Add datamodel-code-generator link in pydantic document site (#1532)
* Add datamodel-code-generator link in pydantic document site.

* tweak menu

* fix blank lines

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-05-31 15:23:20 +01:00
Shawn Wall 4f9e77d213 1541 UUID bytes support for 16 byte strings / BINARY(16) (#1542)
* [1541] add fallback to UUID instantiation through bytes

* [1541] added changes md file

* Update UUID docs for bytes/bytearray

Co-authored-by: Shawn Wall <shawn@civicscience.com>
2020-05-31 15:05:53 +01:00
John Carter 827388b4fe Add a test assertion that default_factory can return a singleton (#1523) 2020-05-31 14:51:50 +01:00
Alexander Hultnér 2eb62a3b2f Added a "Talks and video tutorials" section (#1499)
* Added a "Talks and video tutorials" section

Added my "Python pydantic Introduction – Give your data classes super powers" talk YouTube video based on the talk for Python Pizza.

* Add changelog entry for video change

Did this after creating the pr so I would have the number to use as a reference.

* Moved video into a discussion subsection

Now placed under testimonials

* Updated change log entry to reflect changes.
2020-05-26 07:50:56 +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
Daniel Burkhardt Cerigo 913025ac3f FIX: validation on model attribute with nested Literal breaks (#1364)
* Add tests for nested literals validator

* Implement flatten literal in validator

* Add test for flatten literal

* Add changelog entry

* Add test skip markers if not Literal

* Refactor: use improved all literals implementation

From Github user PrettyWood, see PR https://github.com/samuelcolvin/pydantic/pull/1364

* Add testing for typing module

Includes moving corresponding tests.

* Remove unnecessary type hint

* Move all literals test to test_utils
2020-05-23 13:01:43 +01:00
Stephen Bunn 1eeb225c67 Fix NameEmail equality comparison (#1527)
* Adding appropriate __eq__ method for NameEmail

- Introducing new test assertions for NameEmail

Signed-off-by: Stephen Bunn <stephen@bunn.io>

* Adding change doc for NameEmail.__eq__

Signed-off-by: Stephen Bunn <stephen@bunn.io>

* Update pydantic/networks.py

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

* Update tests/test_networks.py

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

* Fixing indentation issue left over from suggestion

Signed-off-by: Stephen Bunn <stephen@bunn.io>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-05-19 08:59:33 +01:00
Mike Grima 28c2ac7a43 Added docs on dumping dataclasses to JSON (#1487) 2020-05-18 21:55:49 +01:00
Arseny Boykov 0b9b308ca5 Make signature class only (#1466)
* Make signature class only

* Add changes file
2020-05-18 21:41:24 +01:00
Selim Belhaouane 881df8bde7 move const validator to post validators. fixes #1410 (#1446) 2020-04-30 19:05:42 +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
Samuel Colvin f96c4dc59f uprev 2020-04-23 13:43:34 +01:00
PrettyWood b2871468b7 Fix :: Signature generation with extra: allow never uses a field name (#1420)
* fix :: signature generation does not use field names

fix #1418

* fix :: use field.outer_type_ when generating signature

`extra_data: Dict[str, str]` would generate `extra_data: str` in the signature

* tweak to keep signature kwargs name

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-23 13:42:06 +01:00
PrettyWood 3cd8b1ee2d Fix :: avoid mutating Field default value (#1413)
fix #1412
2020-04-22 11:25:13 +01:00
Samuel Colvin 67acb659c2 prepare v1.5 release 2020-04-18 17:32:16 +01:00
Stephen Brown II cab6313798 Allow subclasses of known types to be encoded with superclass encoder (#1291)
* Add (failing) test for Subclass JSON encoding

* Allow subclasses of known types to be encoded with superclass encoder

* Add change file

* Add documentation, fix custom json_encoders and add unit test

Blacken doc
Fix test that worked on my machine
datetime.timestamp() is flakey?
Single quotes only

* Reduce lookups

- Remove last element in `__mro__` as it will always be `object`
- Use .get for compactness

* Regarding the loop

* Move Path and Enum into ENCODERS_BY_TYPE

Sort ENCODERS_BY_TYPE

* improve JSON docs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-18 17:11:00 +01:00
AlexECX 52af916206 Prevent mutation of excludes/includes (#1404)
* Don't mutate excludes/includes dictionaries inside ValueItems

* Make exludes/includes read-only

* Add changelog

* Update 1404-AlexECX.md

* Add test for includes/excludes immutability
2020-04-18 17:09:06 +01:00
AlexECX 92b0bbc0f3 Bug fix: excludes skipped for aliased nested fields when by_alias=True (#1398)
* Typo in test function name, fix #1397

* Always use a field's real name for includes/excludes

* Add changes.md file
2020-04-18 17:05:20 +01:00
Lance McNearney 161a12a1a1 Update constr regex example to include start and end lines (#1400)
This is due to the regex using `re.match()` which allows for only a partial match, fix #1396
2020-04-18 17:02:22 +01:00
Atheuz 097762b67a make secret types idempotent (#1330)
* make secret types idempotent

* remove idempotent from secret types example.
add note to docs for idempotently creating secret types.
use isinstance for comparing when creating secret types idempotently.
add assert for idempotent secret types test.

* tweaks

Co-authored-by: Lasse Gravesen <lasse.gravesen@falcon.io>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-18 16:10:36 +01:00
Samuel Colvin 9a8bda5b16 confirm shallow copy works as expected, (#1394)
* confirm shallow copy works as expected, fix #1383

* fix linting
2020-04-15 22:58:56 +01:00
Kittipat Virochsiri c5af109e57 Fix argument name collision (#1367)
fix #1366

* Fix argument name collision

* change qoutes

* make mypy happy?

* Adding change file
2020-04-15 22:52:07 +01:00
Dima Boger ccd5f7b8f3 Replace exception to silent passing in mypy plugin for non-Var attributes (#1348)
* Replace exception to silent passing in mypy plugin for non-Var attributes

* Add a history file
2020-04-15 22:46:22 +01:00
PrettyWood 2c05415bae accept None as value for all optional fields (#1307)
* ignore pyenv generated file

* Allow `None` as input to all optional list fields

* skip test on python 3.6

* fix mypy error, line length

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-15 22:43:38 +01:00
Danny Weinberg a015cd01ba Have BaseModel inherit from Representation (#1319)
* Have BaseModel inherit from Representation

This changes the inheritance so that `BaseModel` *actually* inherits from `Representation`, rather than just having the same methods as it. While this doesn't actually affect the runtime, it does make mypy happier.

Fixes https://github.com/samuelcolvin/pydantic/issues/1310

* Add '__dict__' back into slots
2020-04-15 22:07:16 +01:00
Atheuz 7cc5dbeb6a document making secret types dumpable using the json method (#1328)
Co-authored-by: Lasse Gravesen <lasse.gravesen@falcon.io>
2020-04-15 22:05:43 +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 3f04bb3200 fix card number length check in PaymentCardNumber (#1321)
* fix card number length check in PaymentCardNumber

* add check for PaymentCardBrand str()
2020-03-21 17:08:08 +00:00
Samuel Colvin 96c9c400bc Move to GitHub actions (#1326)
* trying move to github actions

* move name

* remove travis and cleanup

* macos and windows coverage

* env_vars string

* bump

* bump

* delete old coverage file

* remove coverage.xml

* add 'make test-codecov'

* fix 'make test-codecov'

* beginning build phase

* combined deploy

* try to fix articacts

* remove set.cfg wheel tag

* install cython before build

* fix windows builds

* fix windows builds

* reenable tests

* build docs, test pypi upload

* finalize

* add change description, fix job conditionals
2020-03-21 17:02:07 +00:00
Stephen Brown II 13b002e97f Add datetime field to default_factory example (#1301)
* Add change file

* Add datetime field to default_factory example

Print instead of assert
Remove bad quotes
Remove bad quotes from all examples

* fix change description

* remove 'avoid-escape' from setup.cfg

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-03-18 20:23:42 +00:00
Michael (Misha) Salim ebb2e6c942 Use __all__ to exclude fields from all elements of a list/tuple of submodels. Raise more informative TypeError when passing bad exclude kwargs. (#1286)
* Raise informative TypeError when passing bad exclude kwargs to export

* Cleanup spurious example

* Add __all__ keyword to enable excluding fields from all members of a list/tuple of child submodels

* Add changelog

* Fix MD format inconsistencies and consolidate export tests

* Excluding from exported List of Dict fields with "__all__"

* tweaks

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-03-18 20:22:49 +00:00
Samuel Colvin e57f777c4d Support instance methods and class methods with @validate_arguments (#1272) 2020-03-17 20:12:30 +00:00
Pavel Ilyin d8262131d2 No dict update (#1244)
* update_forward_refs now modifies only a copy of __dict__ of cls.__module__

* changes

* test for update_forward_refs

* fixed brackets

* black changes fixed

* make format

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-03-17 19:57:13 +00:00
Alexei Robyn d1279e865e Fix parsing nested custom root type models (#1253) 2020-03-17 19:55:48 +00:00
adamgreg 6f4945d84c Accept pre-compiled Pattern objects (#1238)
* Change str_validator() return type to str

* Make pattern_validator() accept pre-compiled Pattern objects.

Resolves #1237.

* fix str_validator return type

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-03-04 17:27:16 +00:00
Ondrej Kokes ddc8e5a57b Fix type annotations in examples (#1248)
* README change to make type annotations valid

* example fixed to have valid type annotations

* changes description

* Update README.md

explicit assignment for an optional field

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

* Update docs/examples/index_main.py

explicit assignment for an optional field

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

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-03-04 17:26:11 +00:00
PrettyWood 78a3f42b0a Dynamic default value (#1210)
* Simplify dynamic default values

closes #866

* Add documentation and change

* Add `ModelField.get_default` method

* Fix default value in `infer`

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

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-03-04 16:37:23 +00:00
Thaïs de Boisfossé 679e5d149e Generics inherit doc (#1249)
* doc: add an example of inheritance with Generics

* Update the changes directory with the doc changes.
2020-03-04 15:40:04 +00:00
Arseny Boykov 0f948618ba Add model signature generation (#1034)
* Add right __init__ signature generation

* Add params overlay support for custom __init__, enhance tests

* Change fake_ini creation to solve AttributeError in cython
AttributeError: attribute '__code__' of 'cython_function_or_method' objects is not writable
Possible there's other attrs that cannot be set in cython, but can't check myself yet

* Cython compatibility, add __signature__, ignore non-identifiers fields
Moved tests to separate file
Moved __init__ creation to utils

* Remove # pragma: no cover

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

* Fixed adding signature to doc, some compatibility fixes

* Changed None to tuple() for py3.8 compatibility

* Finally (hope so) fix issues with closure. Add new docstring each time

* coverage for UndefinedType

* Checking signature more simple and obvious way

* Changed doc caption to simple message, used Undefined, merge conflicts

* Compatibility with py3.6

* Add changes/ file

* Ah, that space... Reformatted and ready to go!

* Update pydantic/main.py

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

* Update pydantic/main.py

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

* Fix signature for properties declared as fields

* Sort imports

* Add comments

* Generate BaseModel signature instead of __init__ signature

* Fixed imports

* Minor improvements from review

* Add docs for model signature

* Delete unused imports

* Fix formatting in tests

* Remove inspect imports from top level

* Update docs/examples/models_signature.py

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

* Add missing import

* Remove notes from doc

* change hypothesis-auto mention to hypothesis

* Use None as slice end instead of len()

* tweak generate_model_signature

* improve docs

* Revert af3dd4d, add fields to custom init only if var_kw declared

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-02-27 17:35:51 -05:00
Jocelyn Boullier f2f83bcae2 Add support for NewType of List, Optional, etc. (#1208)
* add support for NewType of List, Optional, etc.

* bump

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-02-27 17:14:53 -05:00
Arseny Boykov 7d9614e3f8 Refactor ._iter() method, 10x speed boost for dict(model) (#1017)
* Refactor ._iter() method:
Moved all keys-related stuff (include, exclude, etc.) to ._iter()
Removed redundant iteration through default values
Almost all arguments checks moved out of loops, so checks happen once
Fast yield from .__dict__ on plain .iter() (x10 boost)
Removed redundant set(dict.keys()) in ._calculate_keys()

* Moved back from nested generator checks to checks, optimized copy a bit

* Bump pytest-mock from 1.12.0 to 1.12.1 (#1018)

Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 1.12.0 to 1.12.1.
- [Release notes](https://github.com/pytest-dev/pytest-mock/releases)
- [Changelog](https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst)
- [Commits](https://github.com/pytest-dev/pytest-mock/compare/v1.12.0...v1.12.1)

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

* __str__ and __repr__ inheritance for models, fix #1022 (#1023)

* add testimonials section to docs with reference to python bytes podcast episode (#1025)

* add testimonials section with reference to python bytes podcast episode

* added description to changes directory

* Bump twine from 3.0.0 to 3.1.0 (#1029)

Bumps [twine](https://github.com/pypa/twine) from 3.0.0 to 3.1.0.
- [Release notes](https://github.com/pypa/twine/releases)
- [Changelog](https://github.com/pypa/twine/blob/master/docs/changelog.rst)
- [Commits](https://github.com/pypa/twine/compare/3.0.0...3.1.0)

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

* Support typing.Literal in python 3.8 (#1027)

* Support typing.Literal in python 3.8

* Improve import pattern for Literal

* Update references to  in docs

* Try to get build to pass

* Add support for mapping types as custom root (#958)

* Add support for mapping types as custom root

* Incorporate feedback

* Add changes

* Incorporate feedback

* Add docs and tests

* Fix linting issue

* Incorporate more feedback

* Add more specific match

* Add parse_as_type function (#934)

* Add parse_as_type function

* Add changes

* Incorporate feedback

* Add naming tests

* Fix double quotes

* Fix docs example

* Reorder parameters; add dataclass and mapping tests

* Rename parse_as_type to parse_obj, and add parse_file

* Incorporate feedback

* Incorporate feedback

* use custom root types

* Add better support for validator reuse (#941)

* Add better support for validator reuse

* Clean up classmethod unpacking

* Add changes

* Fix coverage check

* Make 3.8 compatible

* Update changes/940-dmontagu.md

Co-Authored-By: Samuel Colvin <s@muelcolvin.com>

* Make allow_reuse discoverable by adding to error message

* switch _check_validator_name to _prepare_validator

* Add changes file

* Delete unrelated files

* Add check that k in fields before using alias

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

* Remove redundant call to __iter__

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

* Use typing.AbstractSet

* Update pydantic/main.py

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

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Colin Sullivan <csullivan@brandwatch.com>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
2020-02-27 15:21:22 -05:00
Leo Sussan c4e42746f3 add pydantic.color.Color objects as available input for Color fields (#1258)
* feat: add pydantic.color.Color as available type for Pydantic color fields

* chore: test for color

* chore: change md

* fix: assertion is incorrect

* fix: logic on _original assignment

* fix: value is set to value._original in the isinstance conditional
fix: remove ._original conditional (logic is set earlier in the method)
fix: removed pydantic.color.Color line from errorstring

* chore: revert error message adjust

* chore: grammaer ;)
2020-02-26 15:25:24 -05:00
Samuel Colvin 6914410f38 Validation Decorator (#1179)
* starting validation_decorator

* correct skip_pre_38

* fix coverage and type hints

* mypy tests and move to class based decorator

* 3.6 fix, prevent duplicate github actions

* correct py 3.6 check

* better errors

* cleaner field names for args and kwargs

* add change and comments

* starting docs

* back to 3.7 for docs

* docs

* bump

* add async example and fix print indents

* allow type annotations as strings

* python 3.8 in docs
2020-02-05 17:27:12 +00:00
Samuel Colvin 55f7d35727 update root_validator signature, fix #1192 (#1204) 2020-02-01 16:59:32 +00:00
Samuel Colvin e4cd9d2c87 uprev 2020-01-24 10:48:44 +00:00