
GITHUB . COM {
}
Detected CMS Systems:
- Wordpress (2 occurrences)
Title:
Discriminator field on BaseModel instances are incorrectly accessed by alias in validation Β· Issue #3846 Β· pydantic/pydantic
Description:
Checks I added a descriptive title to this issue I have searched (google, github) for similar issues and couldn't find anything I have read and followed the docs and still think this is a bug B...
Website Age:
17 years and 8 months (reg. 2007-10-09).
Matching Content Categories {π}
- TV
- Fitness & Wellness
- Photography
Content Management System {π}
What CMS is github.com built with?
Github.com uses WORDPRESS.
Traffic Estimate {π}
What is the average monthly size of github.com audience?
ππ Tremendous Traffic: 10M - 20M visitors per month
Based on our best estimate, this website will receive around 10,653,634 visitors per month in the current month.
check SE Ranking
check Ahrefs
check Similarweb
check Ubersuggest
check Semrush
How Does Github.com Make Money? {πΈ}
Subscription Packages {π³}
We've located a dedicated page on github.com that might include details about subscription plans or recurring payments. We identified it based on the word pricing in one of its internal links. Below, you'll find additional estimates for its monthly recurring revenues.How Much Does Github.com Make? {π°}
Subscription Packages {π³}
Prices on github.com are in US Dollars ($).
They range from $4.00/month to $21.00/month.
We estimate that the site has approximately 5,316,035 paying customers.
The estimated monthly recurring revenue (MRR) is $22,327,346.
The estimated annual recurring revenues (ARR) are $267,928,149.
Wordpress Themes and Plugins {π¨}
What WordPress theme does this site use?
It is strange but we were not able to detect any theme on the page.
What WordPress plugins does this website use?
It is strange but we were not able to detect any plugins on the page.
Keywords {π}
discriminator, pydantic, field, literal, basemodel, validation, bug, alias, issue, sign, chornsby, python, import, union, access, class, added, github, instance, key, pydanticerrorwrappersvalidationerror, error, top, missing, typevalueerrordiscriminatedunionmissingdiscriminator, discriminatorkeyliteral, mentioned, navigation, issues, pull, requests, actions, security, instances, incorrectly, accessed, closed, docs, pydanticutils, printpydanticutilsversioninfo, version, true, discriminated, model, validatediscriminatedunion, function, fieldaliaslit, related, fix, type,
Topics {βοΈ}
85e2f5c chornsby mentioned /venv/lib/python3 comment metadata assignees access discriminator field similar issues literal['a'] = field literal['b'] = field chornsby added typing import literal pydantic model instance discriminator field pydantic v1 pydantic import basemodel pydantic version docs discriminator='literal' discriminator key import pydantic literal='b' ['typing-extensions'] lit='a' lit='b' discriminator_key=literal class config validation fails 1 validation error incorrectly accessed descriptive title red hat 11 x86_64-x86_64 _validate_discriminated_union function tiny mistake basemodel instance projects milestone milestone relationships issue type=value_error type projects ] = field basemodel instances discriminated union added alias='lit' discriminator github pydantic literal class python $ python
Payment Methods {π}
- Braintree
Questions {β}
- Already have an account?
Schema {πΊοΈ}
DiscussionForumPosting:
context:https://schema.org
headline:Discriminator field on BaseModel instances are incorrectly accessed by alias in validation
articleBody:### Checks
* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug
# Bug
Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
$ python -c "import pydantic.utils; print(pydantic.utils.version_info())"
pydantic version: 1.9.0
pydantic compiled: True
install path: ./venv/lib/python3.10/site-packages/pydantic
python version: 3.10.2 (main, Jan 17 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]
platform: Linux-5.16.9-200.fc35.x86_64-x86_64-with-glibc2.34
optional deps. installed: ['typing-extensions']
```
When validating a discriminated union where the union value has been passed as a Pydantic model instance, it appears that the `_validate_discriminated_union` function is trying to access the value of the discriminator field via its alias. Since the alias does not exist on the model as an attribute, the validation fails.
I believe this is the result of a tiny mistake when setting up the `_validate_discriminated_union` function and that whenever a `BaseModel` or dataclass is detected then we should use the discriminator key and not the alias to access the discriminator value.
```py
from typing import Literal, Union
from pydantic import BaseModel, Field
class A(BaseModel):
literal: Literal['a'] = Field(alias='lit')
class B(BaseModel):
literal: Literal['b'] = Field(alias='lit')
class Config:
allow_population_by_field_name = True
class Top(BaseModel):
sub: Union[A, B] = Field(..., discriminator='literal')
Top(sub=A(lit='a'))
# pydantic.error_wrappers.ValidationError: 1 validation error for Top
# sub
# Discriminator 'literal' is missing in value (type=value_error.discriminated_union.missing_discriminator; discriminator_key=literal)
Top(sub=B(lit='b'))
# pydantic.error_wrappers.ValidationError: 1 validation error for Top
# sub
# Discriminator 'literal' is missing in value (type=value_error.discriminated_union.missing_discriminator; discriminator_key=literal)
Top(sub=B(literal='b'))
# pydantic.error_wrappers.ValidationError: 1 validation error for Top
# sub
# Discriminator 'literal' is missing in value (type=value_error.discriminated_union.missing_discriminator; discriminator_key=literal)
```
author:
url:https://github.com/chornsby
type:Person
name:chornsby
datePublished:2022-02-22T09:56:44.000Z
interactionStatistic:
type:InteractionCounter
interactionType:https://schema.org/CommentAction
userInteractionCount:0
url:https://github.com/3846/pydantic/issues/3846
context:https://schema.org
headline:Discriminator field on BaseModel instances are incorrectly accessed by alias in validation
articleBody:### Checks
* [x] I added a descriptive title to this issue
* [x] I have searched (google, github) for similar issues and couldn't find anything
* [x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug
# Bug
Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
$ python -c "import pydantic.utils; print(pydantic.utils.version_info())"
pydantic version: 1.9.0
pydantic compiled: True
install path: ./venv/lib/python3.10/site-packages/pydantic
python version: 3.10.2 (main, Jan 17 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]
platform: Linux-5.16.9-200.fc35.x86_64-x86_64-with-glibc2.34
optional deps. installed: ['typing-extensions']
```
When validating a discriminated union where the union value has been passed as a Pydantic model instance, it appears that the `_validate_discriminated_union` function is trying to access the value of the discriminator field via its alias. Since the alias does not exist on the model as an attribute, the validation fails.
I believe this is the result of a tiny mistake when setting up the `_validate_discriminated_union` function and that whenever a `BaseModel` or dataclass is detected then we should use the discriminator key and not the alias to access the discriminator value.
```py
from typing import Literal, Union
from pydantic import BaseModel, Field
class A(BaseModel):
literal: Literal['a'] = Field(alias='lit')
class B(BaseModel):
literal: Literal['b'] = Field(alias='lit')
class Config:
allow_population_by_field_name = True
class Top(BaseModel):
sub: Union[A, B] = Field(..., discriminator='literal')
Top(sub=A(lit='a'))
# pydantic.error_wrappers.ValidationError: 1 validation error for Top
# sub
# Discriminator 'literal' is missing in value (type=value_error.discriminated_union.missing_discriminator; discriminator_key=literal)
Top(sub=B(lit='b'))
# pydantic.error_wrappers.ValidationError: 1 validation error for Top
# sub
# Discriminator 'literal' is missing in value (type=value_error.discriminated_union.missing_discriminator; discriminator_key=literal)
Top(sub=B(literal='b'))
# pydantic.error_wrappers.ValidationError: 1 validation error for Top
# sub
# Discriminator 'literal' is missing in value (type=value_error.discriminated_union.missing_discriminator; discriminator_key=literal)
```
author:
url:https://github.com/chornsby
type:Person
name:chornsby
datePublished:2022-02-22T09:56:44.000Z
interactionStatistic:
type:InteractionCounter
interactionType:https://schema.org/CommentAction
userInteractionCount:0
url:https://github.com/3846/pydantic/issues/3846
Person:
url:https://github.com/chornsby
name:chornsby
url:https://github.com/chornsby
name:chornsby
InteractionCounter:
interactionType:https://schema.org/CommentAction
userInteractionCount:0
interactionType:https://schema.org/CommentAction
userInteractionCount:0
External Links {π}(3)
Analytics and Tracking {π}
- Site Verification - Google
Libraries {π}
- Clipboard.js
- D3.js
- Lodash
Emails and Hosting {βοΈ}
Mail Servers:
- aspmx.l.google.com
- alt1.aspmx.l.google.com
- alt2.aspmx.l.google.com
- alt3.aspmx.l.google.com
- alt4.aspmx.l.google.com
Name Servers:
- dns1.p08.nsone.net
- dns2.p08.nsone.net
- dns3.p08.nsone.net
- dns4.p08.nsone.net
- ns-1283.awsdns-32.org
- ns-1707.awsdns-21.co.uk
- ns-421.awsdns-52.com
- ns-520.awsdns-01.net