-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary & Motivation Introduces a `StrictModel` class, which is a subclass of `pydantic.BaseModel` that's frozen and doesn't allow default constructor arguments that aren't class members. The idea is for this to eventually replace all our uses of `NamedTuple`, `BaseModel`, and `@dataclass`. This is a replacement for the unmerged #20461. Downstream PRs that demonstrate its usage: - #20641 - #20643 - #20638 ## How I Tested These Changes
- Loading branch information
1 parent
4189315
commit 276804c
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Any | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class DagsterModel(BaseModel): | ||
"""Standardizes on Pydantic settings that are stricter than the default. | ||
- Frozen, to avoid complexity caused by mutation. | ||
- extra=forbid, to avoid bugs caused by accidentally constructing with the wrong arguments. | ||
""" | ||
|
||
def __init__(self, **data: Any) -> None: | ||
super().__init__(**data) | ||
|
||
class Config: | ||
extra = "forbid" | ||
frozen = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
python_modules/dagster/dagster_tests/model_tests/test_dagster_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest | ||
from dagster._model import DagsterModel | ||
from pydantic import ValidationError | ||
|
||
|
||
def test_override_constructor_in_subclass(): | ||
class MyClass(DagsterModel): | ||
foo: str | ||
bar: int | ||
|
||
def __init__(self, foo: str, bar: int): | ||
super().__init__(foo=foo, bar=bar) | ||
|
||
MyClass(foo="fdsjk", bar=4) | ||
|
||
|
||
def test_override_constructor_in_subclass_different_arg_names(): | ||
class MyClass(DagsterModel): | ||
foo: str | ||
bar: int | ||
|
||
def __init__(self, fooarg: str, bararg: int): | ||
super().__init__(foo=fooarg, bar=bararg) | ||
|
||
MyClass(fooarg="fdsjk", bararg=4) | ||
|
||
|
||
def test_override_constructor_in_subclass_wrong_type(): | ||
class MyClass(DagsterModel): | ||
foo: str | ||
bar: int | ||
|
||
def __init__(self, foo: str, bar: str): | ||
super().__init__(foo=foo, bar=bar) | ||
|
||
with pytest.raises(ValidationError): | ||
MyClass(foo="fdsjk", bar="fdslk") |