Skip to content

Commit 46ae4a0

Browse files
committed
Fix PR comments
* Correctly identify unions and complex types * Drop python 3.12 specific syntax
1 parent 924de34 commit 46ae4a0

File tree

5 files changed

+22
-48
lines changed

5 files changed

+22
-48
lines changed

pydantic_settings/sources/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pydantic._internal._utils import is_model_class
1717
from pydantic.fields import FieldInfo
1818
from typing_extensions import get_args
19+
from typing_inspection import typing_objects
1920
from typing_inspection.introspection import is_union_origin
2021

2122
from ..exceptions import SettingsError
@@ -25,6 +26,7 @@
2526
_annotation_is_complex,
2627
_get_alias_names,
2728
_get_model_fields,
29+
_strip_annotated,
2830
_union_is_complex,
2931
)
3032

@@ -353,7 +355,10 @@ def _extract_field_info(self, field: FieldInfo, field_name: str) -> list[tuple[s
353355
field_info.append((v_alias, self._apply_case_sensitive(v_alias), False))
354356

355357
if not v_alias or self.config.get('populate_by_name', False):
356-
if is_union_origin(get_origin(field.annotation)) and _union_is_complex(field.annotation, field.metadata):
358+
annotation = field.annotation
359+
if annotation and typing_objects.is_typealiastype(field.annotation):
360+
annotation = _strip_annotated(annotation.__value__)
361+
if is_union_origin(get_origin(annotation)) and _union_is_complex(annotation, field.metadata):
357362
field_info.append((field_name, self._apply_case_sensitive(self.env_prefix + field_name), True))
358363
else:
359364
field_info.append((field_name, self._apply_case_sensitive(self.env_prefix + field_name), False))

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ 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-
149146
[tool.mypy]
150147
python_version = '3.10'
151148
show_error_codes = true

tests/conftest.py

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

33
import os
4-
import sys
54
from pathlib import Path
65
from typing import TYPE_CHECKING
76

@@ -100,20 +99,3 @@ def cli_test_env():
10099
yield setenv
101100

102101
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.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
dataclasses as pydantic_dataclasses,
3535
)
3636
from pydantic.fields import FieldInfo
37-
from typing_extensions import override
37+
from typing_extensions import TypeAliasType, override
3838

3939
from pydantic_settings import (
4040
BaseSettings,
@@ -474,6 +474,21 @@ class AnnotatedComplexSettings(BaseSettings):
474474
]
475475

476476

477+
def test_annotated_with_type(env):
478+
"""https://github.com/pydantic/pydantic-settings/issues/536.
479+
480+
PEP 695 type aliases need to be analyzed when determining if an annotation is complex.
481+
"""
482+
MinLenList = TypeAliasType('MinLenList', Annotated[Union[list[str], list[int]], MinLen(2)])
483+
484+
class AnnotatedComplexSettings(BaseSettings):
485+
apples: MinLenList
486+
487+
env.set('apples', '["russet", "granny smith"]')
488+
s = AnnotatedComplexSettings()
489+
assert s.apples == ['russet', 'granny smith']
490+
491+
477492
def test_set_dict_model(env):
478493
env.set('bananas', '[1, 2, 3, 3]')
479494
env.set('CARROTS', '{"a": null, "b": 4}')

tests/test_settings_py312.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)