Skip to content
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

update_forward_refs modifies __dict__ of a module #1228

Closed
Paul-Ilyin opened this issue Feb 13, 2020 · 1 comment · Fixed by #1244
Closed

update_forward_refs modifies __dict__ of a module #1228

Paul-Ilyin opened this issue Feb 13, 2020 · 1 comment · Fixed by #1244
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@Paul-Ilyin
Copy link
Contributor

Bug

OS: macOS Catalina 10.15.3  
python: 3.7.3
pydantic version: 1.1

After creating a BaseModel subclass and calling update_forward_ref() classmethod, pydantic can modify dict of a module in which the model was created if the model was declared inside some function of this module.

from typing import Optional, ForwardRef
from pydantic import BaseModel

def first_func():
    class Mod(BaseModel):
        field: Optional[ForwardRef("Mod")]
    Mod.update_forward_refs()
    return Mod

def second_func():
    class Mod(BaseModel):
        another_field: Optional[ForwardRef("Mod")]

    Mod.update_forward_refs()
    
    return Mod

first_model = first_func()
second_model = second_func()

print(second_model.__fields__["another_field"].type_)
# Output: <class '__main__.first_func.<locals>.Mod'>
# Expected: <class '__main__.second_func.<locals>.Mod'>

It happens because of these two lines: https://github.com/samuelcolvin/pydantic/blob/645e5fe6a0c74c95c24410571e9bb804af3eb677/pydantic/main.py#L632-L633
They can be fixed that way:

        globalns = sys.modules[cls.__module__].__dict__.copy()
        globalns.setdefault(cls.__name__, cls)

This bug can lead to errors when two different models with the same names are defined in two different test cases or some other errors which can be hard to debug.

@Paul-Ilyin Paul-Ilyin added the bug V1 Bug related to Pydantic V1.X label Feb 13, 2020
@samuelcolvin
Copy link
Member

thanks for reporting, PR welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants