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

[3/n][pythonic config] Layer in Pydantic compat abstractions #16436

Merged
merged 3 commits into from
Oct 11, 2023

Conversation

benpankow
Copy link
Member

@benpankow benpankow commented Sep 12, 2023

Summary

PR stack which re-implements #16257, in hopes of making it easier to review.

This PR builds out a Pydantic 2 compat layer, which abstracts away some of the model accesses. The next PR in the stack will actually enable Pydantic 2 tests + turn on compatibility.

Test Plan

Existing unit tests.

@github-actions
Copy link

github-actions bot commented Sep 12, 2023

Deploy preview for dagster-docs ready!

Preview available at https://dagster-docs-1mvswt01j-elementl.vercel.app
https://benpankow-restructure-pythonic-config-module-3.dagster.dagster-docs.io

Direct link to changed pages:

@benpankow benpankow changed the title [3/n][pythonic config] Layer in Pydantic compat abstraction layer" [3/n][pythonic config] Layer in Pydantic compat abstractions Sep 12, 2023
Comment on lines 100 to 104
if "Instance is frozen" in str( # Pydantic 2.x error
e
) or "is immutable and does not support item assignment" in str( # Pydantic 1.x error
e
):
Copy link
Member

Choose a reason for hiding this comment

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

is there any less fragile way of doing this?

Copy link
Member

Choose a reason for hiding this comment

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

Still relevant comment

Copy link
Member Author

@benpankow benpankow Oct 1, 2023

Choose a reason for hiding this comment

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

Sadly no, not that I can tell. These values aren't set or exported as constants anywhere, just passed directly to the error constructor.

I've pulled this out into a separate fn for the moment. Our tests should at least catch if this changes.

Comment on lines +147 to +158
def ensure_env_vars_set_post_init(set_value: T, input_value: Any) -> T:
"""Pydantic 2.x utility. Ensures that Pydantic field values are set to the appropriate
EnvVar or IntEnvVar objects post-model-instantiation, since Pydantic 2.x will cast
EnvVar or IntEnvVar values to raw strings or ints as part of the model instantiation process.
"""
if isinstance(set_value, dict) and isinstance(input_value, dict):
Copy link
Member

Choose a reason for hiding this comment

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

do we have any context as to why Pydantic 2 does this? Are we going to break anything by doing this?

invalid_type=e.current_value,
is_resource=model_cls is not None
and safe_is_subclass(model_cls, ConfigurableResourceFactory),
)

shape_cls = Permissive if model_cls.__config__.extra == Extra.allow else Shape
shape_cls = Permissive if model_config(model_cls).get("extra") == "allow" else Shape
Copy link
Member

Choose a reason for hiding this comment

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

is there a first-class way of doing this in pydantic 2?

Copy link
Member Author

Choose a reason for hiding this comment

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

It can be extracted directly (outside of the compat layer), but still relies on a .get call (it's an unstructured dictionary):

>>> MyModel2(a_str='foo').model_config
{'extra': 'allow'}

Comment on lines 52 to 75
# Pydantic 1.x
field_info = getattr(self.field, "field_info", None)
if field_info:
return field_info.description

# Pydantic 2.x
return getattr(self.field, "description", None)

def is_required(self):
# Pydantic 2.x
if hasattr(self.field, "is_required"):
return self.field.is_required()

# Pydantic 1.x
return self.field.required

@property
def discriminator(self):
# Pydantic 2.x
if hasattr(self.field, "discriminator"):
return self.field.discriminator

# Pydantic 1.x
return getattr(self.field, "discriminator_key", None)
Copy link
Member

Choose a reason for hiding this comment

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

should we just explicitly have different codepaths for each version here? I think that will be more bulletproof

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's a better approach I think. Reworked.

from pydantic import BaseModel

from .attach_other_object_to_context import (
IAttachDifferentObjectToOpContext as IAttachDifferentObjectToOpContext,
Copy link
Member

Choose a reason for hiding this comment

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

sidenote: should we just kill this feature?

Copy link
Member Author

Choose a reason for hiding this comment

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

We still use it in a few places, unfortunately. I think it would be a bit of work to strip it out, but can look into it once this lands.

@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-2 branch from 203badb to 6c0fe9b Compare September 14, 2023 23:21
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-3 branch from 49b59df to 73e9411 Compare September 14, 2023 23:21
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-2 branch 3 times, most recently from b6e711d to 039c24b Compare September 28, 2023 03:33
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-3 branch from 73e9411 to 8924ae9 Compare September 28, 2023 05:07
Comment on lines 100 to 104
if "Instance is frozen" in str( # Pydantic 2.x error
e
) or "is immutable and does not support item assignment" in str( # Pydantic 1.x error
e
):
Copy link
Member

Choose a reason for hiding this comment

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

Still relevant comment

@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-2 branch from 039c24b to a627a90 Compare October 1, 2023 22:03
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-3 branch from 8924ae9 to 4d29663 Compare October 1, 2023 22:39
@benpankow benpankow requested a review from schrockn October 1, 2023 23:40
Copy link
Member

@schrockn schrockn left a comment

Choose a reason for hiding this comment

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

make-it-so-picard

@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-2 branch from e9127ce to 2c5ee94 Compare October 10, 2023 16:51
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-3 branch 4 times, most recently from 35f9b86 to 4c102b0 Compare October 10, 2023 21:02
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-2 branch from 28fc043 to a6aba5f Compare October 11, 2023 22:45
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-3 branch from 4c102b0 to afebf53 Compare October 11, 2023 22:45
@benpankow
Copy link
Member Author

benpankow commented Oct 11, 2023

Merge activity

  • Oct 11, 4:24 PM: @benpankow started a stack merge that includes this pull request via Graphite.
  • Oct 11, 4:24 PM: Graphite rebased this pull request as part of a merge.
  • Oct 11, 4:25 PM: @benpankow merged this pull request with Graphite.

Base automatically changed from benpankow/restructure-pythonic-config-module-2 to master October 11, 2023 23:24
@benpankow benpankow force-pushed the benpankow/restructure-pythonic-config-module-3 branch from afebf53 to b7beca2 Compare October 11, 2023 23:24
@benpankow benpankow merged commit f46a70a into master Oct 11, 2023
@benpankow benpankow deleted the benpankow/restructure-pythonic-config-module-3 branch October 11, 2023 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants