-
Notifications
You must be signed in to change notification settings - Fork 1
fix(oocana): use dict mapping for compression_suffix instead of if-elif chain #457
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,28 +69,28 @@ def compression_options(context: 'Context') -> CompressionOptions | None: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"An unexpected error occurred while reading compression options: {e}. Returning None.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Mapping from compression method to file suffix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| COMPRESSION_SUFFIXES = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "zip": ".zip", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "gzip": ".gz", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "bz2": ".bz2", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "zstd": ".zst", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "xz": ".xz", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "tar": ".tar", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def compression_suffix(context: 'Context') -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Get the file suffix based on the compression method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If no compression is specified, return an empty string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If no compression is specified, return ".pkl" (pickle format). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compression = compression_options(context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if compression is None or compression["method"] is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if compression is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".pkl" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method = compression["method"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if method == "zip": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".zip" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif method == "gzip": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".gz" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif method == "bz2": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".bz2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif method == "zstd": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".zst" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif method == "xz": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".xz" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif method == "tar": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".tar" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ".pkl" # Default case if method is not recognized | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method = compression.get("method") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if method is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+92
to
+93
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method = compression.get("method") | |
| if method is None: | |
| # Ensure we have a dictionary before accessing keys. Malformed or unexpected | |
| # JSON in the options file may result in a non-dict value here. | |
| if not isinstance(compression, dict): | |
| return ".pkl" | |
| method = compression.get("method") | |
| # Guard against non-string (and thus potentially unhashable) methods. | |
| if not isinstance(method, str): |
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.
防止压缩配置为非 dict 时触发 AttributeError
compression_options 读取的是任意 JSON,若文件内容为合法但非 dict(如字符串/列表),compression.get(...) 会直接报错。建议加类型保护并回退到默认后缀。
🛡️ 参考修复
compression = compression_options(context)
- if compression is None:
+ if not isinstance(compression, dict):
return ".pkl"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| compression = compression_options(context) | |
| if compression is None or compression["method"] is None: | |
| if compression is None: | |
| return ".pkl" | |
| method = compression["method"] | |
| if method == "zip": | |
| return ".zip" | |
| elif method == "gzip": | |
| return ".gz" | |
| elif method == "bz2": | |
| return ".bz2" | |
| elif method == "zstd": | |
| return ".zst" | |
| elif method == "xz": | |
| return ".xz" | |
| elif method == "tar": | |
| return ".tar" | |
| else: | |
| return ".pkl" # Default case if method is not recognized | |
| \ No newline at end of file | |
| method = compression.get("method") | |
| if method is None: | |
| return ".pkl" | |
| return COMPRESSION_SUFFIXES.get(method, ".pkl") | |
| compression = compression_options(context) | |
| if not isinstance(compression, dict): | |
| return ".pkl" | |
| method = compression.get("method") | |
| if method is None: | |
| return ".pkl" | |
| return COMPRESSION_SUFFIXES.get(method, ".pkl") |
🤖 Prompt for AI Agents
In `@oocana/oocana/serialization.py` around lines 87 - 96, compression_options 返回的
compression 可能不是 dict,从而在 compression.get("method") 处抛出
AttributeError;在函数/片段中(使用
compression_options、compression、method、COMPRESSION_SUFFIXES 的地方)先对 compression
做类型保护(例如 isinstance(compression, dict) 或
collections.abc.Mapping),若不是合适的映射类型则直接返回默认后缀 ".pkl";若是映射再安全读取 method 并用
COMPRESSION_SUFFIXES.get(method, ".pkl") 返回结果。
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,164 @@ | ||||||
| import unittest | ||||||
| from unittest.mock import MagicMock, patch | ||||||
|
||||||
| from unittest.mock import MagicMock, patch | |
| from unittest.mock import MagicMock |
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 compression methods are now duplicated in three places (
SUPPORTED_COMPRESSION_METHODS, theCompressionOptionsLiteral, andCOMPRESSION_SUFFIXES). This can drift over time (e.g., a method added toSUPPORTED_COMPRESSION_METHODSbut missing here would silently fall back to .pkl). Consider making one source of truth (e.g., deriveSUPPORTED_COMPRESSION_METHODSfromCOMPRESSION_SUFFIXES.keys()), and keep the type Literal in sync with that constant.