Commit Graph

440 Commits

Author SHA1 Message Date
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
Yasser Tahiri e07d73b373 Update URLs after migrating to organization (#4348)
* update URLs

* add a change file
2022-08-08 12:27:35 +00:00
Luca Repetti 460f858cc5 Add mypy static checking for default and default_factory (#3430)
* Add static checking for default and default_factory

* Describe PR change

* Remove unnecessary booleans default_specified and default_factory_specified

* Add test with default without Field

* Apply suggestions from code review

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

* Fix tests

* Add handling of Overload types for mypy0.971

* Avoid to use walrus operator

* Support mypy 0.910 for overloaded types

* Increase coverage for mypy plugin

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 13:07:23 +01:00
Yurii Karabas a69136d209 Add ability to use Final in a field type annotation (#2768)
* Add ability to use Final in a field type annotation

* Change exc message

* Handle case when field declarated as Field call

* Final field no longer can be class vars

* Remove required assigment for Final field

* Update impl to match description

* Update due to comments
2022-08-05 15:34:53 +01:00
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
Adrian Garcia Badaracco a26665a5ad bug: Add annotation to ModelField for use in BaseModel.__signature__ (#3413)
* Add annotation to ModelField for use in BaseModel.__signature__

* add changelog

* replace " with '

* Update changes/3413-fix-inspect-signature.md

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

* fix

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-05 15:13:47 +01:00
Piotr Rogulski 5885e6ce12 fix(PostgresDsn): Add support for multiple hosts in PostgresDsn #3337 (#3349)
* Move host regex out of url_regex and inject it afterwards

* Add new host regex with cache variable

* Move url regex strings to separate variables

* Add new postgres url regex with cache variable

* Add tests for failed postgres dns with many hosts

* Add new parts type: HostParts

* Add new slot 'hosts' to PostgreDsn and update init with it

* Add validators to PostgreDsn class.

When multiple hosts are found, all hosts will be store in hosts list. To
keep back compatibility single host will return all data as previously
in main params.

* Add more detail tests to check multi host postgres dsn with attributes

* Add changes description in the file

* Delete usless comments to regex strings because of varable names

* Add missing docstring to postgres_url_regex function

* Update postgre dsn with AnyUrl.slots

* Update AnyUrl.validate_parts to support disabling port validation

* Reuse _host_regex in postgres_url_regex

* Fix typing

* Delete not needed group in regex

* Improve tests by adding parametrize to postgreDsn tests

* Update postgres regex to not validate hosts in it

* Delete duplicated code and use shared validate method

* Move multi host methods into separate class and inherit it in the postgreDsn

* fix tests

* tweaks and re order code

* add a note to docs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-05 13:57:16 +00:00
Samuel Colvin 39d30c24df fix parsing of tiny timedelta values (#4329) 2022-08-05 11:31:13 +00:00
Jonas Obrist 88ade4bb6d fix int_validator not catching overflows (#3112)
* fix int_validator not catching overflows

* Update changes/3112-ojii.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-08-05 11:22:55 +00:00
Jean 5293adb3d3 fix(secret): uppercase in filename on linux (#3304)
* fix(secret): uppercase in filename

* test(uppercase): fix test

* refactor(secret): apply feedbacks

* refactor(path): apply feedbacks

* fix(test): fix path in test

* cleanup and correct tests

Co-authored-by: Jean Arhancetebehere <jean.arhancetebehere@ubisoft.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-04 16:07:41 +00:00
Salar Nosrati-Ershad d53259aa58 Add MongoDB network data source name (DSN) schema (#3230)
* Add MongoDsn to pydantic.networks with allowed_schemas and get_default_parts

* Add unit test to MongoDsn and remove default host from default parts becouse it's required by MongoDB protocol

* Fix import issues, follow contributing guide

* Add changes to docs

* Add changes to changelog
2022-08-04 16:10:42 +01:00
Ezequiel Lara Gomez 2ac10affe5 Allow ellipsis on Fields inside Annotated (#3133)
* Allow ellipsis on Fields within annotated types

The usecase is TypedDicts, which can't have default values otherwise.
Meaning that adding Optionals to Annotated drops them from the list of
required fields, which the patch fixes.

* Add test for TypedDict required optionals

Clarifies what's required with TypedDicts schemas.

* Add description of changes

* PR feedback

* extra tests as requested

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-04 14:21:05 +00:00
Will McGugan e58dad6dec Added support for Rich Repr protocol (#3099)
* Added support for Rich Repr protocol

* restore line

* Words

* quotes

* Update changes/3099-willmcgugan.md

Adds code to markdown

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

* added usage with Rich

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-04 13:47:42 +00:00
Nuno André bd0a28d8d7 Avoid __dict__ and __weakref__ attributes in AnyUrl and _BaseAddress subclasses (#2890)
* Avoid __dict__ and __weakref__ attributes in AnyUrl and _BaseAddress subclasses

When inheriting from a class with `__slots__`, [child subclasses will get a `__dict__` and `__weakref__` unless they also define `__slots__`](https://docs.python.org/3/reference/datamodel.html#notes-on-using-slots).

```python
from pydantic import AnyUrl, AnyHttpUrl

class AnyOtherHttpUrl(AnyUrl):
    allowed_schemes = {'http', 'https'}
    __slots__ = ()

print(set(dir(AnyHttpUrl)) - set(dir(AnyUrl)))
#> {'__weakref__', '__dict__'}

print(set(dir(AnyOtherHttpUrl)) - set(dir(AnyUrl)))
#> set()
```

* Update changes/2890-nuno-andre.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-04 11:56:37 +00:00
Eric Jolibois 576e4a3a8d refactor: change pydantic dataclass decorator (#2557)
* refactor: rewrite the whole pydantic dataclass logic

* test: add tests for issue 2162

* test: add tests for issue 2383

* test: add tests for issue 2398

* test: add tests for issue 2424

* test: add tests for issue 2541

* test: add tests for issue 2555

* refactor: polish

* change default and support 3.6

* fix coverage

* fix mypy and text

* typos

* test: add tests for issue 2594

* fix: forward doc for schema description

* add change

* chore: small changes from review

* refactor: avoid extra __pydantic_run_validation__ parameter

* small tweaks

* remove wrapper

* support 3.6

* fix: mypy

* rewrite doc

* add docs

* wrapper is removed now

* a bit more docs

* code review

* faster dict update

* add test for issue 3162

* add test for issue 3011

* feat: add `Config.post_init_after_validation`

* allow config via dict

* fix cython and TypedDict

* chore: typo

* move `compiled` in `version.py`

* refactor: switch from `Config.post_init_after_validation` to \'post_init_call`

* add dataclass isinstance support

* avoid multi paragraphs in change file

* feat: support `Config.extra`

* refactor: simplify a bit code

* refactor: avoid creating useless functions

* refactor: simplify `is_builtin_dataclass`

* support extra in post_init

* docs: add warning on config extra

* fix #3713 compatibility

* update docs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-04 12:06:54 +01:00
Sergey Tsaplin 9cfbd2b11b Empty string is a valid JSON-key (#4252)
* Empty string is a valid JSON-key

Signed-off-by: Sergey Tsaplin <me@sergeytsaplin.com>

* Update changes/4253-sergeytsaplin.md

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

* has_alias attribute of the ModelField also should be a result of comparison with None

Signed-off-by: Sergey Tsaplin <me@sergeytsaplin.com>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-08-03 16:27:49 +01: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
Giuliano Oliveira 5a61292252 Adds dataclass_transform to dataclass (#4007)
*  Adds dataclass_transform to dataclasses

*  Adds dataclass_transform to dataclasses

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-05-18 12:19:25 +01:00
Samuel Colvin abea8232ee speedup __instancecheck__ check on BaseModel when they fail (#4081)
* speedup __instancecheck__ check on BaseModel when they fail

* add change description

* linting
2022-05-17 14:13:36 +01:00
David Brochart a7e896c5a3 Update Jupyter's use of pydantic (#4082)
* Update Jupyter's use of pydantic

* Fix markdown link
2022-05-16 11:26:31 +01:00
Maxim Martynov cc54acb612 Fix missing port in HttpUrl.build() result (#3652)
* Port number is no longer being ignored by HttpUrl.build()

* Update tests/test_networks.py

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

* Update networks.py

* Update tests/test_networks.py

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

* Update test_networks.py

* Update test_networks.py

* update change description

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-05-14 17:35:39 +01:00
Samuel Colvin 122dd2f24e fix ClassVars, better fix for #3679 (#4077) 2022-05-14 17:27:52 +01:00
Alex f69012a5aa fix: error checking inheritance when using PEP585 and PEP604 type hints (#3681)
* Add tests

* Fix the issue

* Add changes file

* Improved convert_generics

* Add default fallback to convert_generics
Improved Annotated and Literal handling

* Fix Cython doesn't support generic types (PEP560)
Watch cython issue cython/cython#2753
Previous implementation can be used after cython 3.0 release

* Add custom type test

* Cosmetic fixes

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

* Fix typos

* Add SelfReferencing test validation
Add parametrization to

* Fix: parametrization caused test discovery problem

* Better explanation for a test case

* Better assertions for model creation tests

* Rerun CI

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-05-14 17:26:12 +01: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
Adrian Garcia Badaracco e5540296cc Fix issue with in-place modification of FieldInfo (#4067)
* Fix info with in-place modification of field info

* add changes

* add test for 3714

* Update changes/4067-adriangb.md

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

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-05-13 15:16:32 +01:00
Samuel Colvin a45276d6b1 guard against ClassVar in fields (#4064)
* guard against ClassVar in fields, fix #3679

* fix linting

* skipif for test_class_var_forward_ref
2022-05-13 15:06:53 +01:00
Yurii Karabas 42acd8f8d2 Fix issue with self-referencing dataclass (#3713)
* Fix issue with self-referencing dataclass

* Fix mypy issue
2022-05-11 19:09:13 +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
Samuel Colvin 74403c2f15 test pyright with pydantic (#3972)
* test pyright with pydantic

* rename file to avoid pytest running it

* try another name 😴

* add docs about BaseSettings and Field

* add change
2022-05-11 19:00:37 +01:00
Eric Jolibois 5490ad5173 fix: Config.copy_on_model_validation does a deep copy and not a shallow one (#3642)
* fix: `Config.copy_on_model_validation` does a deep copy and not a shallow one

closes #3641

* fix: typo

* use python 3.10 to run fastapi tests

* fix fastapi test call

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-05-11 17:03:48 +01:00
Samuel Colvin 8997cc5961 Remove benchmarks completely (#3973)
* removing benchmarks completely

* [no ci] add change
2022-04-04 13:29:17 +01:00
Samuel Colvin b25e22f7d4 [no ci] correct name of change file 2022-04-04 13:22:07 +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
Samuel Colvin f0acf6efe7 Prevent subclasses of bytes being converted to bytes (#3707)
* adding a test

* fix and add change description
2022-04-02 14:45:31 +01:00
Tom Milligan 02eb182db0 fix: clarify that discriminated unions do not support singletons (#3639) 2022-04-02 14:06:11 +01:00
Wong Hoi Sing Edison 6e0f81c73f CentOS 7: read_text(encoding='utf-8') (#3625)
With CentOS 7 Python 3.6, running install from source with pip failed:

    sudo podman run -ti --rm centos:7
    yum -y update
    yum -y install epel-release
    yum -y install git python3 python3-devel python3-pip python3-setuptools python3-wheel
    git clone https://github.com/samuelcolvin/pydantic.git
    cd pydantic
    pip3 install .

With following error message:

    [root@c99d0585636c pydantic]# pip3 install .
    Processing /pydantic
        Complete output from command python setup.py egg_info:
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "/tmp/pip-91v_ixvz-build/setup.py", line 62, in <module>
            history = (THIS_DIR / 'HISTORY.md').read_text()
          File "/usr/lib64/python3.6/pathlib.py", line 1197, in read_text
            return f.read()
          File "/usr/lib64/python3.6/encodings/ascii.py", line 26, in decode
            return codecs.ascii_decode(input, self.errors)[0]
        UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 14648: ordinal not in range(128)

        ----------------------------------------
    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-91v_ixvz-build/

This PR add the required `read_text(encoding='utf-8')` for `setup.py`.

Signed-off-by: Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
2022-04-02 14:05:12 +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
Samuel Colvin 5406423268 prepare for v1.9.0 release 2021-12-31 14:53:14 +00:00
Samuel Colvin 87da9ac23f apply update_forward_refs to json_encoders (#3595)
* apply update_forward_refs to json_encoders, fix #3583

* linting

* mypy

* avoid use of ForwardRef with python3.6

* fix ForwardRef usage, take 2

* coverage
2021-12-31 14:44:09 +00:00
Samuel Colvin 5d6f48cb07 prepare for v1.9.0a2 2021-12-24 13:54:35 +00:00