Commit Graph

44 Commits

Author SHA1 Message Date
PattyC d0e5f98be4 new to_lower_camel() function (#3473)
* Create utils.py

I needed non pascal case camel case support

* write tests for new `to_lower_camel()` function

* Create 3463-schlerp.md

* added mention to to_lower_camel()

* changed quotes to single tick

* adding second blank line at end of file.

* again, adding second blank line...

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-05 14:18:19 +00:00
Samuel Colvin 8846ec4685 limit the length of generics._limit_assigned_parameters (#4083)
* limit the length of generics._limit_assigned_parameters

* switch to using _limit_cache_size for both

* add change description

* correct `_limit_cache_size` cache

* implemented LimitedDict

* try using UserDict

* try upgrading cython

* stop LimitedDict from inheriting from dict

* separate LimitedDict for typing checking :-(

* fix for __class_getitem__
2022-05-18 15:22:58 +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
Christian Clauss 90080ba0de Fix typos discovered by codespell (#3096) 2021-09-04 00:05:36 +02:00
Eric Jolibois 4a54f393ad Add python 3.10 support (#2885)
* refactor: extra `BaseConfig` and `Extra` in dedicated `config` module

* refactor: clean useless `#noqa: F401`

* refactor: clean useless `#noqa: F811`

* refactor: replace enum check

Error with 3.10
> DeprecationWarning: accessing one member from another is not supported

* refactor: avoid using `distutils` directly

error with python 3.10
> DeprecationWarning: The distutils package is deprecated and slated
> for removal in Python 3.12.
> Use setuptools or check PEP 632 for potential alternatives

* fix: `__annotations__` always exists

* fix: origin of `typing.Hashable` is not `None`

* ci: add run with 3.10.0b2

* docs: add 3.10

* feat: support `|` union operator properly

`|` operator has origin `types.Union` (and not `typing.Union`)

* fix: enum repr is different with 3.10+

* fix: error message changed a bit

change from basic `__init__` to `test_hashable_required.<locals>.MyDataclass.__init__()` (with `__qualname__`)

* fix:  always exists and is not inherited anymore

* fix: avoid calling `asyncio.get_event_loop` directly

With python 3.10, calling it results in
> DeprecationWarning: There is no current event loop

* fix(ci): do not run 3.10 on linux for now

For now it can not be compiled.
Let's just skip the check on linux for now instead of tuning the CI pipeline

* fix(ci): ignore DeprecationWarning raised by `mypy` on windows

* docs: add change file
2021-07-19 14:23:07 +01:00
Eric Jolibois 0b3c5631bd fix: generate a valid name in schema for Enum classes (#2582)
* fix: generate a valid name in schema for `Enum` classes

* test: add schema with generic enum

* (erase me): just to check fastapi tests

* Revert "(erase me): just to check fastapi tests"

This reverts commit f334c4b575d413639d26c17c2c36d30333afcf7c.

* fix linting 😴

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-05-09 14:11:48 +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
Yurii Karabas 5261fd05a0 Add missed assert statement (#2662) 2021-04-12 22:55:28 +02:00
Samuel Colvin 2ee6811655 remove DeprecationWarnings from v1 release & fix coverage (#2415)
* remove DeprecationWarnings from v1 release

* coverage on unpickling Undefined private attributes

* coverage on undefined in copy, allow Undefined to be pickled unchanged

* fix coverage of model._iter()
2021-02-26 14:43:47 +00:00
Davis Kirkendall aa92db5e4f Add test for generic aliases and lenient_issubclass (#2392) 2021-02-25 17:08:21 +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
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
Davis Kirkendall 6b8d2babaa Fix #947, #1483, #1247: allow inner type vars to be present in parent generic classes (#1989)
* Fix #947, fix #1483, fix #1247 allow inner type vars to be present in parent generic classes

- Rename generics._is_type to _is_generic in response to comment:
  https://github.com/samuelcolvin/pydantic/pull/1989#discussion_r503400391
- Add more explicit type assertion in generics test
- Add generics tests and unify naming
- Move deep generic tests all into same place in code
- Unify naming convention in deep generic tests using naming of existing tests
- Add tests for multiple inheritance and multiple type vars
- Add generic tests for type hint resolution cases
- Fix edge cases for handling parameters in generic models
- Resolve parameters correctly
- Add tests for special cases like callables
- Add returning generic type directly if parameters and arguments are
  identical.

* Apply review comments

 - Add docstrings
 - Small refactor `generics.__concrete_name__`
 - Small refactor of `generics.resolve_type_hints`
 - Change `is_identity_typevars_map` to more generic and clearer
  `all_identical` and move into utils.
 - rename "resolve_type_hint" to "replace_types" so as to not get
   confused with "resolve_annotations" from `pydantic.typing`

* Fix generics test coverage

* Update pydantic/generics.py, remove unneeded annotation

mypy understands list comprehensions as they are now

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2020-12-30 18:23:55 +00:00
Samuel Colvin 8b4e726be2 Fix typing coverage (#2032)
* fix coverage

* linting

* correct forward ref import

* fix conlist __args__

* chore: remove useless except

* skip test_get_args prior to 3.8

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2020-10-26 10:25:48 +00:00
mdgilene 30cd121cfe Add ability to read secrets from files (#1820)
* Add ability to read secrets from files

* Added docs and updated tests to handle new _build_values argument

* Updated docs to note which types are loaded

* Updated deep_update to take a list of update mappings

* Added warning when fields are not defined as a secret type

* Address additional PR feedback

* improve documentation

* remove redundant UserWarning lines

* allow any field type, not just SecretStr

* regex escape paths in windows 😴

* skip tests on windows 😧 🔫

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-26 10:06:25 +00:00
Samuel Colvin a43c2d2670 Revert "fix coverage"
This reverts commit 6e0c5c336c.
2020-10-25 13:45:46 +00:00
Samuel Colvin 6e0c5c336c fix coverage 2020-10-25 13:44:09 +00:00
Davis Kirkendall 30ee8e2203 Force fields.Undefined to be a singleton objectIn various places of the code, we compare directly to fields.Undefinedsince we assume it to be constant.When new models get created however, the object is deepcopied andis no longer identical with the original object.We therefore add __copy__ and __deepcopy__ methods to ensurethat the copied objects are actually the same original object. (#1981) 2020-10-09 13:25:24 +01:00
Arseny Boykov d5e9d9abc8 add smart_deepcopy (originaly from #1679) (#1920)
* add smart_deepcopy

* uncomment tuple in BUILTIN_COLLECTIONS, fix doc a bit

* Fix grammar

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

* replace map() usage with generator comprehension, fix comment

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2020-10-08 19:50:54 +01:00
beezee e3c5e1d3cd aggregate root validation errors (#1586)
* aggregate root validation errors

* add changelog

* update language in docs around root validators and prior failure

* factor out unique_list
2020-07-03 20:47:04 +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
Arseny Boykov 0b9b308ca5 Make signature class only (#1466)
* Make signature class only

* Add changes file
2020-05-18 21:41:24 +01:00
Samuel Colvin e169bd60e4 encourage use of version_info() in issues (#1138)
* encourage use of version_info() in issues

* improve grammar and add change

* switch test import
2020-01-02 19:34:33 +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 9255012c08 add version_info to make creating issues easier (#1071)
* add version_info to make creating issues easier

* improve version_info()

* improve contributing docs
2019-12-04 15:14:53 +00:00
Aviram Hassan 1b9f08bead Dataclass is now compatible with schema methods (#1045) 2019-12-02 12:44:21 +00:00
Samuel Colvin 151143deb4 coverage for UndefinedType 2019-11-28 18:28:51 +00:00
Samuel Colvin 78921da353 better str and repr for ModelField (#912)
* better str and repr for ModelField, fix #505

* better type display, fix tests

* correct _type_display signature

* fix for python3.6 differences

* fix PyObjectStr

* fix coverage
2019-10-18 14:32:30 +01:00
Ivan Dmitrievsky 806eba3810 Merge init_kwargs with env variables recursively to handle nested values (#891)
* Merge init_kwargs with env variables recursively to handle nested values

This commit implements `deep_update` function that works as standard
`update` method on dicts, but also recursively updates all the nested
dict values. It allows splitting init arguments between environment
variables and in-code values, as long as they create a valid object
when merged together.

* Add entry to changes directory

* Fix CI/CD checks

* Make deep_copy not mutating, improve tests and type annotations
2019-10-15 11:24:46 +01:00
Samuel Colvin c3098a30cf Consistent __repr__ and __str__ methods for all types (#884)
* Consistent __repr__ and __str__ methods for all types

* add change description

* devtools integration and feedback on repr methods

* fix Color repr

* tests for truncate

* add devtools section to docs

* tests for devtools

* ValidationError inheriting from Representation

* fix imports

* tweaks

* tweak docs

* exec_examples.py integration with __repr__ changes
2019-10-14 17:37:04 +01:00
Samuel Colvin 79017111aa new URL parsing (#755)
* new URL parsing, fix #603, fix #541

* AnyUrl parts and more tests

* more coverage and db DSNs

* remove DSN methods

* tests for urlstr

* remove debug

* make AnyStr a subtype of str

* fix with cython

* rearranging networking code

* allowing international domains, cleanup

* support international domains

* better URL builder

* allow underscores in subdomains and domains

* tests for json and schema, max length

* urlstr > stricturl

* updating docs

* tweak docs examples

* tweak docs
2019-09-02 11:37:33 +01:00
Samuel Colvin 5f634067da moving typing related objects into typing.py (#761)
* moving typing related objects into typing.py, fix #713

* cleanup
2019-08-21 12:49:19 +01:00
Arseny Boykov 74768c1535 Add advanced exclude and include support for dict, json and copy (#648)
* Add advanced exclude support for dict, json and copy

* Add advanced exclude support for dict, json and copy

Add new version section (v0.31)

* Add advanced include support, add more tests, improve code style
Rename ValueExclude to ValueItems and move it to utils
Use old logic to calculate keys, but still exclude it in _iter

* Add more tests for ValueItems

* Removed update arg check in _calculate_keys for return None
This will increase speed when no include or exclude given and skip_defaults is False

* Fix formatting, remove duplicate imports

* Add # pragma: no cover to 'if TYPE_CHECKING:' block

* tweaks and coverage

* fix history

* Add docs

* tweak docs
2019-07-24 09:58:38 +01:00
dmontagu 18d4b2bb2a Make schema work for Literal and NewType (#649)
* Make schema work for Literal and NewType, fix #646

* Fix test when typing_extensions not installed

* Make literal values fetching 3.6 compatible

* Rework to get around issues with Literal in python 3.6

* Make cython-compatible

* Use union for multiple allowed literal values

* Minor simplification of values-gathering

* Updated history
2019-07-15 20:06:35 +01:00
dmontagu ccdf8e12cd Fix #608 (repr on model with class) (#611)
* Fix #608

* add try-except

* Fix quotes
2019-06-26 14:03:33 +01:00
Samuel Colvin 7a06736ead Add additional parameters to Schema for validation and annotation (#319)
* Add additional parameters to Schema for validation and annotation (#311)

* Add tests for validation declared via Schema class in defaults

* Add validations to field from declarations in Schema

* Add annotations in generated JSON Schema from validations via Schema

* Augment tests for schema generation

* Simplify validations from Schema in fields, from coverage hints

* Update schema test to use the spec plural "examples"

* Add docs and simple example of the additional parameters for Schema

* Update history

* Fix number of PR in HISTORY

* Refactor check for numeric types, remove break to make coverage happy

* Fix typo in docs, I confused gt with maximum

* Finish docstring for Schema (I had forgotten about it)

* Implement code review requests and lenient_issubclass with tests

* Move Schema to its now file to extract from fields.py but avoid circular imports

* Control coverage

* Schema fixes (#318)

* rearrange code

* cleanup get_annotation_from_schema

* fix typo

* rename _schema to schema
2018-12-03 12:37:39 +00:00
Sebastián Ramírez 94706bc834 JSON Schema update/refactor/augment, to conform to spec (#308)
* Update schema tests to conform to JSON Schema spec

* Add JSON Schema tests for all supported types

including datetime and all supported Pydantic.types

* Add JSON Schema conforming schema sub module

* Update BaseModel to use schema module for JSON Schema generation

and update/simplify internal Schema methods

* Remove Schema code from Field class, replaced with JSON Schema module

* Add submodules to test model name generation for JSON Schemas

* Refactor/rewrite schema module to generate definions and refs

* Update and augment JSON Schema tests to include definitions and refs

and generation of a single JSON Schema with definitions from multiple (unrelated) models

* Add ref_prefix functionality to JSON Schema generation functions

* Test custom ref_prefix in JSON Schema generation

* Remove un-used BaseModel method, now refactored to schema module

* Update formating of test_schema

* Fix long lines in test_schema

* Fix imported but unused in fields

* Fix imported but unused in main.py

* Ignore imported but unused for testing modulec

* Refactor schema module for complexity

* Add conflicting name model to raise coverage

* Add conflicting model to test other flow and raise coverage

* Ignore complexity as destructuring more would make it more complex

and more difficult to understand, similar to .fields.validate

* Fix import sorting

* Update formatting with black, with CI settings

* Fix test for schemas with email validation

* Check if field is class before checking if is subclass

* Improve schema error when using unsuported types

* Add additional tests for corner cases, raise coverage to 100%

* Rename BaseModel.schema_json to schema_str (EAFP Python style)

* Add more tests to utils.display_as_type to increase the coverage for enums

* Remove unused catched error in schema tests

* Fix formatting with black

* Update docs schema example

* Add schema examples for top-level schema with multiple models

* Update docs, section Schema, with new JSON Schema generation details

* Update docs, history, with new features

* Update fields, remove unnecessary schema code for enums

* Update docs, fix links and typos in Schema section

* Trigger CI, as Python 3.7-dev seems to have random CI errors

* Revert Model.schema_str to Model.schema_json as requested

* Remove unnecessary assert in schema module as requested

* Remove annotations in internal functions, as requested

* Refactor get_flat_models_from_fields and reuse

* Use set short assignment syntax in schema module

* Remove unwanted assertion

* Make get_long_model_name a single line f-string

* Update model_name_map, add docstring and remove first return value

* Simplify dict operation in get_model_name_map as requested

* Make more concise model_name_map computation

* Remove bool from field check in schema as is subclass of int

* Make ref_prefix default to None and use global default

* Fix formatting for schema.py

* Refactor field_singleton_schema to use data structures

* Move main functions to top of schema, and add docstrings for them

* Implement __all__, move and order parts of schema

* Remove schema testing sub-package code as requested

* Generate schema testing subpackage in code

* Update schema tests with several related fields to use parametrized pytest

* Fix formatting and imports I missed after rebase

* Fix new formatting errors from CI

* Re-trigger Travis CI, Python 3.7-dev random error again, no re-run click in Travis for non owners

* Trigger annotation error with non-forward references

* Add docstrings for submodel schema

* tweaks and rewrite schema mapping table in python

* support complex defaults

* use str not int as dict keys

* Fix links to JSON Schema and OpenAPI
2018-11-22 16:00:06 +00:00
Samuel Colvin 15850a43c5 moving to black (#287)
* moving to black

* put back flake8

* remove isort option

* putting back isort

* uprev pycodestyle

* remove black from docs/examples

* tweak parse.py
2018-11-15 11:30:07 +00:00
Samuel Colvin 31f5f9c267 Cleanup 183 (#187)
* fix coverage

* use change_exception

* simplifying get_exc_type

* improve to_snake_case

* improve change_exception

* remove to_snake_case completely

* correct comment
2018-06-02 18:08:07 +01:00
Nikita Grishko 31683f8dc4 Errors format (#179)
* Get rid of `track` in errors

* Move `display_as_type` func into utils module

* Get rid of error as `namedtuple`

* Renamed `Error.index` to `Error.loc`

* New way to get error type

* New way to get error message

* New errors format

* Renamed `flatten_errors` property to `flat_errors`

* `__slots__` for `Error` and `ValidationError`

* `loc` as `tuple`

* Tests

* Review fixes

* `flatten_errors` as generator
2018-05-23 14:50:04 +01:00
Samuel Colvin afb90b91e8 implement email validator (#142)
* implement email validator, replace #112

* coverage and cleanup

* update docs
2018-03-25 18:09:37 +01:00
Samuel Colvin 45cba8b1ae fixing isort, adding deploy recipe 2017-06-03 16:18:02 +01:00
Samuel Colvin c4f9398d79 model validator and utils tests 2017-05-06 15:35:04 +01:00
Samuel Colvin af6ab2b7e5 add email address validation 2017-05-05 22:01:10 +01:00