#280: Fix unicode max diameter#1228
Conversation
📝 WalkthroughWalkthroughAdds the P34 Class inheritance pattern, registers it, documents it, and adds fixtures/tests for inheritance cases. It also updates autodetected text reading to try multiple encodings and adds non-UTF-8 coverage. ChangesClass inheritance pattern
Autodetected encoding fallback
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PMD (7.25.0)test/patterns/class_inheritance/NestedInheritance.java[ERROR] Error at scripts/ruleset.xml:23:36 24| 20| 24| test/patterns/class_inheritance/TypeParameterExtends.java[ERROR] Error at scripts/ruleset.xml:23:36 24| 20| 24| test/patterns/class_inheritance/SimpleInheritance.java[ERROR] Error at scripts/ruleset.xml:23:36 24| 20| 24|
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@aibolit/utils/encoding_detector.py`:
- Around line 24-31: The fallback in encoding detection is unreachable because
`detect_encoding_of_data`, `utf-8`, and `latin-1` are tried before the
replacement decode, and `latin-1` will always succeed. Update `decode_data` in
`aibolit/utils/encoding_detector.py` so the final `data.decode(...,
errors="replace")` path can actually run, either by removing `latin-1` from the
initial `encodings` list or by moving the replacement decode ahead of it while
keeping the existing trial order logic in `dict.fromkeys(...)`.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b9ab1e85-095e-407a-ac30-058f7d221c89
📒 Files selected for processing (12)
PATTERNS.mdaibolit/config.pyaibolit/patterns/class_inheritance/__init__.pyaibolit/patterns/class_inheritance/class_inheritance.pyaibolit/utils/encoding_detector.pytest/metrics/maxDiameter/test_max_diameter.pytest/patterns/class_inheritance/NestedInheritance.javatest/patterns/class_inheritance/OnlyImplements.javatest/patterns/class_inheritance/SimpleInheritance.javatest/patterns/class_inheritance/TypeParameterExtends.javatest/patterns/class_inheritance/test_class_inheritance.pytest/utils/test_encoding_detector.py
| encodings = [detect_encoding_of_data(data), 'utf-8', 'latin-1'] | ||
| for encoding in dict.fromkeys(filter(None, encodings)): | ||
| try: | ||
| return data.decode(encoding) | ||
| except (LookupError, UnicodeDecodeError): | ||
| continue | ||
|
|
||
| return data.decode('utf-8', errors='replace') |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
utf-8(errors="replace") fallback is unreachable with current order
Because Line 24 always includes latin-1, Line 31 will never execute (latin-1 decode succeeds for any bytes). If replacement-decoding is intended as a real last resort, remove latin-1 from the trial list or move replacement logic before it.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@aibolit/utils/encoding_detector.py` around lines 24 - 31, The fallback in
encoding detection is unreachable because `detect_encoding_of_data`, `utf-8`,
and `latin-1` are tried before the replacement decode, and `latin-1` will always
succeed. Update `decode_data` in `aibolit/utils/encoding_detector.py` so the
final `data.decode(..., errors="replace")` path can actually run, either by
removing `latin-1` from the initial `encodings` list or by moving the
replacement decode ahead of it while keeping the existing trial order logic in
`dict.fromkeys(...)`.
Summary by CodeRabbit
New Features
Bug Fixes
Tests