Commit Graph

479 Commits

Author SHA1 Message Date
Talley Lambert beb3e4c8f1 Fix duplicate calls to __set_name__ for non-private attributes in BaseModel.__new__ (#4410)
* fix: double call for public attrs, extend test

* add changes

* remove change file, update 4407 file
2022-08-22 11:16:53 +00:00
Talley Lambert d501c39f2b Fix PEP487 __set_name__ protocol in BaseModel (#4407)
* fix: fix __set_name__ protocol in BaseModel

* undo changes

* add comment

* fix lint

* fix lint

* add change file

* remove mock, move imports
2022-08-21 13:53:26 +00:00
Hasan Ramezani abcf81ec10 Rename master to main (#4405) 2022-08-19 18:49:13 +01:00
DetachHead 9585ef3475 fix dataclass containing Any (#4356)
Co-authored-by: detachhead <detachhead@users.noreply.github.com>
2022-08-19 11:46:09 +01:00
Fares Ahmed e34ff9255f Fix AnyUrl.build doesn't do percent encoding (#3061) (#4224)
* Fix `AnyUrl.build` doesn't do percent encoding (#3061)

please review

* Use `urllib.parse` instead of custom implementation for percent encoding

* Make `quote_plus` a `stricturl` option

* Add docs for `stricturl(quote_plus=True)`

* Better changes message for #3061

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

* Fix precent encoding version note to V1.10

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

* Fix `Building URLs` snippit markdown

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

* Fix formatting for `docs/examples/types_url_building.py`

* fix docs

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-19 09:20:20 +00:00
chbndrhnns ba58f94150 feat: Make SecretStr hashable (#4399)
* feat: Make SecretStr hashable

* docs: Add change documentation

* test: Assert that the hash of SecretStr is an integer value

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>

* feat: Make SecretBytes hashable

Co-authored-by: Johannes Rueschel <johannes.rueschel@telekom.de>
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
2022-08-19 09:13:30 +00:00
Hasan Ramezani 899b5f0661 Fix StrictStr does not raise ValidationError when max_length is present in Field (#4388) 2022-08-19 09:51:59 +01:00
Jean 67b653f19a fix: strictbytes-max-length (#4383)
* fix: strictbytes-max-length

* fix: apply feedbacks

* Update changes/4380-JeanArhancet.md

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
2022-08-16 23:41:17 +02:00
rafael 7344374dd0 BaseModel: Document and test structural pattern matching (#3921)
* BaseModel: Add __match_args__ attribute to namespace

* changelog

* amend docs

* add tests

* Update changes/3920-irgolic.md

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

* Update .github/workflows/ci.yml

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

* rename test_match => test_structural_pattern_matching

* pytest.skip => pytest.mark.skipif

* Revert "BaseModel: Add __match_args__ attribute to namespace"

This reverts commit 79367356fddac90b047b10620dc895425ec25b40.

* adjust tests for kwarg only structural pattern matching

* adjust docs for kwarg only pattern matching

* changes: Support => Document

* fix docs?

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-08-16 08:47:17 +02:00
Hasan Ramezani dab478aa81 Add support for bare type (#4375) 2022-08-14 21:52:34 +01:00
Samuel Colvin cc68900274 Build binaries for Python 3.11 (#4374)
* build binaries for python 3.11

* add change

* [skip ci] revert CI if comment

* add 3.11 to pypi classifiers
2022-08-14 18:32:33 +01:00
Arseny Boykov c24d33e5f1 Generate docs exampels for Python 3.10 and above (#4339)
* Generate docs exampels for Python 3.10 and above

Code quality is not great and main intent here is to show the result.

* Fix docs build on 3.9

* Build docs on 3.10

* What's Python 3.1?

* Create temp dir if not exists

* Refactor and improve imlementetion

* Keep runtime typing in examples

* Revert unrelated formatting changes

* Add changes file

* Allow specifying requirements in examples

* Pin autoflake and pyupgrade

* Add docs/build to Makefile lint/format/mypy

* ignore_missing_imports for ansi2html and devtools

* Add .tmp-projections to .gitignore

* Remove dont-upgrade now when Pattern is supported

* Update postponed evaluation examples

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-14 17:21:53 +01:00
hottwaj cdd091c8ac Initial implementation of ConstrainedDate, condate (#3740)
* Initial implmenetation of ConstrainedDate, condate, with tests and docs

* Attempt at fixing hypothesis tests for ConstrainedDate.  Suspect still not working

* removed forgotten breakpoint() :(

* suggested changes to pydantic/_hypothesis_plugin.py for condate etc

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>

* - fixed import for hypothesis tests

* - linting fixes only

* - more fixes for linting

* changes following review by samuelcolvin 2022-08-08.  Will rebase next

* - added changes to changes file

* fixed changes file filename to PR #3740

* additional changes requested by samuelcolvin 2022-08-11: add test for schema_json, add hypothesis test, etc

* make test_json_schema compare to a dictionary rather than json string

* added test_date_constrained_types to test_schema

* rewrite tests

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-12 14:37:41 +00:00
KotlinIsland 03606916e4 Fix mypy version check (#3783)
* fix mypy version check

* fix mypy version check

* implement parse_mypy_version and test

* add change file

Co-authored-by: KotlinIsland <kotlinisland@users.noreply.github.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-12 15:07:43 +01:00
Kyungmin Lee 0320a99361 feat: add support multiple dotenv files (#1497) (#3222)
* feat: support multiple dotenv files

Allow BaseSettings to accept multiple dotenv files in Config as a list
or tuple.

* docs: add multiple dotenv support

* docs: add a change summary

* test: add missing tests

* fix: test coverage

* refactor: raise a TypeError when an object of an invalid type is passed

* fix: a lint error

* refactor: remove `_read_process_env_vars` method

* docs: update priority

* refactor: apply a review

* test: simplify testing

* refactor: apply reviews

* reverse piority order, reduce diff size

* fix change file grammar

* correct docs, avoid unnecessary copy of os.environ

* correct wording in docs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-12 12:17:01 +00:00
Hasan Ramezani 104748c793 Add support for re.Pattern (#4366) 2022-08-12 12:36:15 +01:00
Samuel Colvin 47362e33b6 Pick 1.9 (#4350)
* generate history from changes, uprev

* Pydantic V2 blog (#4218)

* first draft of pydantic V2 blog

* more blog

* blog rendering and formatting

* more section

* completing conversion table

* prompt build

* reviewing blog post

* more reviewing and extending

* recommendations from @Rabscuttler and @PrettyWood

* add implementation details and more suggestions

* comment about breaking changes

* convert namespae to table, more removals

* Apply suggestions from code review by @tiangolo

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>

* feedback from @tiangolo's review

* changes from @adriangb's review

* Apply suggestions from code review

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>

* convert namespace info to psuedo-code

* rename property, remove schema_json()

* adding validation context

* remove 'model_schema_json', take 2

* more tweaks while reviewing

* comment about pypy and tagged unions

* add thanks :prey:, prepare for release

* suggestions from @PrettyWood

* suggestions from @PrettyWood, model_dump_json comment

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>

* comments mostly from @PrettyWood (#4226)

* comments mostly from @PrettyWood

* add updated comment

* fix pre-commit

* allow for shallow copies (#4093)

* allow for shallow copies

* Add changes file

* tweak change

* update for comments

* rename attr

* use single quotes

* bump ci

* add warning if not a string, switch to string literals

* fix linting, prompt ci

* fix ci

* extend and fix tests

* change default to "shallow"

Co-authored-by: Samuel Colvin <s@muelcolvin.com>

* uprev and prepare for release

* linting

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Co-authored-by: Tim Paine <t.paine154@gmail.com>
2022-08-12 11:09:49 +01:00
Samuel Colvin 5c8942bdd4 Revert "Add nested json encoding (#3941)" (#4367)
This reverts commit b42fae081c.
2022-08-12 11:08:33 +01:00
Matt Coneybeare b8a695788e Catch Exceptions in smart_deepcopy (#4187)
* Catch Exceptions in smart_deepcopy

* address feedback
2022-08-12 07:56:47 +00:00
Amin Alaee fa6b9788a1 Allow BaseModel schema to have fields of type Type (#4154)
* Allow BaseModel to have fields of type Type

* update

* minor update

* update tests

* update

* fix type

* update

* Update changes/4051-aminalaee.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-08-11 11:47:30 +00:00
Amin Alaee 1f83221323 Fix imports indirectly from pydantic typing (#4358)
* Fix imports indirectly from pydantic typing

* fix pipeline

* Add MappingIntStrAny to __all__

* fix ForwardRef

* Fix Callable

* remove from ForwardRef imports

* Add changes
2022-08-11 12:35:56 +01:00
DetachHead 1e821b6df4 fix "extra fields not permitted" error when dataclass with Extra.forbid is validated multiple times (#4354)
Co-authored-by: detachhead <detachhead@users.noreply.github.com>
2022-08-11 11:23:57 +00:00
Kyle Amos a587ecee88 Fix #4192 bug with BaseModel.construct and aliased Fields (#4191)
* Fix a bug where BaseModel.construct would not appropriately respect field aliases

* Perhaps do not raise on construct and just apply the fix

* Fix quotes and remove check on allow_population_by_field_name

* Fix lint

* Fix lint, remove bad arg

* Black formatted

* Mmmm black formatter and single quotes linting, what a world

* Added change file

* PR feedback

Co-authored-by: Kyle Amos <kamos@seatgeek.com>
2022-08-11 11:49:07 +01:00
Max McLennan 4cb58cf9df Discriminated union schemas use oneOf instead of anyOf (#4335) 2022-08-11 11:42:28 +01:00
Hasan Ramezani 149c05bc2e Fix __post_init_post_parse__ is incorrectly passed keyword arguments when no __post_init__ is defined (#4361) 2022-08-11 12:40:50 +02:00
arsenron 6650edfe8f Fix #3807: matching characters in nested env delimeter and env prefix (#3975)
* fix nested env delimeter with matching prefix

* Update env_settings and add change readme

* fix change and add comment

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-11 11:35:03 +01:00
chenyijian 02a2a8b5c0 create_model support generics model (#3946)
* create_model support generics model

* fix change.

Co-authored-by: chenyijian <chenyijian@mycapital.net>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-08-11 10:34:24 +00:00
Patrick Arminio f2c3a49d66 Don't override __init__ method in MyPy plugin if it already exists (#3824)
* Don't override pre-existing `__init__` method in MyPy plugin

* Add change file
2022-08-10 10:54:38 +01:00
David Robertson 11d8589423 Teach mypy plugin that validator methods are classmethods (#4102)
* Teach mypy plugin that validator methods are classmethods

For your consideration: a patch which implements the suggestion I made here:
https://github.com/samuelcolvin/pydantic/discussions/4101

Briefly: pydantic automatically wraps validator methods using
`@classmethod`. Hence the first argument to a user-provided validator
should be `cls`. But mypy doesn't know this: it analyses validator
methods like any other regular method, believing the first parameter
`cls` to be a model instance (usually denoted self).

This means that if one annotates `cls` as `Type[...]` then mypy
believes raises an error:

	error: The erased type of self "Type[temp.Model]" is not a supertype of its class "temp.Model"

I concede that this is an extremely niche thing. The only tangible
end-user benefit I can think of is that it'll stop you from calling
instance methods in a validator.

----

I haven't written a mypy plugin before, so this is a bit of a
hack-until-it-works. But it was more straightforward than I expected to
get something working!

* changelog

* Add failure test
2022-08-09 17:04:05 +01:00
Daniele Esposti 6afc0c695a Added abstract SecretField class for secret fields (#3717)
* Added abstract SecretField class for secret fields

* Added changes for changelog
2022-08-09 15:47:15 +00:00
Arseny Boykov 0500610ec5 Typecheck Json inner type (#4332)
* Add .venv/ to .gitignore

* Allow typecheckers to infer Json inner type

* Fix and improve mypy tests

* Add type tests

* Add Json[Any] case to schema test

* Update example in docs

* Add changes file

* Use <3.9 compatible annotations for tests
2022-08-09 11:49:27 +01:00
Michael Milton 86b8486c1d Update to PEP 681 dataclasses (#4241)
* Update to PEP 681 dataclasses

* Add changes file

* Pre-commit

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-09 10:07:35 +00:00
Jacob Hayes b59a85a20c Resolve ForwardRef on Field.outer_type_ (#4249)
* Add tests checking Field.outer_type_ ForwardRefs

* Update root ForwardRefs in field.outer_type_

* fix strings

* Add changes file
2022-08-09 10:57:56 +01:00
Rory Byrne f6c74a55d5 feat: pass config to NamedTuple fields (#4225)
* feat: pass config to create_model_from_namedtuple

* feat: tests for arbitrary_types_allowed in NamedTuple fields

* misc: add changelog file for the PR
2022-08-08 20:33:30 +01:00
Gustavo Satheler d90def3c4e Add to upper function for strings and bytes (#4165)
* feat: add to upper function for strings and bytes

* docs(changes): add message for change

* fix: add constr upper on types

* fix: add constr upper on types

* feat: add examples and doc usage

* test: add test to upper for types

* chore: apply suggestions from code review

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>

* chore(docs): reorder `anystr_upper` to under `anystr_lower`

* fix(test): adjust parametrizes to constrained bytes upper

* refactor: use pytest parametrize for unify test constrained str upper

* refactor: use pytest parametrize for unify test constrained str lower

* refactor(test): use pytest parametrize for unify test any str upper

* refactor(test): use pytest parametrize for unify test any str lower

* refactor(test): use pytest parametrize for unify test constrained bytes lower

* refactor(test): use pytest parametrize for unify test any str strip whitespace

* refactor(test): change test signatures to improve readability

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
2022-08-08 17:06:00 +01:00
Amin Alaee 65a1381591 Fix schema for set and frozenset with default value (#4155)
* Fix schema for Set with default value

* add changelog

* Add frozenset to schema mapping docs
2022-08-08 17:02:04 +01:00
Lily Wang b42fae081c Add nested json encoding (#3941)
* add nested json encoding

* fix timezones

* add changes doc

* split tests and rename example

* Update changes/3941-lilyminium.md

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

* split tests into functions

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 15:46:51 +00:00
Amin Alaee 8f388e1e30 Add default to JSON schema when const is True (#4152) 2022-08-08 16:44:49 +01:00
Richard Xia 02a076d14f mypy plugin: More precisely detect when fields are required. (#4086)
The mypy plugin would previously incorrectly determine that a field was
not required in a few scenarios where the field really is required. This
specifically affected cases when the `Field()` function is used, where
the plugin assumed that the first argument would always be `default`.

This changes the code to examine each argument more closely, and it now
properly handles several more scenarios where the default is explicitly
named or when the default_factory named argument is used.
2022-08-08 16:29:26 +01:00
Eric Jolibois da8f0c034e support overwriting dunder attributes of BaseModel instances (#3907)
* support overwriting dunder attributes of `BaseModel` instances

closes #3777

* suggestion from @adriangb

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 15:15:01 +00:00
Tiemo cf16f7c388 Add CockroachDsn type (#3830)
* Add CockroachDsn type

The CockroachDsn type supports the following dialects: cockroachdb,
cockroachdb+psycopg2 and cockroachdb+asyncpg. It's meant to be used in
conjunction with the cockroachdb sqlalchemy dialect, more information
can be found here:
https://github.com/cockroachdb/sqlalchemy-cockroachdb

* tweak change log

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 15:14:35 +00:00
strue36 4d87699363 Adds reserved word check to signature generation logic. (#4012)
* Adds reserved word check to signature generation logic.

* Improve documentation.
2022-08-08 15:47:23 +01:00
SergioSim c356011892 Fix Json strategy serialization failure (#4005)
When generating a pydantic model having nested Json fields with
hypothesis, a JSON serialization exception was raised.
2022-08-08 15:45:21 +01:00
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
Pepa ef4a5d3315 fix: add correct handling for negative time deltas of iso format serialization (#3899) (#3909)
* changed timedelta_isoformat in pydantic/json.py to produce ISO 8601 compliant string even for negative values
* added tests to verify correct serialization of negative time deltas

Co-authored-by: Josef <sorry.i-keep@it.private>
2022-08-08 14:54:09 +01:00
Charlie Hornsby cd439a4e8d fix: access discriminator field on BaseModel instance using key (#3847)
When validating a discriminated union where the union value has been
passed as a Pydantic model instance, we should access the discriminator
field value using the field name and not the field alias (whether one is
set or not).
2022-08-08 14:43:23 +01:00
DetachHead 0b8b7eb4b6 support kw_only on dataclasses (#3674)
* support `kw_only`

* add changes file

* add test for `kw_only`

* tweak error message

Co-authored-by: detachhead <detachhead@users.noreply.github.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 13:44:16 +01:00
Amin Alaee 90a103ec3c Add Color comparison method (#3646)
* Add Color comparison method

* add changelog

* switch to public attribute

* compare rgb tuple

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 13:37:58 +01:00
Michael 177ca2a6b2 Validator decorator field check. Closes #3215 (#3216)
* Descriptive exception for incorrectly defined validator fields.

Throws a ConfigError wth helpful feedback if a validator is passed incorrectly defined fields.

* Minor grammar correction.

* Expanded example.

* Added test for bad validator decorator fields.

* Added changes .md file.

* Improved wording in error message.

* Test fix.

* Update pydantic/class_validators.py

Encloses expression into a list comprehension.

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

* Update pydantic/class_validators.py

Removed some exception verbosity.

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

* Update changes/3215-SunsetOrange.md

Removed some changes verbosity.

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

* use pytest.raises(.., match=...)

Co-authored-by: Michael <michael.bedford@superloop.com>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 13:34:45 +01:00
Mateo Aguirre Duque 7431683e88 Alias support in decorator validate_arguments (#3019)
* add support for alias handling in validate_arguments

* add test for alias handling in validate_arguments

* add documentation on alias support for decorator

* bug fixed in the validate_argumen decorator documentation

* add changes README

* change in the documentation due to a drafting error

* Update changes/3019-MAD-py.md

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

* more challenging tests

* run the format checker

* integration of empty string as alias

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 13:33:51 +01:00