Commit Graph

1353 Commits

Author SHA1 Message Date
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
Samuel Colvin 4fb872ef3a Remove Label & Assign
Using https://github.com/pydantic/hooky instead
2022-08-10 18:50:18 +01: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
Hasan Ramezani 29020d2317 Test Python 3.11.0-rc.1 (#4357) 2022-08-10 10:23:14 +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
Hasan Ramezani 697459a73d Change path in validation_decorator_types example to a relative path (#4341)
* Change path in validation_decorator_types example to a relative path

* make the example a little clearer

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-09 11:12:05 +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
Anton Shestakov f41ac92b3c fix main.BaseModel.(dict, json) fields types (#4293)
* fix main.BaseModel.(dict, json) fields types

fix main.BaseModel.dict fields (include, exclude, skip_defaults) types
fix main.BaseModel.json fields (include, exclude, skip_defaults) types

* fix types for BaseModel.copy, BaseModel._iter

fix types for BaseModel.copy fields: include, exclude, update
BaseModel._iter fields: include, exclude
2022-08-09 11:20:15 +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
Hasan Ramezani a9c273235f Fix sub-model link in usage/models docs (#4234) 2022-08-09 10:51:52 +01:00
Nikita Sobolev 10508f7259 Adds assert_never helper (#3294)
* Adds `assert_never` helper

* Update utils.py

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-09 10:15:04 +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
Hasan Ramezani 42462abf9d Fix #4041 -- Fix create_model docstring for custom-specified Fields (#4142) 2022-08-08 16:38:32 +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
gieddy660 9599a3d256 updates to description of the fields option. (#3822)
* updates to description of the fields option.

Update of the fields option for config to reflect the fact that it is not actually equivalent to the Field class, as I was having troubles understanting ehy I couldn't set default or default_factory.

* corrected typo

* correction

* fix typo

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-08-08 14:53:43 +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
Yurii Karabas e92f12efac Add ability to use TypedDict Required/NotRequired (#3687)
* Add ability to use TypedDict Required/NotRequired

* Update pydantic/typing.py

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

* Seperate TypedDict tests

* Update update_typeddict_keys doc string

* Add inheritance test

* Update pydantic/typing.py

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

* Raise exc instead of reimplementing new TypedDict

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2022-08-08 13:57:36 +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
dependabot[bot] 2049acec74 build(deps): bump coverage from 6.4.2 to 6.4.3 (#4346)
Bumps [coverage](https://github.com/nedbat/coveragepy) from 6.4.2 to 6.4.3.
- [Release notes](https://github.com/nedbat/coveragepy/releases)
- [Changelog](https://github.com/nedbat/coveragepy/blob/master/CHANGES.rst)
- [Commits](https://github.com/nedbat/coveragepy/compare/6.4.2...6.4.3)

---
updated-dependencies:
- dependency-name: coverage
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-08 12:05:49 +00:00
dependabot[bot] b67c77f71a build(deps): bump flake8 from 5.0.3 to 5.0.4 (#4347)
Bumps [flake8](https://github.com/pycqa/flake8) from 5.0.3 to 5.0.4.
- [Release notes](https://github.com/pycqa/flake8/releases)
- [Commits](https://github.com/pycqa/flake8/compare/5.0.3...5.0.4)

---
updated-dependencies:
- dependency-name: flake8
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-08 12:05:27 +00:00
dependabot[bot] ae3aae9763 build(deps): bump hypothesis from 6.53.0 to 6.54.1 (#4345)
Bumps [hypothesis](https://github.com/HypothesisWorks/hypothesis) from 6.53.0 to 6.54.1.
- [Release notes](https://github.com/HypothesisWorks/hypothesis/releases)
- [Commits](https://github.com/HypothesisWorks/hypothesis/compare/hypothesis-python-6.53.0...hypothesis-python-6.54.1)

---
updated-dependencies:
- dependency-name: hypothesis
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-08 13:02:46 +02:00
dependabot[bot] 73fe393127 build(deps): bump pycodestyle from 2.9.0 to 2.9.1 (#4344)
Bumps [pycodestyle](https://github.com/PyCQA/pycodestyle) from 2.9.0 to 2.9.1.
- [Release notes](https://github.com/PyCQA/pycodestyle/releases)
- [Changelog](https://github.com/PyCQA/pycodestyle/blob/main/CHANGES.txt)
- [Commits](https://github.com/PyCQA/pycodestyle/compare/2.9.0...2.9.1)

---
updated-dependencies:
- dependency-name: pycodestyle
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-08-08 13:02:30 +02: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
Samuel Colvin c73bf444db improved issue templates (#4328)
* improved issue template and more

* tweak wording

* more tweaks

* more tweaks

* add Affected Components

* Update .github/ISSUE_TEMPLATE/bug.yml

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

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
2022-08-05 11:22:43 +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
Samuel Colvin ea739fcb22 test another version of mypy (#4326) 2022-08-04 15:50:44 +00:00