Skip to content

Commit d9b28ef

Browse files
committed
Fix handle type alias declared with type statement
1 parent 6bae3ab commit d9b28ef

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

pydantic_settings/sources/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def parse_env_vars(
4343
def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) -> bool:
4444
# If the model is a root model, the root annotation should be used to
4545
# evaluate the complexity.
46+
if annotation and (
47+
typing_objects.is_typealiastype(annotation) or typing_objects.is_typealiastype(get_origin(annotation))
48+
):
49+
annotation = annotation.__value__
4650
if annotation is not None and _lenient_issubclass(annotation, RootModel) and annotation is not RootModel:
4751
annotation = cast('type[RootModel[Any]]', annotation)
4852
root_annotation = annotation.model_fields['root'].annotation

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ pydocstyle = { convention = 'google' }
143143
[tool.ruff.format]
144144
quote-style = 'single'
145145

146+
[tool.ruff.per-file-target-version]
147+
"tests/test_settings_py312.py" = "py312"
148+
146149
[tool.mypy]
147150
python_version = '3.10'
148151
show_error_codes = true

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
from pathlib import Path
56
from typing import TYPE_CHECKING
67

@@ -99,3 +100,20 @@ def cli_test_env():
99100
yield setenv
100101

101102
setenv.clear()
103+
104+
105+
def pytest_ignore_collect(collection_path: Path, config) -> bool:
106+
"""
107+
Ignore collection of test files that contain syntax unsupported
108+
by the current Python version.
109+
"""
110+
py312_tests = {'test_settings_py312.py'}
111+
112+
if sys.version_info < (3, 12) and collection_path.name in py312_tests:
113+
print(
114+
f"\nSkipping collection of '{collection_path.name}' on Python "
115+
f'{sys.version_info.major}.{sys.version_info.minor} due to syntax requirements.'
116+
)
117+
return True
118+
119+
return None

tests/test_settings_py312.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Annotated
2+
3+
from annotated_types import MinLen
4+
5+
from pydantic_settings import BaseSettings
6+
7+
try:
8+
import dotenv
9+
except ImportError:
10+
dotenv = None
11+
12+
13+
def test_annotated_with_type(env):
14+
type MinLenList = Annotated[list[str], MinLen(2)]
15+
16+
class AnnotatedComplexSettings(BaseSettings):
17+
apples: MinLenList
18+
19+
env.set('apples', '["russet", "granny smith"]')
20+
s = AnnotatedComplexSettings()
21+
assert s.apples == ['russet', 'granny smith']

0 commit comments

Comments
 (0)