pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/fastapi/sqlmodel/pull/1729

ssets/global-0bd78641c0a1f3e0.css" /> 🏷️ Allow `Discriminator` type for `discriminator` parameter in `Field` by YuriiMotov · Pull Request #1729 · fastapi/sqlmodel · GitHub
Skip to content

🏷️ Allow Discriminator type for discriminator parameter in Field#1729

Open
YuriiMotov wants to merge 3 commits intomainfrom
discriminator-type-hints
Open

🏷️ Allow Discriminator type for discriminator parameter in Field#1729
YuriiMotov wants to merge 3 commits intomainfrom
discriminator-type-hints

Conversation

@YuriiMotov
Copy link
Copy Markdown
Member

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

    discriminator: str | types.Discriminator | None = _Unset,

Also, added tests for callable discriminator.


The following code works on master:

from collections.abc import Hashable
from typing import Annotated, Any, Literal, Union

from pydantic import Discriminator, Tag
from sqlmodel import Field, SQLModel

class Pie(SQLModel):
    time_to_cook: int
    num_ingredients: int

class ApplePie(Pie):
    fruit: Literal["apple"] = "apple"

class PumpkinPie(Pie):
    filling: Literal["pumpkin"] = "pumpkin"

def get_discriminator_value(v: Any) -> Hashable:
    if isinstance(v, dict):
        return v.get("fruit", v.get("filling"))
    return getattr(v, "fruit", getattr(v, "filling", None))

    class ThanksgivingDinner(SQLModel):
        dessert: Union[
            Annotated[ApplePie, Tag("apple")],
            Annotated[PumpkinPie, Tag("pumpkin")],
        ] = Field(
            discriminator=Discriminator(get_discriminator_value),
        )

apple_variation = ThanksgivingDinner.model_validate(
    {"dessert": {"fruit": "apple", "time_to_cook": 60, "num_ingredients": 8}}
)

pumpkin_variation = ThanksgivingDinner.model_validate(
    {"dessert": {"filling": "pumpkin", "time_to_cook": 40, "num_ingredients": 6}}
)

print(type(apple_variation.dessert))
# <class '__main__.ApplePie'>

print(type(pumpkin_variation.dessert))
# <class '__main__.PumpkinPie'>

.. but mypy argues with:

error: No overload variant of "Field" matches argument types "EllipsisType", "Discriminator"  [call-overload]

This PR fixes this

@YuriiMotov YuriiMotov added the feature New feature or request label Jan 29, 2026
@YuriiMotov YuriiMotov changed the title Allow Discriminator for discriminator in Field 🏷️ Allow Discriminator for discriminator in Field Jan 29, 2026
@YuriiMotov YuriiMotov marked this pull request as ready for review January 29, 2026 09:29
@YuriiMotov YuriiMotov force-pushed the discriminator-type-hints branch from 80d2b66 to 80f8986 Compare January 29, 2026 09:33
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Feb 20, 2026
@github-actions

This comment was marked as resolved.

@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Feb 20, 2026
@YuriiMotov YuriiMotov changed the title 🏷️ Allow Discriminator for discriminator in Field 🏷️ Allow Discriminator type for discriminator parameter in Field Feb 20, 2026
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

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy