Skip to content

Conversation

@YuriiMotov
Copy link
Member

@YuriiMotov YuriiMotov commented Jan 28, 2026

This PR updates type hints for validation_alias parameter of Field to be in line with Pydantic's version:

    validation_alias: str | AliasPath | AliasChoices | None = _Unset,

Also, added tests for AliasPath and AliasChoices


The following code works on master:

from typing import Optional

from pydantic import AliasChoices, AliasPath
from sqlmodel import Field, SQLModel


class User(SQLModel):
    first_name: str = Field(validation_alias="firstName", serialization_alias="f_name")
    second_name: Optional[str] = Field(
        default=None, validation_alias=AliasChoices("secondName", "surname")
    )
    nickname: Optional[str] = Field(
        default=None, validation_alias=AliasPath("names", 2)
    )


test_data = [
    {"firstName": "John", "secondName": "Doe", "names": ["John", "Doe", "Johnny"]},
    {"firstName": "John", "surname": "Doe", "names": ["John", "Doe", "Johnny"]},
]


for data in test_data:
    print(User.model_validate(data))

# first_name='John' second_name='Doe' nickname='Johnny'
# first_name='John' second_name='Doe' nickname='Johnny'

.. but mypy argues with:

main.py:9: error: No overload variant of "Field" matches argument types "None", "AliasChoices"  [call-overload]
main.py:12: error: No overload variant of "Field" matches argument types "None", "AliasPath"  [call-overload]

This PR fixes this.

@YuriiMotov YuriiMotov marked this pull request as ready for review January 28, 2026 21:02
@YuriiMotov YuriiMotov added feature New feature or request and removed refactor labels Jan 29, 2026
@YuriiMotov YuriiMotov force-pushed the validation_alias-type-hints branch from 63578da to b15644e Compare January 29, 2026 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants