Data parsing and validation using Python type hints

Overview

pydantic

CI Coverage pypi CondaForge downloads versions license

Data validation and settings management using Python type hinting.

Fast and extensible, pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.6+; validate it with pydantic.

Help

See documentation for more details.

Installation

Install using pip install -U pydantic or conda install pydantic -c conda-forge. For more installation options to make pydantic even faster, see the Install section in the documentation.

A Simple Example

from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: Optional[datetime] = None
    friends: List[int] = []

external_data = {'id': '123', 'signup_ts': '2017-06-01 12:22', 'friends': [1, '2', b'3']}
user = User(**external_data)
print(user)
#> User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]
print(user.id)
#> 123

Contributing

For guidance on setting up a development environment and how to make a contribution to pydantic, see Contributing to Pydantic.

Reporting a Security Vulnerability

See our security policy.

Issues
  • Class attributes starting with underscore do not support assignment

    Class attributes starting with underscore do not support assignment

    Bug

    For bugs/questions:

    • OS: macOS
    • Python version import sys; print(sys.version): 3.6.6
    • Pydantic version import pydantic; print(pydantic.VERSION): 0.14

    In #184 it was suggested to use variables starting with the underscore, however this does not work. The last comment in #184 referred to the same problem, but it is technically a separate issue.

    from pydantic import BaseModel
    m = BaseModel()
    m._foo = "bar"
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/dmfigol/projects/my/public/simple-smartsheet/.venv/lib/python3.6/site-packages/pydantic/main.py", line 176, in __setattr__
        raise ValueError(f'"{self.__class__.__name__}" object has no field "{name}"')
    ValueError: "BaseModel" object has no field "_foo"
    
    feature request help wanted 
    opened by dmfigol 41
  • Auto-completion when instantiating BaseModel objects

    Auto-completion when instantiating BaseModel objects

    Feature Request

    Hi! Currently auto-completion does not work well when calling the initializer of classes that inherit from pydantic.BaseModel, at least in IntelliJ / PyCharm.

    Are there any plans to improve that? Auto-completion works nicely with dataclasses from the python 3.7 standard library. Same applies to highlighting type mismatches or missing arguments in the call. Here is a comparison of the behavior in IntelliJ:

    image

    image

    Code example to reproduce the screenshots:

    from pydantic import BaseModel
    from dataclasses import dataclass
    
    
    class User(BaseModel):
        id: int
        name: str
        age: float
    
    
    @dataclass
    class UserDataclass:
        id: int
        name: str
        age: float
    
    
    # no auto-completion
    user = User()
    
    # auto-completion
    user_dataclass = UserDataclass()
    

    Thanks for creating pydantic! Best regards, Marlon

    feature request Feedback Wanted 
    opened by marlonjan 38
  • StrictBool

    StrictBool

    Thank you for bringing types into python -- it makes daily life so much easier :)

    However, I think there's a pretty serious bug with the bool validator.

    Minimal working example:

    from pydantic import BaseModel
    
    class Model(BaseModel):
        a: bool
    
    Model(a=1) # should cause exception
    Model(a="snap") # should cause exception
    

    a should be a bool and only a bool. If a user provides any other value to the endpoint (using fastapi), the database throws because it's the wrong type.

    I cannot think of a single type that can be passed to bool(X) that won't return a boolean, and yet bool_validator returns that coercion. e.g. bool({"moo": 1}) == True. This makes the validator completely useless :(

    Is there a reason behind this? And if it needs to stay as is, do you think adding a strict mode to the validator is an option?

    Also, as an aside, there are no unit tests for the function.

    I'm very happy to make a PR (adding some unit tests as well!) because it shouldn't take long, but I need to understand if there's a reason for this first.

    Thanks!

    feature request help wanted Feedback Wanted 
    opened by cazgp 36
  • Refactored extra types to use a single enum

    Refactored extra types to use a single enum

    Change Summary

    Use a single value to describe allowing extra attributes or ignoring them, by using an enum:

    class ExtraAttributes(Enum):
        DISALLOW_MUTATION = auto()
        ALWAYS_DISALLOW = auto()
        MUTATION_ONLY = auto()
        ALWAYS_ALLOW = auto()
    
        def allow_mutation(self):
            return self in (self.MUTATION_ONLY, self.ALWAYS_ALLOW)
    
        def should_ignore(self):
            return self in (self.DISALLOW_MUTATION, self.MUTATION_ONLY)
    

    Related issue number

    #351

    Checklist

    • [x] Unit tests for the changes exist
    • [ ] Tests pass on CI and coverage remains at 100%
    • [ ] Documentation reflects the changes where applicable
    • [ ] HISTORY.rst has been updated
      • if this is the first change since a release, please add a new section
      • include the issue number or this pull request number #<number>
      • include your github username @<whomever>
    opened by liiight 35
  • Question: add private attribute

    Question: add private attribute

    I have a use case that I'd to add an attribute when initialising the instance which is not part of the model, thus should not be validated. Is that possible?

    Here's a practical example:

    from pydantic import BaseModel
    from datetime import datetime
    
    class Test(BaseModel):
        a: int
    
    class TestExtra(BaseModel):
        a: int
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self._processed_at = datetime.utcnow()
    
    test = {"a": 1}
    Test(**test)
    TestExtra(**test)  # ValueError: "TestExtra" object has no field "_processed_at"
    
    opened by rrbarbosa 35
  • mypy: invalid type comment or annotation

    mypy: invalid type comment or annotation

    Hi,

    I think this issue related more to mypy, but maybe you know about any workaround or how to solve this.

    Here is my code:

    class Test(BaseModel):
        k: constr(min_length=2)
    

    And when I run mypy I got:

    error: invalid type comment or annotation
    note: Suggestion: use constr[...] instead of constr(...)
    

    Any ideas?

    opened by Gr1N 32
  • IMPORTANT: PEP 563, PEP 649 and the future of pydantic

    IMPORTANT: PEP 563, PEP 649 and the future of pydantic

    Update: see below this has been resolved by a changes in python 3.10 from the python steering council.

    Thanks everyone for your thoughts, patience and hard work.


    PEP 563 "Postponed Evaluation of Annotations" was introduced in python 3.7 behind the from __future__ import annotations switch, it basically meant all annotations are strings, not python objects.

    Pydantic has tried to supported Postponed Annotations since v0.18.0, see #348.

    The problem however is that trying to evaluate those strings to get the real annotation objects is really hard, perhaps impossible to always do correctly, see #248 #234 #397 #415 #486 #531 #545 #635 #655 #704 #1298 #1332 #1345 #1370 #1668 #1736 #1873 #1895 #2112 #2314 #2323 #2411

    The reasons are complicated but basically typing.get_type_hints() doesn't work all the time and neither do the numerous hacks we've introduced to try and get fix it. Even if typing.get_type_hints() was faultless, it would still be massively slower than the current semantics or PEP 649 (see below).

    In short - pydantic doesn't work very well with postponed annotations, perhaps it never will.

    The Problem

    The problem is that postponed annotations are set to be come default in 3.10, features of which will be frozen in about three week.

    Even worse, there's no way to switch back to the current behaviour.

    The Solution

    The solution to this is PEP 649 developed by Larry Hastings. This basically means annotations evaluation is lazy - the work of building __annotations__ isn't done until you access __annotations__.

    As far as I can tell this is the best of both worlds.

    The Second Problem

    The sad reality however is that it seems very possible that PEP 649 will get rejected and when python 3.10 is released it will break much of pydantic, and thereby FastAPI and all the other libraries that rely on pydantic (as well as other libraries that use annotations at runtime like enforce and typer presumably).

    See this very long discussion of the issue on python-dev about whether PEP 649 is a good idea.


    The Point

    So why am I saying all this apart from whinging about something I don't agree with?

    1. This is fair warning

    That pydantic might break in a big way if python's core developers continue value principle over pragmatism.

    2. You can help

    Thousands of developers and organisations big and small rely on pydantic. Type hints might have been conceived to help readers and static type checkers, but they're now used to great effect at runtime by lots and lots of people - they make python better.

    In the end I don't think python's core developers and steering council want to make your experience of python worse. They just haven't realised how important this is. (Even Larry hadn't heard of pydantic or FastAPI until yesterday when he emailed me, I guess they don't read the python developer survey 😉)

    ~If you value pydantic or FastAPI or other libraries that use annotations at runtime, please (constructively and respectfully) let the python steering council know that you would like PEP 649 to be accepted.~ Please don't contact members of the python community, they're aware of this issue (see below) and are taking it seriously.

    I understand the decision on PEP 649 will be made over the next few days, ~so if you're going to do anything do it today~.

    Meta 
    opened by samuelcolvin 30
  • Version 1 Release

    Version 1 Release

    replaces #143

    As per the dicussion on #568 we should get pydantic to version 1 then aggressively follow semvar for breaking changes.

    However before we release version 1, I want to make any breaking changes that are obviously required now.

    So, to do before version 1:

    • [x] rename Schema to Field? #577
    • [ ] ~~remove references to validation and use parse instead #578~~
    • [ ] ~~decide about load_alias and dump_alias ~~#477~~ #624~~ not required for v1.
    • [x] fix any significant open bugs (see issue tracker / filter by "bug")
    • [x] need to decide about #508

    anything else? no point in spending too long perfecting the library and never getting to v1, but still, let's get anything obviously wrong right now.

    Meta Feedback Wanted 
    opened by samuelcolvin 30
  • added `KafkaDsn` to `networks.py` and add default ports for `HttpUrl`

    added `KafkaDsn` to `networks.py` and add default ports for `HttpUrl`

    ❤️ pydantic

    Change Summary

    • Added KafkaDsn class e.g. already existing RedisDsn :fire:
    • Added default_parts member and apply_default_parts method to the AnyUrl class

    Related issue number

    Checklist

    • [x] Unit tests for the changes exist
    • [x] Tests pass on CI and coverage remains at 100%
    • [x] Documentation reflects the changes where applicable
    • [x] changes/<pull request or issue id>-<github username>.md file added describing change (see changes/README.md for details)
    opened by MihanixA 30
  • ✨ Add autocomplete support for VS Code, via `dataclass_transform`

    ✨ Add autocomplete support for VS Code, via `dataclass_transform`

    Change Summary

    In short, this adds ✨ autocomplete ✨ for fields when creating a new instance of a Pydantic model. 🎉

    Selection_408

    Details

    Eric Traut, Pyright's author, is writing a proposal for a new standard for Python typing to help declare models like pydantic and others as dataclass-like, to support autocompletion when creating a new instance (automatic typed __init__ constructor).

    The gist is that a new decorator typing.dataclass_transform() that would be applied in pydantic's metaclass (not needed by final users) would give it those extra powers.

    The draft standard is here: https://github.com/microsoft/pyright/blob/master/specs/dataclass_transforms.md

    The discussion is here: https://github.com/microsoft/pyright/discussions/1782

    The draft includes a small trick to adopt it right away even before the standard is accepted, adding a custom minimal __dataclass_transform__ to the codebase. Pyright, and so, Pylance, and so VS Code, already support it.

    So, this simple change in this PR enables autocompletion in VS Code right away. In a similar way that it is provided in PyCharm by @koxudaxi's awesome plug-in.

    As a side note, if this standard is accepted, I think it would also replace and/or simplify a large part of the pydantic mypy plugin.

    Thanks @koxudaxi for pointing me to this in the discussions! https://github.com/samuelcolvin/pydantic/discussions/2698

    Related issue number

    I understand Samuel was already aware this was in progress and already in comm with Eric, so I'm not sure this justifies an issue. If so, let me know and I'll make one.

    Checklist

    • [ ] Unit tests for the changes exist
    • [x] Tests pass on CI and coverage remains at 100%
    • [x] Documentation reflects the changes where applicable
    • [x] changes/<pull request or issue id>-<github username>.md file added describing change (see changes/README.md for details)
    ready for review 
    opened by tiangolo 28
  • Enhancing editor completion for BaseModel.__init__

    Enhancing editor completion for BaseModel.__init__

    Discussed in https://github.com/samuelcolvin/pydantic/discussions/3263

    Originally posted by elda27 September 26, 2021 Currently, BaseModel.__init__ takes **data as an argument. I would like to set __annotations__ for editor complementions. I seem that BaseModel.__init__.__annotations__ can be set in ModelMetaclass.

    Is this change acceptable? If it acceptable, I would like to contribute.

    I posted three months ago. Arguments of __init__ doesn't work code completion on the VSCode now.

    Current behavior type_hint-before

    Expected proposing behavior type_hint-after

    I think __signature__ may work well since Python 3.9. But practical annotation for __init__ should be __annotaions__ before Python 3.8.

    I have already implemented this solution three months ago. So I can contribute the solution. Please consider my suggestion.

    Following code is an example of changes at ModelMetaclass for code completion.

    if not hasattr(cls, '__init__'): # __init__ is defined at subclass
        cls.__init__.__annotations__ = {
            k: field.type_
            for k, field in fields.items()
        }
        cls.__init__.__kwdefaults__ = {
            k: field.default for k, field in fields.items()
        }
        cls.__init__.__kwdefaults__.update({
            field.alias: field.default
            for field in fields.values()
            if field.has_alias
        })
    # set __signature__ attr only for model class, but not for its instances
    cls.__signature__ = ClassAttribute('__signature__', 
    generate_model_signature(cls.__init__, fields, config))
    
    opened by elda27 1
  • new `to_lower_camel()` function

    new `to_lower_camel()` function

    I needed non pascal case camel case support

    Change Summary

    created new function (to_lower_camel() for non pascal case camel case), unit tests, and updated documentation:

    >>> to_camel("i_like_turtles")
    ILikeTurtles
    
    >>> to_lower_camel("i_like_turtles")
    iLikeTurtles
    
    

    Related issue number

    #3464

    Checklist

    • [ y ] Unit tests for the changes exist
    • [ y ] Tests pass on CI and coverage remains at previous level
    • [ y ] Documentation reflects the changes where applicable
    • [ y ] changes/<pull request or issue id>-<github username>.md file added describing change (see changes/README.md for details)
    • [ y ] My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers
    ready for review 
    opened by schlerp 2
  • Construct does not recursively construct BaseModel?

    Construct does not recursively construct BaseModel?

    Checks

    • [x] I added a descriptive title to this issue
    • [x] I have searched (google, github) for similar issues and couldn't find anything
    • [x] I have read and followed the docs and still think this is a bug

    Bug

    I have built a "nested" Basemodel (e.g, the fields of a basemodel are basemodels themselves), where I have validation that warns in certain cases, and validation that sets some attributes on other cases. I would like to be able to pass this validated model to a very similar BaseModel, without warnings and setting occurring again.

    I looked at BaseModel.construct, which is advertised as exactly what I want, but it looks like none of its fields are set to be baseclasses themselves. Is this intentional behaviour? This does not occur if I use BaseModel.parse_obj.

    If this is intentional, is there any way I instantiate a nested model without validation?

    To be extra clear with the example below: Given a dict data, I would like to create object a without validation occuring.

    Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

    ...             pydantic version: 1.8.2
                pydantic compiled: True
                     install path: /usr/local/lib/python3.6/site-packages/pydantic
                   python version: 3.6.15 (default, Oct 12 2021, 03:54:14)  [GCC 8.3.0]
                         platform: Linux-5.4.120+-x86_64-with-debian-10.11
         optional deps. installed: ['dotenv', 'typing-extensions']
    
    from pydantic import BaseModel
    
    class Bar(BaseModel):
        y: int
        z: int
    
    class Foo(BaseModel):
        x: Bar
    
    data = {'x':{'y': 1, 'z': 2}}
    
    a = Foo(**data)
    print(isinstance(a, BaseModel)) # True
    print(isinstance(a.x, BaseModel)) # True
    
    b = Foo.construct(**data) #               <----- Difference is here!
    print(isinstance(b, BaseModel)) # True
    print(isinstance(b.x, BaseModel)) # False
    
    a.x.y # Works
    b.x.y # Error: 'dict' object has no attribute 'y'
    
    bug 
    opened by thomasaarholt 4
  • docs: Print name classes for `__concrete_name__` example

    docs: Print name classes for `__concrete_name__` example

    Change Summary

    Related issue number

    Checklist

    • [x] Unit tests for the changes exist
    • [ ] Tests pass on CI and coverage remains at 100%
    • [x] Documentation reflects the changes where applicable
    • [ ] changes/<pull request or issue id>-<github username>.md file added describing change (see changes/README.md for details)
    • [ ] My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers
    ready for review 
    opened by q0w 1
  • `BaseModel.schema()` raises exception when it contains a frozen dataclass

    `BaseModel.schema()` raises exception when it contains a frozen dataclass

    Checks

    • [X] I added a descriptive title to this issue
    • [X] I have searched (google, github) for similar issues and couldn't find anything
    • [X] I have read and followed the docs and still think this is a bug

    Bug

    Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

    pydantic version: 1.8.2
                pydantic compiled: True
                     install path: /Users/{username}/Library/Caches/pypoetry/virtualenvs/{virtualenv_name}-py3.8/lib/python3.8/site-packages/pydantic
                   python version: 3.8.12 (default, Nov  4 2021, 13:59:45)  [Clang 13.0.0 (clang-1300.0.29.3)]
                         platform: macOS-11.6-x86_64-i386-64bit
         optional deps. installed: ['typing-extensions']
    
    import pydantic
    import dataclasses
    
    @dataclasses.dataclass(frozen=True)
    class ParentClass:
        child: ChildClass
    
    @dataclasses.dataclass(frozen=True)
    class ChildClass:
        name: str
    
    class WrapperModel(pydantic.BaseModel):
        parent: ParentClass
    
    # Test 1: create schema from the wrapper class
    print(WrapperModel.schema()) # raises "TypeError: cannot inherit non-frozen dataclass from a frozen one"
    
    # Test 2: convert child class to Pydantic and create schema
    PydanticChild = pydantic.dataclasses.dataclass(ChildClass) # raises "TypeError: cannot inherit non-frozen dataclass from a frozen one"
    
    # Test 3: add "frozen" when converting
    PydanticChild = pydantic.dataclasses.dataclass(ChildClass, frozen=True) # works okay
    print(PydanticChild.__pydantic_model__.schema()) # works okay
    
    # Test 4: add "frozen" on parent conversion
    PydanticParent = pydantic.dataclasses.dataclass(ParentClass, frozen=True) # works okay
    print(PydanticParent.__pydantic_model__.schema()) # raises "TypeError: cannot inherit non-frozen dataclass from a frozen one"
    
    # Test 5: simply use the WrapperModel class
    model = WrapperModel(parent=ParentClass(child=ChildClass(name='hello'))) # works okay
    
    

    The error message is quite confusing here, since no inheritance is explicitly performed by the user. I think this issue is due to some instruction inside BaseModel.schema() trying to convert the native dataclass into a Pydantic dataclass without taking into account the frozen argument.

    bug 
    opened by rsayn 0
  • BaseSettings with aliased fields conflicts at instantiation with field argument if an environment variable with alias name exists

    BaseSettings with aliased fields conflicts at instantiation with field argument if an environment variable with alias name exists

    Hello,

    first of all, thanks for the awesome work you make with pydantic, this is fantastic library. If needed, I could contribute to push a fix for this bug, but I'll need some hints to find out what to modify since I am not familiar with the codebase.

    Checks

    • [X] I added a descriptive title to this issue
    • [X] I have searched (google, github) for similar issues and couldn't find anything
    • [X] I have read and followed the docs and still think this is a bug

    Bug

    Environment

    Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

    pydantic version: 1.8.2
    pydantic compiled: True
       install path: C:\ProgramData\anaconda3\envs\kedro_sas\Lib\site-packages\pydantic
     python version: 3.7.10 (default, Feb 26 2021, 13:06:18) [MSC v.1916 64 bit (AMD64)]
           platform: Windows-10-10.0.18362-SP0
    optional deps. installed: ['typing-extensions']
    

    Description and setup

    I have created a class which from BaseSettings with some aliased fields which refers to environment variables the settigns can be taken from. A minimal reproducible example is:

    from pydantic import BaseSettings, Field
    import os
    
    class User(BaseSettings):
        username: str = Field(..., env="OS_USERNAME", alias="OS_USERNAME")
        password: str = Field(..., env="OS_PASSWORD", alias="OS_PASSWORD")
    
        class Config:
            extra = "forbid"  # extra keys are forbidden in the constructor
            allow_population_by_field_name = True  # you can use both "username" and "OS_USERNAME" (the alias) to populate the model
    
    

    Bug example

    With previous setup and as far as I understand pydantic, I should be able to create a User instance with all of the following ways:

    • CASE 1 (OK): no variable in env, populate with field
    User(username="John", password="secret_password") 
    # works and returns: User(username="John", password="secret_password")
    
    • CASE 2 (OK) : password in env, populate with field
    os.environ["OS_PASSWORD"]="secret_password_from_env"
    User(username="John") 
    # works and returns: User(username='John', password='secret_password_from_env')
    
    • CASE 3 (OK) : password in env, populate with alias
    os.environ["OS_PASSWORD"]="secret_password_from_env"
    User(username="John", OS_PASSWORD="secret_password") 
    # works, provided argument has precedence over environment: User(username='John', password='secret_password')
    
    • CASE 4 (KO) : password in env, populate with field
    os.environ["OS_PASSWORD"]="secret_password_from_env"
    User(username="John", password="secret_password") # we use password (field) instead of OS_PASSWORD (alias) like in environment variable
    # FAILS with: 
    # ValidationError: 1 validation error for User
    # password
    #   extra fields not permitted (type=value_error.extra)
    
    # EXPECTED result identical to CASE 3:
    # User(username='John', password='secret_password')
    

    This looks like a bug, intutively it should behave like CASE 3, e.g. override the environment variable with the provided field. Even worse, this is the exact same code like CASE 1: CASE 1 works if no environment variable is provided, and failed if an environment variable exists which leads to very confusing and hard to debug behaviour.

    bug 
    opened by Galileo-Galilei 0
  • Using a subclass of an stdlib dataclass as field fails validation

    Using a subclass of an stdlib dataclass as field fails validation

    Checks

    • [x] I added a descriptive title to this issue
    • [x] I have searched (google, github) for similar issues and couldn't find anything
    • [x] I have read and followed the docs and still think this is a bug

    Bug

    Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

    # python -c "import pydantic.utils; print(pydantic.utils.version_info())"
                 pydantic version: 1.8.2
                pydantic compiled: True
                     install path: /usr/local/lib/python3.9/site-packages/pydantic
                   python version: 3.9.9 (main, Nov 17 2021, 16:20:40)  [GCC 10.2.1 20210110]
                         platform: Linux-5.10.47-linuxkit-x86_64-with-glibc2.31
         optional deps. installed: ['typing-extensions']
    
    from dataclasses import dataclass
    from typing import TypeVar
    
    from pydantic import BaseModel
    
    
    @dataclass(frozen=True)
    class Base:
        __annotations__ = {}
    
        def do_something():
            pass
    
    
    BaseBounded = TypeVar("BaseBounded", bound=Base)
    
    
    @dataclass(frozen=True)
    class BaseWithParam(Base):
        a: str
    
    
    class PydanticModel(BaseModel):
        u: BaseBounded
    
    
    u = BaseWithParam(a="1")
    assert isinstance(u, Base)
    PydanticModel(u=u)
    
    # ---------------------------------------------------------------------------
    # ValidationError                           Traceback (most recent call last)
    # <ipython-input-1-c2b627b7a10a> in <module>
    #      27 u = BaseWithParam(a="1")
    #     28 assert isinstance(u, Base)
    # ---> 29 PydanticModel(u=u)
    # 
    # /usr/local/lib/python3.9/site-packages/pydantic/main.cpython-39-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()
    # 
    # ValidationError: 1 validation error for PydanticModel
    # u
    #  instance of Base, tuple or dict expected (type=type_error.dataclass; class_name=Base)
    
    

    some additional info/context:

    I think in previous versions (before dataclass support was added) this code would work just fine if the pydantic model had this config:

    class Config:
       arbitrary_types_allowed = True
    

    Then, the validation would only check isinstance(u, Base) and the validation would be successful. But now, pydantic will try to validate the dataclass and to do that it first converts it to a dict using the recursive method dataclasses.asdict which causes all information about specific subclasses to be lost

    possible solution

    I can make the code run without errors if I modify this function _validate_dataclass to add an extra elif and use a non recursive version of dataclasses.asdict found here python docs for dataclasses

    def _validate_dataclass(cls: Type['DataclassT'], v: Any, config=None) -> 'DataclassT':
      if isinstance(v, cls):
          return v
      elif isinstance(v, (list, tuple)):
          return cls(*v)
      elif isinstance(v, dict):
          return cls(**v)
      # In nested dataclasses, v can be of type `dataclasses.dataclass`.
      # But to validate fields `cls` will be in fact a `pydantic.dataclasses.dataclass`,
      # which inherits directly from the class of `v`.
      elif is_builtin_dataclass(v) and cls.__bases__[0] is type(v):
          import dataclasses
          non_recursive_dict = dict((field.name, getattr(v, field_name)) for field in dataclasses.fields(v))
          return cls(**non_recursive_dict)
      elif is_builtin_dataclass(v) and isinstance(v, cls.__abses__[0]):
          import dataclasses
          non_recursive_dict = dict((field.name, getattr(v, field_name)) for field in dataclasses.fields(v))
    
          # note this is a hack, I hardcoded frozen=True to make it work
          target = dataclass(type(v), config=config, frozen=True)
          return target(**non_recursive_dict)
      else:
          raise DataclassTypeError(class_name=cls.__name__)
    

    to make sure config makes it to the function arguments, I also modified this line dataclasses to be:

        from functools import partial
        cls.__validate__ = classmethod(partial(_validate_dataclass, config=config))  # type: ignore[assignment]
    

    This was a hack just to make the code run without validation errors, but if it makes sense I would be happy to try to make it a formal PR

    bug 
    opened by godinenbicicleta 0
  • pyright 1.1.191 raising `reportUnknownVariableType` for reusable validators

    pyright 1.1.191 raising `reportUnknownVariableType` for reusable validators

    Checks

    • [x] I added a descriptive title to this issue
    • [x] I have searched (google, github) for similar issues and couldn't find anything
    • [x] I have read and followed the docs and still think this is a bug

    Bug

    Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

                 pydantic version: 1.8.2
                pydantic compiled: True
                     install path: /Users/kyle/.pyenv/versions/3.9.7/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pydantic
                   python version: 3.9.7 (default, Sep 22 2021, 08:26:25)  [Clang 13.0.0 (clang-1300.0.29.3)]
                         platform: macOS-11.6-x86_64-i386-64bit
         optional deps. installed: ['typing-extensions']
    
    from typing import Any
    
    import pydantic
    
    
    def reusable_validator(v: Any) -> Any:
        return v
    
    
    class MyModel(pydantic.BaseModel):
        example: str
    
        # Type of "_reusable_validator" is "classmethod[Unknown]" (reportUnknownVariableType)
        _reusable_validator = pydantic.validator("example", allow_reuse=True)(
            reusable_validator
        )
    

    This appears to be caused by the latest version of typeshed changing classmethod to a generic.

    Making the following change resolves this issue for the reported use case but there are other usages of classmethod that need to be updated to cover other cases that will result in the same error from pyright.

    _T = TypeVar("_T", bound=AnyCallable)
    
    def validator(
        *fields: str,
        pre: bool = False,
        each_item: bool = False,
        always: bool = False,
        check_fields: bool = True,
        whole: bool = None,
        allow_reuse: bool = False,
    ) -> Callable[[_T], classmethod[_T]]:
    
    bug 
    opened by ITProKyle 0
  • pydantic.utils.to_camel() is actually to_pascal()

    pydantic.utils.to_camel() is actually to_pascal()

    Checks

    • [ y ] I added a descriptive title to this issue
    • [ y ] I have searched (google, github) for similar issues and couldn't find anything
    • [ y ] I have read and followed the docs and still think this is a bug

    Bug

    Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

                 pydantic version: 1.8.1
                pydantic compiled: True
                     install path: /home/schlerp/projects/pelt-studio/venv/lib/python3.8/site-packages/pydantic
                   python version: 3.8.5 (default, Sep  4 2020, 07:30:14)  [GCC 7.3.0]
                         platform: Linux-5.15.2-zen1-1-zen-x86_64-with-glibc2.10
         optional deps. installed: ['typing-extensions']
    

    Camel case and pascal case are similar however, they differ by the capitalisation of the first letter. The current implementation of camel_case in pydantic us actually pascal case and not camel case at all. I suggest renaming this and also implementing a camel case. See below for code expressing the issue and suggested fix.

    Pascal Case (aka init case or upper camel case) All spaces/underscores removed and the start of every word is capitalised.

    Camel Case (aka lower camel case) All spaces and underscores removed and the start of every word, is capitalised, except the first word which is always lower case.

    Issue:

    from pydantic.utils import to_camel
    
    valid_pascal = "PascalCase"
    valid_camel = "camelCase"
    
    example = to_camel("i_shouldnt_be_capitalised")
    
    assert valid_pascal == to_camel("pascal_case")
    assert valid_camel != to_camel("camel_case")
    

    suggested fix, rename to_camel() -> to_pascal(), and write new to_camel() function:

    def to_pascal(string: str) -> str:
        return "".join(word.capitalize() for word in string.split("_"))
    
    
    def to_camel(string: str) -> str:
        if len(string) >= 1:
            pascal_string = to_pascal(string)
            return pascal_string[0].lower() + pascal_string[1:]
        return string.lower()
    
    

    Alternatively, if there is code which will break because it is dependent on the camel_case() function remaining pascal case, then i propose we implement a new function called to_lower_camel() which implements the first letter lower case variant:

    def to_camel(string: str) -> str:
        return "".join(word.capitalize() for word in string.split("_"))
    
    
    def to_lower_camel(string: str) -> str:
        if len(string) >= 1:
            pascal_string = to_camel(string)
            return pascal_string[0].lower() + pascal_string[1:]
        return string.lower()
    
    
    bug 
    opened by schlerp 1
  • build(deps): bump hypothesis from 6.24.0 to 6.29.0

    build(deps): bump hypothesis from 6.24.0 to 6.29.0

    Bumps hypothesis from 6.24.0 to 6.29.0.

    Release notes

    Sourced from hypothesis's releases.

    Hypothesis for Python - version 6.29.0

    This release teaches "builds()" to use "deferred()" when resolving unrecognised type hints, so that you can conveniently register strategies for recursive types with constraints on some arguments (issue #3026):

    class RecursiveClass: def init(self, value: int, next_node: typing.Optional["SomeClass"]): assert value > 0 self.value = value self.next_node = next_node

    st.register_type_strategy( RecursiveClass, st.builds(RecursiveClass, value=st.integers(min_value=1)) )

    The canonical version of these notes (with links) is on readthedocs.

    Hypothesis for Python - version 6.28.1

    This release fixes some internal calculations related to collection sizes (issue #3143).

    The canonical version of these notes (with links) is on readthedocs.

    Hypothesis for Python - version 6.28.0

    This release modifies our pytest plugin, to avoid importing Hypothesis and therefore triggering Hypothesis' entry points for test suites where Hypothesis is installed but not actually used (issue #3140).

    The canonical version of these notes (with links) is on readthedocs.

    Hypothesis for Python - version 6.27.3

    This release fixes issue #3080, where "from_type()" failed on unions containing PEP 585 builtin generic types (like "list[int]") in Python 3.9 and later.

    The canonical version of these notes (with links) is on readthedocs.

    Hypothesis for Python - version 6.27.2

    This patch makes the hypothesis codemod command somewhat faster.

    The canonical version of these notes (with links) is on readthedocs.

    Hypothesis for Python - version 6.27.1

    This patch changes the backing datastructures of "register_random()" and a few internal caches to use "weakref.WeakKeyDictionary". This reduces memory usage and may improve performance when registered "Random" instances are only used for a subset of your tests (issue #3131).

    ... (truncated)

    Commits
    • 0ccfaa7 Bump hypothesis-python version to 6.29.0 and update changelog
    • 96d0e4a Merge pull request #3164 from Zac-HD/defer-builds-inference
    • 3ffecc1 Doc light-theme vars
    • 1e543f2 Register recursive types
    • 4d7506f Bump hypothesis-python version to 6.28.1 and update changelog
    • 84afe88 Merge pull request #3160 from Zac-HD/denser-small-arrays
    • d28ffc2 Bump hypothesis-python version to 6.28.0 and update changelog
    • eca4cff Merge pull request #3159 from Zac-HD/defer-plugin-imports
    • 4c64ee5 Merge pull request #3162 from HypothesisWorks/create-pull-request/patch
    • 59521cd Fix average_size calculation
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies python 
    opened by dependabot[bot] 0
Releases(v1.8.2)
  • v1.8.2(May 11, 2021)

    See Changelog.

    Warning

    A security vulnerability, level "moderate" is fixed in v1.8.2. Please upgrade ASAP. See security advisory CVE-2021-29510.

    Changes

    • Security fix: Fix date and datetime parsing so passing either 'infinity' or float('inf') (or their negative values) does not cause an infinite loop, see security advisory CVE-2021-29510
    • fix schema generation with Enum by generating a valid name, #2575 by @PrettyWood
    • fix JSON schema generation with a Literal of an enum member, #2536 by @PrettyWood
    • Fix bug with configurations declarations that are passed as keyword arguments during class creation, #2532 by @uriyyo
    • Allow passing json_encoders in class kwargs, #2521 by @layday
    • support arbitrary types with custom __eq__, #2483 by @PrettyWood
    • support Annotated in validate_arguments and in generic models with python 3.9, #2483 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.7.4(May 11, 2021)

  • v1.6.2(May 11, 2021)

  • v1.8.1(Mar 3, 2021)

    See Changelog.

    Bug fixes for regressions and new features in v1.8

    • allow elements of Config.field to update elements of a Field, #2461 by @samuelcolvin
    • fix validation with a BaseModel field and a custom root type, #2449 by @PrettyWood
    • expose Pattern encoder to fastapi, #2444 by @PrettyWood
    • enable the Hypothesis plugin to generate a constrained float when the multiple_of argument is specified, #2442 by @tobi-lipede-oodle
    • Avoid RecursionError when using some types like Enum or Literal with generic models, #2436 by @PrettyWood
    • do not overwrite declared __hash__ in subclasses of a model, #2422 by @PrettyWood
    • fix mypy complaints on Path and UUID related custom types, #2418 by @PrettyWood
    • Support properly variable length tuples of compound types, #2416 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.8(Feb 26, 2021)

    See Changelog.

    Thank you to pydantic's sponsors: @jorgecarleitao, @BCarley, @chdsbd, @tiangolo, @matin, @linusg, @kevinalh, @koxudaxi, @timdrijvers, @mkeen, @meadsteve, @ginomempin, @primer-io, @and-semakin, @tomthorogood, @AjitZK, @westonsteimel, @Mazyod, @christippett, @CarlosDomingues, @Kludex, @r-m-n for their kind support.

    Highlights

    Changes

    • Breaking Change, remove old deprecation aliases from v1, #2415 by @samuelcolvin:
      • remove notes on migrating to v1 in docs
      • remove Schema which was replaced by Field
      • remove Config.case_insensitive which was replaced by Config.case_sensitive (default False)
      • remove Config.allow_population_by_alias which was replaced by Config.allow_population_by_field_name
      • remove model.fields which was replaced by model.__fields__
      • remove model.to_string() which was replaced by str(model)
      • remove model.__values__ which was replaced by model.__dict__
    • Breaking Change: always validate only first sublevel items with each_item. There were indeed some edge cases with some compound types where the validated items were the last sublevel ones, #1933 by @PrettyWood
    • Update docs extensions to fix local syntax highlighting, #2400 by @daviskirk
    • fix: allow utils.lenient_issubclass to handle typing.GenericAlias objects like list[str] in python >= 3.9, #2399 by @daviskirk
    • Improve field declaration for pydantic dataclass by allowing the usage of pydantic Field or 'metadata' kwarg of dataclasses.field, #2384 by @PrettyWood
    • Making typing-extensions a required dependency, #2368 by @samuelcolvin
    • Make resolve_annotations more lenient, allowing for missing modules, #2363 by @samuelcolvin
    • Allow configuring models through class kwargs, #2356 by @MrMrRobat
    • Prevent Mapping subclasses from always being coerced to dict, #2325 by @ofek
    • fix: allow None for type Optional[conset / conlist], #2320 by @PrettyWood
    • Support empty tuple type, #2318 by @PrettyWood
    • fix: python_requires metadata to require >=3.6.1, #2306 by @hukkinj1
    • Properly encode Decimal with, or without any decimal places, #2293 by @hultner
    • fix: update __fields_set__ in BaseModel.copy(update=…), #2290 by @PrettyWood
    • fix: keep order of fields with BaseModel.construct(), #2281 by @PrettyWood
    • Support generating schema for Generic fields, #2262 by @maximberg
    • Fix validate_decorator so **kwargs doesn't exclude values when the keyword has the same name as the *args or **kwargs names, #2251 by @cybojenix
    • Prevent overriding positional arguments with keyword arguments in validate_arguments, as per behaviour with native functions, #2249 by @cybojenix
    • add documentation for con* type functions, #2242 by @tayoogunbiyi
    • Support custom root type (aka __root__) when using parse_obj() with nested models, #2238 by @PrettyWood
    • Support custom root type (aka __root__) with from_orm(), #2237 by @PrettyWood
    • ensure cythonized functions are left untouched when creating models, based on #1944 by @kollmats, #2228 by @samuelcolvin
    • Resolve forward refs for stdlib dataclasses converted into pydantic ones, #2220 by @PrettyWood
    • Add support for NamedTuple and TypedDict types. Those two types are now handled and validated when used inside BaseModel or pydantic dataclass. Two utils are also added create_model_from_namedtuple and create_model_from_typeddict, #2216 by @PrettyWood
    • Do not ignore annotated fields when type is Union[Type[...], ...], #2213 by @PrettyWood
    • Raise a user-friendly TypeError when a root_validator does not return a dict (e.g. None), #2209 by @masalim2
    • Add a FrozenSet[str] type annotation to the allowed_schemes argument on the strict_url field type, #2198 by @Midnighter
    • add allow_mutation constraint to Field, #2195 by @sblack-usu
    • Allow Field with a default_factory to be used as an argument to a function decorated with validate_arguments, #2176 by @thomascobb
    • Allow non-existent secrets directory by only issuing a warning, #2175 by @davidolrik
    • fix URL regex to parse fragment without query string, #2168 by @andrewmwhite
    • fix: ensure to always return one of the values in Literal field type, #2166 by @PrettyWood
    • Support typing.Annotated hints on model fields. A Field may now be set in the type hint with Annotated[..., Field(...); all other annotations are ignored but still visible with get_type_hints(..., include_extras=True), #2147 by @JacobHayes
    • Added StrictBytes type as well as strict=False option to ConstrainedBytes, #2136 by @rlizzo
    • added Config.anystr_lower and to_lower kwarg to constr and conbytes, #2134 by @tayoogunbiyi
    • Support plain typing.Tuple type, #2132 by @PrettyWood
    • Add a bound method validate to functions decorated with validate_arguments to validate parameters without actually calling the function, #2127 by @PrettyWood
    • Add the ability to customize settings sources (add / disable / change priority order), #2107 by @kozlek
    • Fix mypy complaints about most custom pydantic types, #2098 by @PrettyWood
    • Add a Hypothesis plugin for easier property-based testing with Pydantic's custom types - usage details here, #2097 by @Zac-HD
    • add validator for None, NoneType or Literal[None], #2095 by @PrettyWood
    • Handle properly fields of type Callable with a default value, #2094 by @PrettyWood
    • Updated create_model return type annotation to return type which inherits from __base__ argument, #2071 by @uriyyo
    • Add merged json_encoders inheritance, #2064 by @art049
    • allow overwriting ClassVars in sub-models without having to re-annotate them, #2061 by @layday
    • add default encoder for Pattern type, #2045 by @PrettyWood
    • Add NonNegativeInt, NonPositiveInt, NonNegativeFloat, NonPositiveFloat, #1975 by @mdavis-xyz
    • Use % for percentage in string format of colors, #1960 by @EdwardBetts
    • Fixed issue causing KeyError to be raised when building schema from multiple BaseModel with the same names declared in separate classes, #1912 by @JSextonn
    • Add rediss (Redis over SSL) protocol to RedisDsn Allow URLs without user part (e.g., rediss://:[email protected]), #1911 by @TrDex
    • Add a new frozen boolean parameter to Config (default: False). Setting frozen=True does everything that allow_mutation=False does, and also generates a __hash__() method for the model. This makes instances of the model potentially hashable if all the attributes are hashable, #1880 by @rhuille
    • fix schema generation with multiple Enums having the same name, #1857 by @PrettyWood
    • Added support for 13/19 digits VISA credit cards in PaymentCardNumber type, #1416 by @AlexanderSov
    • fix: prevent RecursionError while using recursive GenericModels, #1370 by @xppt
    • use enum for typing.Literal in JSON schema, #1350 by @PrettyWood
    • Fix: some recursive models did not require update_forward_refs and silently behaved incorrectly, #1201 by @PrettyWood
    • Fix bug where generic models with fields where the typevar is nested in another type a: List[T] are considered to be concrete. This allows these models to be subclassed and composed as expected, #947 by @daviskirk
    • Add Config.copy_on_model_validation flag. When set to False, pydantic will keep models used as fields untouched on validation instead of reconstructing (copying) them, #265 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.7.3(Nov 30, 2020)

    See Changelog.

    Thank you to pydantic's sponsors: @timdrijvers, @BCarley, @chdsbd, @tiangolo, @matin, @linusg, @kevinalh, @jorgecarleitao, @koxudaxi, @primer-api, @mkeen, @meadsteve for their kind support.

    • fix: set right default value for required (optional) fields, #2142 by @PrettyWood
    • fix: support underscore_attrs_are_private with generic models, #2138 by @PrettyWood
    • fix: update all modified field values in root_validator when validate_assignment is on, #2116 by @PrettyWood
    • Allow pickling of pydantic.dataclasses.dataclass dynamically created from a built-in dataclasses.dataclass, #2111 by @aimestereo
    • Fix a regression where Enum fields would not propagate keyword arguments to the schema, #2109 by @bm424
    • Ignore __doc__ as private attribute when Config.underscore_attrs_are_private is set, #2090 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.7.2(Nov 1, 2020)

    See Changelog.

    Thank you to pydantic's sponsors: @timdrijvers, @BCarley, @chdsbd, @tiangolo, @matin, @linusg, @kevinalh, @jorgecarleitao, @koxudaxi, @primer-api, @mkeen for their kind support.

    • fix slow GenericModel concrete model creation, allow GenericModel concrete name reusing in module, #2078 by @MrMrRobat
    • keep the order of the fields when validate_assignment is set, #2073 by @PrettyWood
    • forward all the params of the stdlib dataclass when converted into pydantic dataclass, #2065 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.7.1(Oct 28, 2020)

    See Changelog.

    Thank you to pydantic's sponsors: @timdrijvers, @BCarley, @chdsbd, @tiangolo, @matin, @linusg, @kevinalh, @jorgecarleitao, @koxudaxi, @primer-api, @mkeen for their kind support.

    • fix annotation of validate_arguments when passing configuration as argument, #2055 by @layday
    • Fix mypy assignment error when using PrivateAttr, #2048 by @aphedges
    • fix underscore_attrs_are_private causing TypeError when overriding __init__, #2047 by @samuelcolvin
    • Fixed regression introduced in v1.7 involving exception handling in field validators when validate_assignment=True, #2044 by @johnsabath
    • fix: pydantic dataclass can inherit from stdlib dataclass and Config.arbitrary_types_allowed is supported, #2042 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.7(Oct 26, 2020)

    See Changelog.

    Thank you to pydantic's sponsors: @timdrijvers, @BCarley, @chdsbd, @tiangolo, @matin, @linusg, @kevinalh, @jorgecarleitao, @koxudaxi, @primer-api for their kind support.

    Highlights

    Changes

    • Breaking Change: remove __field_defaults__, add default_factory support with BaseModel.construct. Use .get_default() method on fields in __fields__ attribute instead, #1732 by @PrettyWood
    • Rearrange CI to run linting as a separate job, split install recipes for different tasks, #2020 by @samuelcolvin
    • Allows subclasses of generic models to make some, or all, of the superclass's type parameters concrete, while also defining new type parameters in the subclass, #2005 by @choogeboom
    • Call validator with the correct values parameter type in BaseModel.__setattr__, when validate_assignment = True in model config, #1999 by @me-ransh
    • Force fields.Undefined to be a singleton object, fixing inherited generic model schemas, #1981 by @daviskirk
    • Include tests in source distributions, #1976 by @sbraz
    • Add ability to use min_length/max_length constraints with secret types, #1974 by @uriyyo
    • Also check root_validators when validate_assignment is on, #1971 by @PrettyWood
    • Fix const validators not running when custom validators are present, #1957 by @hmvp
    • add deque to field types, #1935 by @wozniakty
    • add basic support for python 3.9, #1832 by @PrettyWood
    • Fix typo in the anchor of exporting_models.md#modelcopy and incorrect description, #1821 by @KimMachineGun
    • Added ability for BaseSettings to read "secret files", #1820 by @mdgilene
    • add parse_raw_as utility function, #1812 by @PrettyWood
    • Support home directory relative paths for dotenv files (e.g. ~/.env), #1803 by @PrettyWood
    • Clarify documentation for parse_file to show that the argument should be a file path not a file-like object, #1794 by @mdavis-xyz
    • Fix false positive from mypy plugin when a class nested within a BaseModel is named Model, #1770 by @selimb
    • add basic support of Pattern type in schema generation, #1767 by @PrettyWood
    • Support custom title, description and default in schema of enums, #1748 by @PrettyWood
    • Properly represent Literal Enums when use_enum_values is True, #1747 by @noelevans
    • Allows timezone information to be added to strings to be formatted as time objects. Permitted formats are Z for UTC or an offset for absolute positive or negative time shifts. Or the timezone data can be omitted, #1744 by @noelevans
    • Add stub __init__ with python 3.6 signature for ForwardRef, #1738 by @sirtelemak
    • Fix behaviour with forward refs and optional fields in nested models, #1736 by @PrettyWood
    • add Enum and IntEnum as valid types for fields, #1735 by @PrettyWood
    • Change default value of __module__ argument of create_model from None to 'pydantic.main'. Set reference of created concrete model to it's module to allow pickling (not applied to models created in functions), #1686 by @MrMrRobat
    • Add private attributes support, #1679 by @MrMrRobat
    • add config to @validate_arguments, #1663 by @samuelcolvin
    • Allow descendant Settings models to override env variable names for the fields defined in parent Settings models with env in their Config. Previously only env_prefix configuration option was applicable, #1561 by @ojomio
    • Support ref_template when creating schema $refs, #1479 by @kilo59
    • Add a __call__ stub to PyObject so that mypy will know that it is callable, #1352 by @brianmaissy
    • pydantic.dataclasses.dataclass decorator now supports built-in dataclasses.dataclass. It is hence possible to convert an existing dataclass easily to add pydantic validation. Moreover nested dataclasses are also supported, #744 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.6.1(Jul 15, 2020)

    See Changelog.

    Thank you to pydantic's sponsors: @matin, @tiangolo, @chdsbd, @jorgecarleitao, and 1 anonymous sponsor for their kind support.

    changes:

    • fix validation and parsing of nested models with default_factory, #1710 by @PrettyWood
    Source code(tar.gz)
    Source code(zip)
  • v1.6(Jul 11, 2020)

    See Changelog.

    Thank you to pydantic's sponsors: @matin, @tiangolo, @chdsbd, @jorgecarleitao, and 1 anonymous sponsor for their kind support.

    changes:

    • Modify validators for conlist and conset to not have always=True, #1682 by @samuelcolvin
    • add port check to AnyUrl (can't exceed 65536) ports are 16 insigned bits: 0 <= port <= 2**16-1 src: rfc793 header format, #1654 by @flapili
    • Document default regex anchoring semantics, #1648 by @yurikhan
    • Use chain.from_iterable in class_validators.py. This is a faster and more idiomatic way of using itertools.chain. Instead of computing all the items in the iterable and storing them in memory, they are computed one-by-one and never stored as a huge list. This can save on both runtime and memory space, #1642 by @cool-RR
    • Add conset(), analogous to conlist(), #1623 by @patrickkwang
    • make pydantic errors (un)pickable, #1616 by @PrettyWood
    • Allow custom encoding for dotenv files, #1615 by @PrettyWood
    • Ensure SchemaExtraCallable is always defined to get type hints on BaseConfig, #1614 by @PrettyWood
    • Update datetime parser to support negative timestamps, #1600 by @mlbiche
    • Update mypy, remove AnyType alias for Type[Any], #1598 by @samuelcolvin
    • Adjust handling of root validators so that errors are aggregated from all failing root validators, instead of reporting on only the first root validator to fail, #1586 by @beezee
    • Make __modify_schema__ on Enums apply to the enum schema rather than fields that use the enum, #1581 by @therefromhere
    • Fix behavior of __all__ key when used in conjunction with index keys in advanced include/exclude of fields that are sequences, #1579 by @xspirus
    • Subclass validators do not run when referencing a List field defined in a parent class when each_item=True. Added an example to the docs illustrating this, #1566 by @samueldeklund
    • change schema.field_class_to_schema to support frozenset in schema, #1557 by @wangpeibao
    • Call __modify_schema__ only for the field schema, #1552 by @PrettyWood
    • Move the assignment of field.validate_always in fields.py so the always parameter of validators work on inheritance, #1545 by @dcHHH
    • Added support for UUID instantiation through 16 byte strings such as b'\x12\x34\x56\x78' * 4. This was done to support BINARY(16) columns in sqlalchemy, #1541 by @shawnwall
    • Add a test assertion that default_factory can return a singleton, #1523 by @therefromhere
    • Add NameEmail.__eq__ so duplicate NameEmail instances are evaluated as equal, #1514 by @stephen-bunn
    • Add datamodel-code-generator link in pydantic document site, #1500 by @koxudaxi
    • Added a "Discussion of Pydantic" section to the documentation, with a link to "Pydantic Introduction" video by Alexander Hultnér, #1499 by @hultner
    • Avoid some side effects of default_factory by calling it only once if possible and by not setting a default value in the schema, #1491 by @PrettyWood
    • Added docs about dumping dataclasses to JSON, #1487 by @mikegrima
    • Make BaseModel.__signature__ class-only, so getting __signature__ from model instance will raise AttributeError, #1466 by @MrMrRobat
    • include 'format': 'password' in the schema for secret types, #1424 by @atheuz
    • Modify schema constraints on ConstrainedFloat so that exclusiveMinimum and minimum are not included in the schema if they are equal to -math.inf and exclusiveMaximum and maximum are not included if they are equal to math.inf, #1417 by @vdwees
    • Squash internal __root__ dicts in .dict() (and, by extension, in .json()), #1414 by @patrickkwang
    • Move const validator to post-validators so it validates the parsed value, #1410 by @selimb
    • Fix model validation to handle nested literals, e.g. Literal['foo', Literal['bar']], #1364 by @DBCerigo
    • Remove user_required = True from RedisDsn, neither user nor password are required, #1275 by @samuelcolvin
    • Remove extra allOf from schema for fields with Union and custom Field, #1209 by @mostaphaRoudsari
    • Updates OpenAPI schema generation to output all enums as separate models. Instead of inlining the enum values in the model schema, models now use a $ref property to point to the enum definition, #1173 by @calvinwyoung
    Source code(tar.gz)
    Source code(zip)
  • v1.5.1(Apr 23, 2020)

  • v1.5(Apr 18, 2020)

    See Changelog.

    • Make includes/excludes arguments for .dict(), ._iter(), ..., immutable, #1404 by @AlexECX
    • Always use a field's real name with includes/excludes in model._iter(), regardless of by_alias, #1397 by @AlexECX
    • Update constr regex example to include start and end lines, #1396 by @lmcnearney
    • Confirm that shallow model.copy() does make a shallow copy of attributes, #1383 by @samuelcolvin
    • Renaming model_name argument of main.create_model() to __model_name to allow using model_name as a field name, #1367 by @kittipatv
    • Replace raising of exception to silent passing for non-Var attributes in mypy plugin, #1345 by @b0g3r
    • Remove typing_extensions dependency for python 3.8, #1342 by @prettywood
    • Make SecretStr and SecretBytes initialization idempotent, #1330 by @atheuz
    • document making secret types dumpable using the json method, #1328 by @atheuz
    • Move all testing and build to github actions, add windows and macos binaries, thank you @StephenBrown2 for much help, #1326 by @samuelcolvin
    • fix card number length check in PaymentCardNumber, PaymentCardBrand now inherits from str, #1317 by @samuelcolvin
    • Have BaseModel inherit from Representation to make mypy happy when overriding __str__, #1310 by @FuegoFro
    • Allow None as input to all optional list fields, #1307 by @prettywood
    • Add datetime field to default_factory example, #1301 by @StephenBrown2
    • Allow subclasses of known types to be encoded with superclass encoder, #1291 by @StephenBrown2
    • Exclude exported fields from all elements of a list/tuple of submodels/dicts with '__all__', #1286 by @masalim2
    • Add pydantic.color.Color objects as available input for Color fields, #1258 by @leosussan
    • In examples, type nullable fields as Optional, so that these are valid mypy annotations, #1248 by @kokes
    • Make pattern_validator() accept pre-compiled Pattern objects. Fix str_validator() return type to str, #1237 by @adamgreg
    • Document how to manage Generics and inheritance, #1229 by @esadruhn
    • update_forward_refs() method of BaseModel now copies __dict__ of class module instead of modyfying it, #1228 by @paul-ilyin
    • Support instance methods and class methods with @validate_arguments, #1222 by @samuelcolvin
    • Add default_factory argument to Field to create a dynamic default value by passing a zero-argument callable, #1210 by @prettywood
    • add support for NewType of List, Optional, etc, #1207 by @Kazy
    • fix mypy signature for root_validator, #1192 by @samuelcolvin
    • Fixed parsing of nested 'custom root type' models, #1190 by @Shados
    • Add validate_arguments function decorator which checks the arguments to a function matches type annotations, #1179 by @samuelcolvin
    • Add __signature__ to models, #1034 by @MrMrRobat
    • Refactor ._iter() method, 10x speed boost for dict(model), #1017 by @MrMrRobat
    Source code(tar.gz)
    Source code(zip)
  • v1.4(Jan 24, 2020)

    See Changelog.

    • Breaking Change: alias precedence logic changed so aliases on a field always take priority over an alias from alias_generator to avoid buggy/unexpected behaviour, see here for details, #1178 by @samuelcolvin
    • Add support for unicode and punycode in TLDs, #1182 by @jamescurtin
    • Fix cls argument in validators during assignment, #1172 by @samuelcolvin
    • completing Luhn algorithm for PaymentCardNumber, #1166 by @cuencandres
    • add support for generics that implement __get_validators__ like a custom data type, #1159 by @tiangolo
    • add support for infinite generators with Iterable, #1152 by @tiangolo
    • fix url_regex to accept schemas with +, - and . after the first character, #1142 by @samuelcolvin
    • move version_info() to version.py, suggest its use in issues, #1138 by @samuelcolvin
    • Improve pydantic import time by roughly 50% by deferring some module loading and regex compilation, #1127 by @samuelcolvin
    • Fix EmailStr and NameEmail to accept instances of themselves in cython, #1126 by @koxudaxi
    • Pass model class to the Config.schema_extra callable, #1125 by @therefromhere
    • Fix regex for username and password in URLs, #1115 by @samuelcolvin
    • Add support for nested generic models, #1104 by @dmontagu
    • add __all__ to __init__.py to prevent "implicit reexport" errors from mypy, #1072 by @samuelcolvin
    • Add support for using "dotenv" files with BaseSettings, #1011 by @acnebs
    Source code(tar.gz)
    Source code(zip)
  • v1.3(Dec 21, 2019)

    See Changelog.

    • Change schema and schema_model to handle dataclasses by using their __pydantic_model__ feature, #792 by @aviramha
    • Added option for root_validator to be skipped if values validation fails using keyword skip_on_failure=True, #1049 by @aviramha
    • Allow Config.schema_extra to be a callable so that the generated schema can be post-processed, #1054 by @selimb
    • Update mypy to version 0.750, #1057 by @dmontagu
    • Trick Cython into allowing str subclassing, #1061 by @skewty
    • Prevent type attributes being added to schema unless the attribute __schema_attributes__ is True, #1064 by @samuelcolvin
    • Change BaseModel.parse_file to use Config.json_loads, #1067 by @kierandarcy
    • Fix for optional Json fields, #1073 by @volker48
    • Change the default number of threads used when compiling with cython to one, allow override via the CYTHON_NTHREADS environment variable, #1074 by @samuelcolvin
    • Run FastAPI tests during Pydantic's CI tests, #1075 by @tiangolo
    • My mypy strictness constraints, and associated tweaks to type annotations, #1077 by @samuelcolvin
    • Add __eq__ to SecretStr and SecretBytes to allow "value equals", #1079 by @sbv-trueenergy
    • Fix schema generation for nested None case, #1088 by @lutostag
    • Consistent checks for sequence like objects, #1090 by @samuelcolvin
    • Fix Config inheritance on BaseSettings when used with env_prefix, #1091 by @samuelcolvin
    • Fix for __modify_schema__ when it conflicted with field_class_to_schema*, #1102 by @samuelcolvin
    • docs: Fix explanation of case sensitive environment variable names when populating BaseSettings subclass attributes, #1105 by @tribals
    • Rename django-rest-framework benchmark in documentation, #1119 by @frankie567
    Source code(tar.gz)
    Source code(zip)
  • v1.2(Nov 28, 2019)

    See Changelog.

    • Add benchmarks for cattrs, #513 by @sebastianmika
    • Add exclude_none option to dict() and friends, #587 by @niknetniko
    • Add benchmarks for valideer, #670 by @gsakkis
    • Add parse_obj_as and parse_file_as functions for ad-hoc parsing of data into arbitrary pydantic-compatible types, #934 by @dmontagu
    • Add allow_reuse argument to validators, thus allowing validator reuse, #940 by @dmontagu
    • Add support for mapping types for custom root models, #958 by @dmontagu
    • Mypy plugin support for dataclasses, #966 by @koxudaxi
    • Add support for dataclasses default factory, #968 by @ahirner
    • Add a ByteSize type for converting byte string (1GB) to plain bytes, #977 by @dgasmith
    • Fix mypy complaint about @root_validator(pre=True), #984 by @samuelcolvin
    • Add manylinux binaries for python 3.8 to pypi, also support manylinux2010, #994 by @samuelcolvin
    • Adds ByteSize conversion to another unit, #995 by @dgasmith
    • Fix __str__ and __repr__ inheritance for models, #1022 by @samuelcolvin
    • add testimonials section to docs, #1025 by @sullivancolin
    • Add support for typing.Literal for Python 3.8, #1026 by @dmontagu
    • Add support for required Optional with name: Optional[AnyType] = Field(...) and refactor ModelField creation to preserve required parameter value, #1031 by @tiangolo
    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(Nov 20, 2019)

  • v1.1(Nov 7, 2019)

    See Changelog.

    • Add a mypy plugin for type checking BaseModel.__init__ and more, #722 by @dmontagu
    • Change return type typehint for GenericModel.__class_getitem__ to prevent PyCharm warnings, #936 by @dmontagu
    • Fix usage of Any to allow None, also support TypeVar thus allowing use of un-parameterised collection types e.g. Dict and List, #962 by @samuelcolvin
    • Set FieldInfo on subfields to fix schema generation for complex nested types, #965 by @samuelcolvin
    Source code(tar.gz)
    Source code(zip)
  • v1.0(Oct 23, 2019)

    See Changelog.

    • Breaking Change: deprecate the Model.fields property, use Model.__fields__ instead, #883 by @samuelcolvin
    • Breaking Change: Change the precedence of aliases so child model aliases override parent aliases, including using alias_generator, #904 by @samuelcolvin
    • Breaking change: Rename skip_defaults to exclude_unset, and add ability to exclude actual defaults, #915 by @dmontagu
    • Add **kwargs to pydantic.main.ModelMetaclass.__new__ so __init_subclass__ can take custom parameters on extended BaseModel classes, #867 by @retnikt
    • Fix field of a type that has a default value, #880 by @koxudaxi
    • Use FutureWarning instead of DeprecationWarning when alias instead of env is used for settings models, #881 by @samuelcolvin
    • Fix issue with BaseSettings inheritance and alias getting set to None, #882 by @samuelcolvin
    • Modify __repr__ and __str__ methods to be consistent across all public classes, add __pretty__ to support python-devtools, #884 by @samuelcolvin
    • deprecation warning for case_insensitive on BaseSettings config, #885 by @samuelcolvin
    • For BaseSettings merge environment variables and in-code values recursively, as long as they create a valid object when merged together, to allow splitting init arguments, #888 by @idmitrievsky
    • change secret types example, #890 by @ashears
    • Change the signature of Model.construct() to be more user-friendly, document construct() usage, #898 by @samuelcolvin
    • Add example for the construct() method, #907 by @ashears
    • Improve use of Field constraints on complex types, raise an error if constraints are not enforceable, also support tuples with an ellipsis Tuple[X, ...], Sequence and FrozenSet in schema, #909 by @samuelcolvin
    • update docs for bool missing valid value, #911 by @trim21
    • Better str/repr logic for ModelField, #912 by @samuelcolvin
    • Fix ConstrainedList, update schema generation to reflect min_items and max_items Field() arguments, #917 by @samuelcolvin
    • Allow abstracts sets (eg. dict keys) in the include and exclude arguments of dict(), #921 by @samuelcolvin
    • Fix JSON serialization errors on ValidationError.json() by using pydantic_encoder, #922 by @samuelcolvin
    • Clarify usage of remove_untouched, improve error message for types with no validators, #926 by @retnikt
    Source code(tar.gz)
    Source code(zip)
  • v1.0b2(Oct 7, 2019)

    • Mark StrictBool typecheck as bool to allow for default values without mypy errors, #690 by @dmontagu
    • Transfer the documentation build from sphinx to mkdocs, re-write much of the documentation, #856 by @samuelcolvin
    • Add support for custom naming schemes for GenericModel subclasses, #859 by @dmontagu
    • Add if TYPE_CHECKING: to the excluded lines for test coverage, #874 by @dmontagu
    • Rename allow_population_by_alias to allow_population_by_field_name, remove unnecessary warning about it, #875 by @samuelcolvin

    see HISTORY.md

    Source code(tar.gz)
    Source code(zip)
  • v0.32.2(Aug 17, 2019)

    • fix __post_init__ usage with dataclass inheritance, fix #739 by @samuelcolvin
    • fix required fields validation on GenericModels classes, #742 by @amitbl
    • fix defining custom Schema on GenericModel fields, #754 by @amitbl
    Source code(tar.gz)
    Source code(zip)
  • v0.32.1(Aug 8, 2019)

  • v0.32(Aug 6, 2019)

  • v0.31.1(Jul 31, 2019)

  • v0.31(Jul 24, 2019)

  • v0.30.1(Jul 15, 2019)

  • v0.30(Jul 7, 2019)

  • v0.29(Jun 19, 2019)

  • v0.28(Jun 11, 2019)

Owner
Samuel Colvin
Software developer and CTO @nell-health. Python, javascript, rust.
Samuel Colvin
Inspects Python source files and provides information about type and location of classes, methods etc

prospector About Prospector is a tool to analyse Python code and output information about errors, potential problems, convention violations and comple

Python Code Quality Authority 1.5k Nov 24, 2021
Performant type-checking for python.

Pyre is a performant type checker for Python compliant with PEP 484. Pyre can analyze codebases with millions of lines of code incrementally – providi

Facebook 5.7k Dec 3, 2021
A system for Python that generates static type annotations by collecting runtime types

MonkeyType MonkeyType collects runtime types of function arguments and return values, and can automatically generate stub files or even add draft type

Instagram 3.6k Nov 28, 2021
A static type analyzer for Python code

pytype - ? ✔ Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Google 3.5k Dec 3, 2021
Static type checker for Python

Static type checker for Python Speed Pyright is a fast type checker meant for large Python source bases. It can run in a “watch” mode and performs fas

Microsoft 7.4k Dec 2, 2021
Unbearably fast O(1) runtime type-checking in pure Python.

Look for the bare necessities, the simple bare necessities. Forget about your worries and your strife. — The Jungle Book.

null 953 Dec 2, 2021
Run-time type checker for Python

This library provides run-time type checking for functions defined with PEP 484 argument (and return) type annotations. Four principal ways to do type

Alex Grönholm 765 Nov 25, 2021
pycallgraph is a Python module that creates call graphs for Python programs.

Project Abandoned Many apologies. I've stopped maintaining this project due to personal time constraints. Blog post with more information. I'm happy t

gak 1.6k Nov 25, 2021
Turn your Python and Javascript code into DOT flowcharts

Notes from 2017 This is an older project which I am no longer working on. It was built before ES6 existed and before Python 3 had much usage. While it

Scott Rogowski 1.2k Dec 3, 2021
The strictest and most opinionated python linter ever!

wemake-python-styleguide Welcome to the strictest and most opinionated python linter ever. wemake-python-styleguide is actually a flake8 plugin with s

wemake.services 1.7k Dec 3, 2021
Optional static typing for Python 3 and 2 (PEP 484)

Mypy: Optional Static Typing for Python Got a question? Join us on Gitter! We don't have a mailing list; but we are always happy to answer questions o

Python 11.9k Nov 25, 2021
Python package to parse and generate C/C++ code as context aware preprocessor.

Devana Devana is a python tool that make it easy to parsing, format, transform and generate C++ (or C) code. This tool uses libclang to parse the code

null 2 Nov 8, 2021
Collects all accepted (partial and full scored) codes submitted within the given timeframe and saves them locally for plagiarism check.

Collects all accepted (partial and full scored) codes submitted within the given timeframe of any contest.

ARITRA BELEL 2 Jul 1, 2021
TidyPy is a tool that encapsulates a number of other static analysis tools and makes it easy to configure, execute, and review their results.

TidyPy Contents Overview Features Usage Docker Configuration Ignoring Issues Included Tools Included Reporters Included Integrations Extending TidyPy

Jason Simeone 30 Nov 6, 2021
Find dead Python code

Vulture - Find dead code Vulture finds unused code in Python programs. This is useful for cleaning up and finding errors in large code bases. If you r

Jendrik Seipp 1.9k Nov 26, 2021
Code audit tool for python.

Pylama Code audit tool for Python and JavaScript. Pylama wraps these tools: pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna; pydocstyle (form

Kirill Klenov 822 Nov 28, 2021
The uncompromising Python code formatter

The Uncompromising Code Formatter “Any color you like.” Black is the uncompromising Python code formatter. By using it, you agree to cede control over

Python Software Foundation 23.8k Dec 2, 2021
A Python utility / library to sort imports.

Read Latest Documentation - Browse GitHub Code Repository isort your imports, so you don't have to. isort is a Python utility / library to sort import

Python Code Quality Authority 4.3k Dec 4, 2021
A formatter for Python files

YAPF Introduction Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- are made to remove lint errors from code. This has som

Google 12.2k Dec 3, 2021