* 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
* 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>
* 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
* feat: pass config to create_model_from_namedtuple
* feat: tests for arbitrary_types_allowed in NamedTuple fields
* misc: add changelog file for the PR
* 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>
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.
* support overwriting dunder attributes of `BaseModel` instances
closes#3777
* suggestion from @adriangb
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
* 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>
* 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>
* 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>
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).
* 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>
* 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>
* 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>
* 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
* 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>
* 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>
* 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>
* 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>