Skip to content

Commit

Permalink
bump-pydantic results, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shouples committed Oct 26, 2023
1 parent 16c18bf commit c12e470
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/dx/formatters/enhanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
from pydantic import BaseSettings, Field
from pydantic import Field

from dx.formatters.main import DEFAULT_IPYTHON_DISPLAY_FORMATTER, DXDisplayFormatter
from dx.settings import get_settings
from pydantic_settings import BaseSettings, SettingsConfigDict

settings = get_settings()

Expand All @@ -15,17 +16,16 @@ class DXSettings(BaseSettings):
DX_DISPLAY_MAX_ROWS: int = 50_000
DX_DISPLAY_MAX_COLUMNS: int = 50
DX_MAX_STRING_LENGTH: int = 250
DX_HTML_TABLE_SCHEMA: bool = Field(True, allow_mutation=False)
DX_MEDIA_TYPE: str = Field("application/vnd.dex.v1+json", allow_mutation=False)
DX_HTML_TABLE_SCHEMA: bool = Field(True, frozen=True)
DX_MEDIA_TYPE: str = Field("application/vnd.dex.v1+json", frozen=True)

DX_FLATTEN_INDEX_VALUES: bool = False
DX_FLATTEN_COLUMN_VALUES: bool = True
DX_STRINGIFY_INDEX_VALUES: bool = False
DX_STRINGIFY_COLUMN_VALUES: bool = True

class Config:
validate_assignment = True # we need this to enforce `allow_mutation`
json_encoders = {type: lambda t: str(t)}
# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = SettingsConfigDict(validate_assignment=True, json_encoders={type: lambda t: str(t)})


@lru_cache
Expand Down
14 changes: 7 additions & 7 deletions src/dx/formatters/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import structlog
from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
from pydantic import BaseSettings, Field
from pydantic import Field

from dx.formatters.main import DEFAULT_IPYTHON_DISPLAY_FORMATTER
from dx.settings import get_settings
from pydantic_settings import BaseSettings, SettingsConfigDict

logger = structlog.get_logger(__name__)
settings = get_settings()
Expand All @@ -22,12 +23,11 @@ class PandasSettings(BaseSettings):
PANDAS_DISPLAY_MAX_ROWS: int = 60
PANDAS_DISPLAY_MAX_COLUMNS: int = 20
PANDAS_MAX_STRING_LENGTH: int = 50
PANDAS_HTML_TABLE_SCHEMA: bool = Field(False, allow_mutation=False)
PANDAS_MEDIA_TYPE: str = Field("application/vnd.dataresource+json", allow_mutation=False)

class Config:
validate_assignment = True # we need this to enforce `allow_mutation`
json_encoders = {type: lambda t: str(t)}
PANDAS_HTML_TABLE_SCHEMA: bool = Field(False, frozen=True)
PANDAS_MEDIA_TYPE: str = Field("application/vnd.dataresource+json", frozen=True)
# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = SettingsConfigDict(validate_assignment=True, json_encoders={type: lambda t: str(t)})


@lru_cache
Expand Down
14 changes: 7 additions & 7 deletions src/dx/formatters/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
from pydantic import BaseSettings, Field
from pydantic import Field

from dx.formatters.main import DEFAULT_IPYTHON_DISPLAY_FORMATTER, DXDisplayFormatter
from dx.settings import get_settings
from pydantic_settings import BaseSettings, SettingsConfigDict

settings = get_settings()

Expand All @@ -16,17 +17,16 @@ class DataResourceSettings(BaseSettings):
DATARESOURCE_DISPLAY_MAX_ROWS: int = 50_000
DATARESOURCE_DISPLAY_MAX_COLUMNS: int = 50
DATARESOURCE_MAX_STRING_LENGTH: int = 250
DATARESOURCE_HTML_TABLE_SCHEMA: bool = Field(True, allow_mutation=False)
DATARESOURCE_MEDIA_TYPE: str = Field("application/vnd.dataresource+json", allow_mutation=False)
DATARESOURCE_HTML_TABLE_SCHEMA: bool = Field(True, frozen=True)
DATARESOURCE_MEDIA_TYPE: str = Field("application/vnd.dataresource+json", frozen=True)

DATARESOURCE_FLATTEN_INDEX_VALUES: bool = False
DATARESOURCE_FLATTEN_COLUMN_VALUES: bool = True
DATARESOURCE_STRINGIFY_INDEX_VALUES: bool = False
DATARESOURCE_STRINGIFY_COLUMN_VALUES: bool = True

class Config:
validate_assignment = True # we need this to enforce `allow_mutation`
json_encoders = {type: lambda t: str(t)}
# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = SettingsConfigDict(validate_assignment=True, json_encoders={type: lambda t: str(t)})


@lru_cache
Expand Down
18 changes: 13 additions & 5 deletions src/dx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
from pandas import set_option as pandas_set_option
from pydantic import BaseSettings, validator
from pydantic import validator

from dx.dependencies import get_default_renderable_types
from dx.types.main import DXDisplayMode, DXSamplingMethod
from pydantic_settings import BaseSettings, SettingsConfigDict

MB = 1024 * 1024

Expand Down Expand Up @@ -77,6 +78,8 @@ class Settings(BaseSettings):
GENERATE_DEX_METADATA: bool = False
ALLOW_NOTEABLE_ATTRS: bool = True

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("RENDERABLE_TYPES", pre=True, always=True)
def validate_renderables(cls, vals):
"""Allow passing comma-separated strings or actual types."""
Expand All @@ -98,6 +101,8 @@ def validate_renderables(cls, vals):

return valid_vals

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("DISPLAY_MAX_COLUMNS", pre=True, always=True)
def validate_display_max_columns(cls, val):
if val < 0:
Expand All @@ -107,18 +112,24 @@ def validate_display_max_columns(cls, val):
pd.set_option("display.max_columns", val)
return val

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("DISPLAY_MAX_ROWS", pre=True, always=True)
def validate_display_max_rows(cls, val):
if val < 0:
raise ValueError("DISPLAY_MAX_ROWS must be >= 0")
pd.set_option("display.max_rows", val)
return val

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("HTML_TABLE_SCHEMA", pre=True, always=True)
def validate_html_table_schema(cls, val):
pd.set_option("html.table_schema", val)
return val

# TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
@validator("MAX_STRING_LENGTH", pre=True, always=True)
def validate_max_string_length(cls, val):
if val < 0:
Expand All @@ -131,10 +142,7 @@ def get_renderable_types(self):
**self.RENDERABLE_TYPES,
**get_default_renderable_types(),
}

class Config:
validate_assignment = True
use_enum_values = True
model_config = SettingsConfigDict(validate_assignment=True, use_enum_values=True)


@lru_cache
Expand Down

0 comments on commit c12e470

Please sign in to comment.