-
Notifications
You must be signed in to change notification settings - Fork 6
[Fix] Add dataclass fallback for ResourceDictValidation when pydantic is missing #939
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
Changes from all commits
8f5b4f5
6ee7928
b731087
45872cf
fd0366f
37d9b71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| import warnings | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel, Extra | ||
| try: | ||
| from pydantic import BaseModel, Extra | ||
|
|
||
| HAS_PYDANTIC = True | ||
| except ImportError: | ||
| from dataclasses import dataclass | ||
|
|
||
| BaseModel = object | ||
| Extra = None | ||
| HAS_PYDANTIC = False | ||
|
|
||
|
|
||
| class ResourceDictValidation(BaseModel): | ||
|
|
@@ -17,16 +26,30 @@ class ResourceDictValidation(BaseModel): | |
| priority: Optional[int] = None | ||
| slurm_cmd_args: Optional[list[str]] = None | ||
|
|
||
| class Config: | ||
| extra = Extra.forbid | ||
| if HAS_PYDANTIC: | ||
|
|
||
| class Config: | ||
| extra = Extra.forbid | ||
|
|
||
|
|
||
| if not HAS_PYDANTIC: | ||
| ResourceDictValidation = dataclass(ResourceDictValidation) # type: ignore | ||
|
|
||
|
|
||
| def _get_accepted_keys(class_type) -> list[str]: | ||
| if hasattr(class_type, "model_fields"): | ||
| return list(class_type.model_fields.keys()) | ||
| elif hasattr(class_type, "__dataclass_fields__"): | ||
| return list(class_type.__dataclass_fields__.keys()) | ||
| raise TypeError("Unsupported class type for validation") | ||
|
|
||
|
|
||
| def validate_resource_dict(resource_dict: dict) -> None: | ||
| _ = ResourceDictValidation(**resource_dict) | ||
|
Comment on lines
47
to
48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check if the no-op validate_resource_dict still exists in task_scheduler/base.py
echo "=== task_scheduler/base.py validate_resource_dict ==="
rg -n 'def validate_resource_dict' --type=py -C 3
echo ""
echo "=== Imports of validate_resource_dict from task_scheduler ==="
rg -n 'from.*task_scheduler.*import.*validate_resource_dict' --type=py
echo ""
echo "=== All imports from task_scheduler.base ==="
rg -n 'from.*task_scheduler\.base.*import' --type=py
echo ""
echo "=== All imports from standalone.validate ==="
rg -n 'from.*standalone\.validate.*import' --type=py
echo ""
echo "=== Check task_scheduler/base.py content around line 16-17 ==="
sed -n '1,30p' src/executorlib/task_scheduler/base.py 2>/dev/null || echo "File not found at expected location"Repository: pyiron/executorlib Length of output: 4644 Remove the no-op The no-op implementation persists and creates a shadowing problem. Five modules— 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| def validate_resource_dict_with_optional_keys(resource_dict: dict) -> None: | ||
| accepted_keys = ResourceDictValidation.model_fields.keys() | ||
| accepted_keys = _get_accepted_keys(class_type=ResourceDictValidation) | ||
| optional_lst = [key for key in resource_dict if key not in accepted_keys] | ||
| validate_dict = { | ||
| key: value for key, value in resource_dict.items() if key in accepted_keys | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
🌐 Web query:
pydantic v2 ConfigDict replace Extra Config class💡 Result:
In Pydantic v2, the old inner
class Config:(andExtra) is replaced bymodel_config = ConfigDict(...)(theConfigclass is still supported but deprecated). (docs.pydantic.dev)v1 → v2 mapping for
extrav1
v2 (recommended)
Pydantic v2 supports exactly these values:
'ignore'(default),'allow','forbid'. (docs.pydantic.dev)Alternative v2 syntax (class kwargs)
(docs.pydantic.dev)
If you used
extra='allow'and need the extrasIn v2, allowed extras are stored in
__pydantic_extra__(and may also be accessible as attributes). (docs.pydantic.dev)Citations:
🏁 Script executed:
Repository: pyiron/executorlib
Length of output: 1465
Use Pydantic V2's
ConfigDictinstead of deprecatedExtraand innerConfigclassThe code imports Pydantic V1 APIs (
Extraand innerclass Config) which are deprecated in Pydantic V2. With pydantic 2.12.4, these patterns emitPydanticDeprecatedSince20warnings on every module load. Since_get_accepted_keys()already uses the V2model_fieldsattribute, the codebase is V2-aware but the configuration pattern hasn't been modernized.Replace with Pydantic V2's native
ConfigDict:♻️ Refactor to V2 API
Also applies to: lines 29–32.
🤖 Prompt for AI Agents