8000 Fix forward ref in generic by adriangb · Pull Request #6511 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@adriangb
Copy link
Member
@adriangb adriangb commented Jul 7, 2023

Fixes #6503

skip change file check

Selected Reviewer: @samuelcolvin

@adriangb
Copy link
Member Author
adriangb commented Jul 7, 2023

please review

@cloudflare-workers-and-pages
Copy link
cloudflare-workers-and-pages bot commented Jul 7, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: b885eac
Status: ✅  Deploy successful!
Preview URL: https://c930025c.pydantic-docs2.pages.dev
Branch Preview URL: https://fix-forward-ref-in-generic.pydantic-docs2.pages.dev

View logs

Copy link
Contributor
@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just see the suggestions about the tests

Foo = module.Foo
Bar = module.Bar

assert Foo(x={Bar: Bar}).x[Bar] is Bar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert Foo(x={Bar: Bar}).x[Bar] is Bar
assert Foo(x={Bar: Bar}).x[Bar] is Bar
def test_forward_ref_in_generic_separate_modules(create_module: Any) -> None:
"""https://github.com/pydantic/pydantic/issues/6503"""
@create_module
def module_1():
import typing as tp
from pydantic import BaseModel
class Foo(BaseModel):
x: tp.Dict['tp.Type[Bar]', tp.Type['Bar']]
@create_module
def module_2():
from pydantic import BaseModel
class Bar(BaseModel):
pass
Foo = module_1.Foo
Bar = module_2.Bar
Foo.model_rebuild(_types_namespace={'tp': typing, 'Bar': Bar})
assert Foo(x={Bar: Bar}).x[Bar] is Bar

(Also need to add import typing at the top of the file.)

This test makes sure it also works when the types aren't in the same module. I think it's worth adding but defer to you.

adriangb and others added 2 commits July 7, 2023 10:19
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
@adriangb adriangb enabled auto-merge (squash) July 7, 2023 15:23
@adriangb adriangb merged commit 1a366d6 into main Jul 7, 2023
@adriangb adriangb deleted the fix-forward-ref-in-generic branch July 7, 2023 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forward reference in typing.Type["..."] errors with TypeError: 'ForwardRef' object cannot be converted to 'PyType'

4 participants

0