pydantic / bump-pydantic

Convert Pydantic from V1 to V2 ♻
MIT License
303 stars 24 forks source link

Examples in codemod files do not all work #164

Closed NodeJSmith closed 1 month ago

NodeJSmith commented 4 months ago

Not sure if this is an actual issue. I cloned this repo to use it to help me design a similar tool for an internal library at my company. I'm trying to work through what it's doing but a lot of the examples under if __name__ == "__main__": do not run.

For example I had to change the example in add_default_none to the below to get it to work. Let me know if you would appreciate a PR, I'm happy to contribute if this is something that is desired.

    with TemporaryDirectory(dir=os.getcwd()) as tmpdir:
        package_dir = f"{tmpdir}/package"
        os.mkdir(package_dir)
        module_path = f"{package_dir}/a.py"
        with open(module_path, "w") as f:
            content = textwrap.dedent(
                """
                from pydantic import BaseModel

                class Foo(BaseModel):
                    a: Optional[str]

                class Bar(Foo):
                    b: Optional[str]
                    c: Union[str, None]
                    d: Any

                foo = Foo(a="text")
                foo.dict()
            """
            )
            f.write(content)
        module = str(Path(module_path).relative_to(tmpdir))
        mrg = FullRepoManager(tmpdir, {module_path}, providers={FullyQualifiedNameProvider})
        wrapper = mrg.get_metadata_wrapper_for_path(module_path)
        context = CodemodContext(wrapper=wrapper)
        instance = ClassDefVisitor(context=context)
        wrapper.visit(instance)

        command = AddDefaultNoneCommand(context=context)  # type: ignore[assignment]
        mod = wrapper.visit(command)
        print(mod.code)
Kludex commented 1 month ago

Oh, I see... Yeah... I may have forgotten them...

I'll close this for housekeeping, but all PRs to fix those are welcome. :)

Thanks!