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

Adds experimental_field decorator #8066

Merged
merged 3 commits into from
Jan 6, 2023
Merged

Adds experimental_field decorator #8066

merged 3 commits into from
Jan 6, 2023

Conversation

desertaxle
Copy link
Member

Add an experimental_field decorator for use with Pydantic models. The decorator allows the user to define a field to warn on and will raise a warning if the correct conditions are met when the model is instantiated. I also wanted to allow the warning to raise whenever the specified value is set, but I didn't find a way to get that working.

Example

from pydantic import BaseModel
from prefect._internal.compatibility.experimental import experimental_field

@experimental_field(
    "value",
    group="test",
    help="This is just a test, don't worry.",
)
class Foo(BaseModel):
    value: int

Foo(value=1) # raises experimental warning

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • This pull request includes tests or only affects documentation.
  • This pull request includes a label categorizing the change e.g. fix, feature, enhancement

@desertaxle desertaxle added the development Tech debt, refactors, CI, tests, and other related work. label Jan 5, 2023
@netlify
Copy link

netlify bot commented Jan 5, 2023

Deploy Preview for prefect-orion ready!

Name Link
🔨 Latest commit 4a4cbb9
🔍 Latest deploy log https://app.netlify.com/sites/prefect-orion/deploys/63b87b7d27c66600089a705b
😎 Deploy Preview https://deploy-preview-8066--prefect-orion.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@desertaxle desertaxle mentioned this pull request Jan 5, 2023
3 tasks
Comment on lines +290 to +296
@experimental_field(
"value",
group="test",
help="This is just a test, don't worry.",
)
class Foo(BaseModel):
value: int = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be implemented as this instead?

Suggested change
@experimental_field(
"value",
group="test",
help="This is just a test, don't worry.",
)
class Foo(BaseModel):
value: int = 1
class Foo(BaseModel):
value: int = ExperimentalField(default=1, group="test", help="...")

It'd then add a validator to the field or something that raises the warning? It seems clearer perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or perhaps

Suggested change
@experimental_field(
"value",
group="test",
help="This is just a test, don't worry.",
)
class Foo(BaseModel):
value: int = 1
class Foo(BaseModel):
value: int = ExperimentalField(Field(default=1, ...), group="test", help="...")

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried that initially, but I couldn't find a way to add validator by wrapping Field. The validators for fields seem to be place on the class that is used as the type hint. Do you have an example of extending Field?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah interesting. Let's stick with this for now then.

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 looks like dynamically adding validators to an existing model isn't supported: pydantic/pydantic#2076

@peytonrunyan peytonrunyan self-requested a review January 6, 2023 20:25
Copy link
Contributor

@peytonrunyan peytonrunyan left a comment

Choose a reason for hiding this comment

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

lgtm!

@desertaxle desertaxle merged commit 136aecc into main Jan 6, 2023
@desertaxle desertaxle deleted the experimental-field branch January 6, 2023 20:26
github-actions bot pushed a commit that referenced this pull request Jan 6, 2023
github-actions bot pushed a commit that referenced this pull request Jan 6, 2023
ddelange added a commit to ddelange/prefect that referenced this pull request Jan 9, 2023
…r-builds-release

* 'main' of https://github.com/ddelange/prefect: (131 commits)
  Fix docker-builds.yaml templating syntax (PrefectHQ#8114)
  Rename Worker pools -> work pools (PrefectHQ#8107)
  Build multi-arch images for commits on main (PrefectHQ#7900)
  [Issue PrefectHQ#456] Change example of `infra_override` in docs/concepts/deployment (PrefectHQ#8101)
  Bump @playwright/test from 1.29.1 to 1.29.2 in /orion-ui (PrefectHQ#8105)
  Bump @prefecthq/orion-design from 1.1.53 to 1.1.54 in /orion-ui (PrefectHQ#8104)
  Update deployment docs to include tag and idempotency key (PrefectHQ#7771)
  Add `BaseWorker` and `ProcessWorker` (PrefectHQ#7996)
  Add Peyton and Serina as global code owners (PrefectHQ#8098)
  Add release notes for 2.7.7 (PrefectHQ#8091)
  Add youtube badge (PrefectHQ#8089)
  Adds `MAX_RRULE_LENGTH` (PrefectHQ#7762)
  Limit task run cache key size (PrefectHQ#7275)
  Add --match flag to work queues documentation (PrefectHQ#7768)
  Modify disable ssl setting tests to allow any for headers and timeout (PrefectHQ#8086)
  Add test for allow_failure and quote (PrefectHQ#8055)
  Adds `experimental_field` decorator (PrefectHQ#8066)
  add docs on migrating block documents (PrefectHQ#8085)
  Add Redoc documentation for REST API reference (PrefectHQ#7503)
  Allow disabling SSL verification (PrefectHQ#7850)
  ...
ddelange added a commit to ddelange/prefect that referenced this pull request Jan 9, 2023
…refect into docker-builds-consolidation

* 'docker-builds-release' of https://github.com/ddelange/prefect: (131 commits)
  Fix docker-builds.yaml templating syntax (PrefectHQ#8114)
  Rename Worker pools -> work pools (PrefectHQ#8107)
  Build multi-arch images for commits on main (PrefectHQ#7900)
  [Issue PrefectHQ#456] Change example of `infra_override` in docs/concepts/deployment (PrefectHQ#8101)
  Bump @playwright/test from 1.29.1 to 1.29.2 in /orion-ui (PrefectHQ#8105)
  Bump @prefecthq/orion-design from 1.1.53 to 1.1.54 in /orion-ui (PrefectHQ#8104)
  Update deployment docs to include tag and idempotency key (PrefectHQ#7771)
  Add `BaseWorker` and `ProcessWorker` (PrefectHQ#7996)
  Add Peyton and Serina as global code owners (PrefectHQ#8098)
  Add release notes for 2.7.7 (PrefectHQ#8091)
  Add youtube badge (PrefectHQ#8089)
  Adds `MAX_RRULE_LENGTH` (PrefectHQ#7762)
  Limit task run cache key size (PrefectHQ#7275)
  Add --match flag to work queues documentation (PrefectHQ#7768)
  Modify disable ssl setting tests to allow any for headers and timeout (PrefectHQ#8086)
  Add test for allow_failure and quote (PrefectHQ#8055)
  Adds `experimental_field` decorator (PrefectHQ#8066)
  add docs on migrating block documents (PrefectHQ#8085)
  Add Redoc documentation for REST API reference (PrefectHQ#7503)
  Allow disabling SSL verification (PrefectHQ#7850)
  ...
masonmenges pushed a commit that referenced this pull request Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Tech debt, refactors, CI, tests, and other related work.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants