-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle forward refs in pydantic.
- Loading branch information
Showing
3 changed files
with
115 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from attr import dataclass as attr_dataclass | ||
from dataclass_settings import load_settings | ||
from pydantic import BaseModel | ||
from pydantic.dataclasses import dataclass as pydantic_dataclass | ||
|
||
|
||
class PydanticConfig(BaseModel): | ||
foo: PydanticFoo | ||
|
||
|
||
class PydanticFoo(BaseModel): | ||
foo: int = 0 | ||
|
||
|
||
def test_pydantic(): | ||
config = load_settings(PydanticConfig) | ||
assert config == PydanticConfig(foo=PydanticFoo(foo=0)) | ||
|
||
|
||
@dataclass | ||
class DataclassConfig: | ||
foo: DataclassFoo | ||
|
||
|
||
@dataclass | ||
class DataclassFoo: | ||
foo: int = 0 | ||
|
||
|
||
def test_dataclass(): | ||
config = load_settings(DataclassConfig) | ||
assert config == DataclassConfig(foo=DataclassFoo(foo=0)) | ||
|
||
|
||
@pydantic_dataclass | ||
class PDataclassConfig: | ||
foo: PDataclassFoo | ||
|
||
|
||
@pydantic_dataclass | ||
class PDataclassFoo: | ||
foo: int = 0 | ||
|
||
|
||
def test_pydantic_dataclass(): | ||
config = load_settings(PDataclassConfig) | ||
assert config == PDataclassConfig(foo=PDataclassFoo(foo=0)) | ||
|
||
|
||
@attr_dataclass | ||
class AttrConfig: | ||
foo: AttrFoo | ||
|
||
|
||
@attr_dataclass | ||
class AttrFoo: | ||
foo: int = 0 | ||
|
||
|
||
def test_attr_dataclass(): | ||
config = load_settings(AttrConfig) | ||
assert config == AttrConfig(foo=AttrFoo(foo=0)) |