v1.8
版本发布时间: 2021-02-27 00:48:56
pydantic/pydantic最新发布版本:v2.9.2(2024-09-17 22:59:21)
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
- Hypothesis plugin for testing, #2097 by @Zac-HD
- support for
NamedTuple
andTypedDict
, #2216 by @PrettyWood - Support
Annotated
hints on model fields, #2147 by @JacobHayes -
frozen
parameter onConfig
to allow models to be hashed, #1880 by @rhuille
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 byField
- remove
Config.case_insensitive
which was replaced byConfig.case_sensitive
(defaultFalse
) - remove
Config.allow_population_by_alias
which was replaced byConfig.allow_population_by_field_name
- remove
model.fields
which was replaced bymodel.__fields__
- remove
model.to_string()
which was replaced bystr(model)
- remove
model.__values__
which was replaced bymodel.__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 handletyping.GenericAlias
objects likelist[str]
in python >= 3.9, #2399 by @daviskirk - Improve field declaration for pydantic
dataclass
by allowing the usage of pydanticField
or'metadata'
kwarg ofdataclasses.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 todict
, #2325 by @ofek - fix: allow
None
for typeOptional[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__
inBaseModel.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 usingparse_obj()
with nested models, #2238 by @PrettyWood - Support custom root type (aka
__root__
) withfrom_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
andTypedDict
types. Those two types are now handled and validated when used insideBaseModel
or pydanticdataclass
. Two utils are also addedcreate_model_from_namedtuple
andcreate_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 aroot_validator
does not return adict
(e.g.None
), #2209 by @masalim2 - Add a
FrozenSet[str]
type annotation to theallowed_schemes
argument on thestrict_url
field type, #2198 by @Midnighter - add
allow_mutation
constraint toField
, #2195 by @sblack-usu - Allow
Field
with adefault_factory
to be used as an argument to a function decorated withvalidate_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. AField
may now be set in the type hint withAnnotated[..., Field(...)
; all other annotations are ignored but still visible withget_type_hints(..., include_extras=True)
, #2147 by @JacobHayes - Added
StrictBytes
type as well asstrict=False
option toConstrainedBytes
, #2136 by @rlizzo - added
Config.anystr_lower
andto_lower
kwarg toconstr
andconbytes
, #2134 by @tayoogunbiyi - Support plain
typing.Tuple
type, #2132 by @PrettyWood - Add a bound method
validate
to functions decorated withvalidate_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
orLiteral[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
ClassVar
s 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 multipleBaseModel
with the same names declared in separate classes, #1912 by @JSextonn - Add
rediss
(Redis over SSL) protocol toRedisDsn
Allow URLs withoutuser
part (e.g.,rediss://:pass@localhost
), #1911 by @TrDex - Add a new
frozen
boolean parameter toConfig
(default:False
). Settingfrozen=True
does everything thatallow_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 recursiveGenericModel
s, #1370 by @xppt - use
enum
fortyping.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 toFalse
, pydantic will keep models used as fields untouched on validation instead of reconstructing (copying) them, #265 by @PrettyWood