New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class properties have a potential for namespace collision #9385
Comments
I didn't have a field where it makes sense to provide additional context after the example_code portion, so I have opted to do so here. In trying to resolve this, I did a little digging and have a few more things to report. Dismantling the Forward Reference I thought to try inverting the order of my classes to avoid a forward reference thinking that this would solve the issue. It did not. This code produces the same error as was submitted: from pydantic import BaseModel
from typing import List, Optional
class StatelessRule(BaseModel):
pass
class StatelessRulesAndCustomActions(BaseModel):
StatelessRules: Optional[List[StatelessRule]] = None
class RulesSource(BaseModel):
StatelessRulesAndCustomActions: Optional["StatelessRulesAndCustomActions"] = None
def test_the_bug():
# Create instances of the classes to trigger type checking
rules_source = RulesSource(StatelessRulesAndCustomActions=StatelessRulesAndCustomActions(StatelessRules=[StatelessRule()])) Check if Name Collisions Are Allowed from pydantic import BaseModel
from typing import Dict, List
class CustomActionDefinition(BaseModel):
PublishMetricAction: Dict[str, List[Dict[str, str]]]
class CustomAction(BaseModel):
ActionName: str
CustomActionDefinition: CustomActionDefinition
def test_property_name_collision_allowed():
custom_action = CustomAction(
ActionName="foo",
CustomActionDefinition={"PublishMetricAction": {"bar": [{"baz": "buzz"}]}}
) Check if the problem is really a name collision from pydantic import BaseModel
from typing import Dict, List, Optional
class StatelessRule(BaseModel):
pass
class StatelessRulesAndCustomActionsModel(BaseModel):
StatelessRules: Optional[List[StatelessRule]] = None
class RulesSource(BaseModel):
StatelessRulesAndCustomActions: Optional["StatelessRulesAndCustomActionsModel"] = None
def test_the_bug():
# Create instances of the classes to trigger type checking
rules_source = RulesSource(StatelessRulesAndCustomActions=StatelessRulesAndCustomActionsModel(StatelessRules=[StatelessRule()])) |
This seems to be the same issue as: #7327, #8240, #7309 (see #6646 (comment) for an explanation). See also #9093 (comment), could be the same issue you are facing |
Closing as a duplicate, based on the issues that @Viicos mentioned above. Thanks! |
I read through the source material you provided and I do agree that this is a duplicate; however, I'm having a hard time reconciling why having the Do you have any insight into why the name collision was perfectly fine without |
Not sure, might need to double check, but I think this comes from what I described here: #8243 (comment). I'll probably need to take a better look at your code example, but could you confirm this matches your use case? |
Since On this same train, now that |
Initial Checks
Description
When you are using a forward reference (as a string literal to defer validation to Runtime), and:
Optional[] = None
for the definition of that propertyThen pydantic can have a bit of a fit when it attempts to create an instance of the class.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: