Skip to content

Json decode not working when the field's annotation is defined by using the type statement #778

@Chotom

Description

@Chotom

Description

According to Python's Document Type aliases, type aliases can be created through simple assignment or using the type statement. The two ways should be equivalent.

However, as the example code below, If a field's annotation is a type aliases defined by using the type statement, the json decoding doesn't trigger (settings B and C).

This is very similar issue to the #715 which was recently fixed in 2.13, but in this case the ForceDecode is not working in both cases: defined explicite and implicite (by deafult). While the code sample from #715) passed and seems to be fixed, the below code does not.

Example Code

from typing import Annotated, TypeVar

from annotated_types import Len
from pydantic_settings import BaseSettings, ForceDecode

T = TypeVar("T")

MaxLenA = Annotated[T, Len(max_length=4)]
type MaxLenB[_T] = Annotated[_T, Len(max_length=4)]
type MaxLenC[_T] = Annotated[_T, Len(max_length=4), ForceDecode]


class MySettingsA(BaseSettings):
    SIMPLE_LIST: MaxLenA[list[str]]


class MySettingsB(BaseSettings):
    SIMPLE_LIST: MaxLenB[list[str]]


class MySettingsC(BaseSettings):
    SIMPLE_LIST: MaxLenC[list[str]]


if __name__ == "__main__":
    import os

    os.environ["SIMPLE_LIST"] = r'["a", "b", "c"]'
    my_settings_a = MySettingsA()
    print(my_settings_a)

    # The below settings will raise a validation error:
    #  Input should be a valid list [type=list_type, input_value='["a", "b", "c"]', input_type=str]
    my_settings_b = MySettingsB()
    print(my_settings_b)
    my_settings_c = MySettingsC()
    print(my_settings_c)

The Result

SIMPLE_LIST=[1, 2, 3]
Traceback (most recent call last):
  File ".../pydantic-bug.py", line 36, in <module>
    my_settings_b = MySettingsB()
                    ^^^^^^^^^^^^^
  File ".../.venv/lib/python3.12/site-packages/pydantic_settings/main.py", line 242, in __init__
    super().__init__(**__pydantic_self__.__class__._settings_build_values(sources, init_kwargs))
  File ".../.venv/lib/python3.12/site-packages/pydantic/main.py", line 250, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for MySettingsB
SIMPLE_LIST
  Input should be a valid list [type=list_type, input_value='["a", "b", "c"]', input_type=str]
    For further information visit https://errors.pydantic.dev/2.12/v/list_type

Version

             pydantic version: 2.12.5
        pydantic-core version: 2.41.5
          pydantic-core build: profile=release pgo=false
               python version: 3.12.11 (main, Jun  3 2025, 15:41:47) [Clang 17.0.0 (clang-1700.0.13.3)]
                     platform: macOS-26.3-arm64-arm-64bit
             related packages: email-validator-2.3.0 typing_extensions-4.15.0 pydantic-settings-2.13.0
                       commit: unknown

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions