-
Notifications
You must be signed in to change notification settings - Fork 0
Fix recursion errors in Ascleandict and validate dataclass exports #16
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
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 |
|---|---|---|
|
|
@@ -213,36 +213,54 @@ def _keep(v) -> bool: | |
| return False | ||
| return True | ||
|
|
||
| def _convert(obj: object) -> Any: | ||
| if is_dataclass(obj) and not isinstance(obj, type): | ||
| result = {} | ||
| for f in fields(obj): | ||
| converted = _convert(getattr(obj, f.name)) | ||
| if _keep(converted): | ||
| result[f.name] = converted | ||
| return result | ||
| if isinstance(obj, dict): | ||
| result = {} | ||
| for k, v in obj.items(): | ||
| converted = _convert(v) | ||
| if _keep(converted): | ||
| key = ( | ||
| str(k) | ||
| if json_serializable and not isinstance(k, str) | ||
| else k | ||
| ) | ||
| result[key] = converted | ||
| return result | ||
| if isinstance(obj, (list, tuple)): | ||
| items = [_convert(item) for item in obj] | ||
| items = [item for item in items if _keep(item)] | ||
| return tuple(items) if isinstance(obj, tuple) else items | ||
| if json_serializable: | ||
| try: | ||
| json.dumps(obj) | ||
| except (TypeError, OverflowError): | ||
| return str(obj) | ||
| return obj | ||
| def _convert(obj: object, _seen: set | None = None) -> Any: | ||
| is_container = isinstance(obj, (dict, list)) or ( | ||
| is_dataclass(obj) and not isinstance(obj, type) | ||
| ) | ||
| if is_container: | ||
| if _seen is None: | ||
| _seen = set() | ||
| obj_id = id(obj) | ||
| if obj_id in _seen: | ||
| logging.getLogger(__name__).warning( | ||
| "ascleandict: circular reference detected in %s, skipping", | ||
| type(obj).__name__, | ||
| ) | ||
| return f"<circular ref: {type(obj).__name__}>" | ||
|
Comment on lines
+225
to
+229
|
||
| _seen.add(obj_id) | ||
| try: | ||
| if is_dataclass(obj) and not isinstance(obj, type): | ||
| result = {} | ||
| for f in fields(obj): | ||
| converted = _convert(getattr(obj, f.name), _seen) | ||
| if _keep(converted): | ||
| result[f.name] = converted | ||
| return result | ||
| if isinstance(obj, dict): | ||
| result = {} | ||
| for k, v in obj.items(): | ||
| converted = _convert(v, _seen) | ||
| if _keep(converted): | ||
| key = ( | ||
| str(k) | ||
| if json_serializable and not isinstance(k, str) | ||
| else k | ||
| ) | ||
| result[key] = converted | ||
| return result | ||
| if isinstance(obj, (list, tuple)): | ||
| items = [_convert(item, _seen) for item in obj] | ||
| items = [item for item in items if _keep(item)] | ||
| return tuple(items) if isinstance(obj, tuple) else items | ||
| if json_serializable: | ||
| try: | ||
| json.dumps(obj) | ||
| except (TypeError, OverflowError): | ||
| return str(obj) | ||
| return obj | ||
| finally: | ||
| if is_container: | ||
| _seen.discard(obj_id) | ||
|
|
||
| return _convert(dclass) | ||
|
|
||
|
|
||
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.
The PR title mentions "validate dataclass exports", but the diff only changes ascleandict recursion handling and adds circular-reference tests. If export validation is intended, it looks missing; otherwise please update the PR title/description to match the actual changes to avoid confusion in release notes/history.