Skip to content

fix: support extension-only inputs in detect_lang#33

Merged
agsaru merged 2 commits into
agsaru:mainfrom
deepmhatre13:investigate-language-detection
Jul 11, 2026
Merged

fix: support extension-only inputs in detect_lang#33
agsaru merged 2 commits into
agsaru:mainfrom
deepmhatre13:investigate-language-detection

Conversation

@deepmhatre13

@deepmhatre13 deepmhatre13 commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #32

Summary

This PR fixes a bug where detect_lang() always returned "unknown" when called with extension-only metadata (e.g. .py, .js, .cpp).

Root Cause

The loaders store only the file extension in doc.metadata["file_type"].

Later, detect_lang() calls os.path.splitext() on that value again.

For example:

os.path.splitext(".py")
# (".py", "")

As a result, the extracted extension becomes an empty string and every lookup falls back to "unknown".

Changes

  • Support extension-only inputs in detect_lang()
  • Preserve existing behavior for filename/path inputs
  • Add regression tests covering:
    • extension-only inputs
    • filename/path inputs
    • unknown extensions

Testing

python -m pytest tests -v

Result:

19 passed

Summary by CodeRabbit

  • Bug Fixes

    • Improved language detection for dotfiles such as .gitignore.
    • Extension matching is now case-insensitive.
    • Unrecognized filenames and extensions continue to return an “unknown” result.
  • Tests

    • Added coverage for file extensions, full paths, filenames, dotfiles, and unknown formats.

Copilot AI review requested due to automatic review settings July 11, 2026 10:00
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@deepmhatre13, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 55 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 57b77431-96c9-40db-aed8-d56e9aec2c07

📥 Commits

Reviewing files that changed from the base of the PR and between 77b6dfa and 3ff692f.

📒 Files selected for processing (1)
  • repo2readme/utils/detect_language.py
📝 Walkthrough

Walkthrough

detect_lang() now recognizes extension-only metadata, including dotfiles, and normalizes extensions before lookup. Unit tests cover extension-only inputs, full paths, filenames, and unknown values.

Changes

Language Detection

Layer / File(s) Summary
Normalize language detection inputs
repo2readme/utils/detect_language.py, tests/test_detect_language.py
detect_lang() handles dotfiles and performs case-insensitive extension lookups; tests cover recognized extensions, paths, filenames, and unknown inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main fix: supporting extension-only inputs in detect_lang.
Linked Issues check ✅ Passed The change fixes issue #32 by handling extension-only metadata while preserving filename and path lookups.
Out of Scope Changes check ✅ Passed No clearly unrelated code changes are present; the edits stay focused on language detection and regression tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/test_detect_language.py (1)

4-21: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add test coverage for case-insensitive extension lookup.

The .lower() normalization in detect_lang is a key part of this change but no test exercises it. A single assertion for an uppercase extension (e.g., .PY or main.PY) would verify this path.

🧪 Suggested addition
 def test_detect_lang_with_extension_only():
     assert detect_lang(".py") == "python"
     assert detect_lang(".js") == "javascript"
     assert detect_lang(".cpp") == "cpp"
     assert detect_lang(".md") == "markdown"
     assert detect_lang(".json") == "json"
+    assert detect_lang(".PY") == "python"
+
+
 def test_detect_lang_with_path():
     assert detect_lang("main.py") == "python"
     assert detect_lang("index.js") == "javascript"
     assert detect_lang("src/config.yaml") == "yaml"
+    assert detect_lang("main.PY") == "python"
🤖 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 `@tests/test_detect_language.py` around lines 4 - 21, Add an assertion to the
existing detect_lang tests covering an uppercase extension, such as ".PY" or
"main.PY", and verify it returns the same language as the lowercase form. Keep
the coverage focused on the case-insensitive normalization in detect_lang.
🤖 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.

Nitpick comments:
In `@tests/test_detect_language.py`:
- Around line 4-21: Add an assertion to the existing detect_lang tests covering
an uppercase extension, such as ".PY" or "main.PY", and verify it returns the
same language as the lowercase form. Keep the coverage focused on the
case-insensitive normalization in detect_lang.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 546ff2b4-d3eb-47fb-a0c6-de6801c88b96

📥 Commits

Reviewing files that changed from the base of the PR and between 9770808 and 77b6dfa.

📒 Files selected for processing (2)
  • repo2readme/utils/detect_language.py
  • tests/test_detect_language.py

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes language detection when detect_lang() is called with extension-only metadata (e.g., .py) by correctly treating the input as an extension when os.path.splitext() yields an empty extension.

Changes:

  • Update detect_lang() to handle extension-only inputs while preserving path/filename behavior.
  • Add regression tests for extension-only inputs, normal paths, and unknown extensions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_detect_language.py Adds regression coverage for extension-only and path-based language detection.
repo2readme/utils/detect_language.py Fixes detection logic to accept extension-only inputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread repo2readme/utils/detect_language.py Outdated
Comment on lines +32 to +36
def detect_lang(path:str):
_,extension=os.path.splitext(path)
return EXTENSION_LANGUAGE_MAP.get(extension,"unknown") No newline at end of file
if not extension and path.startswith("."):
extension=path
return EXTENSION_LANGUAGE_MAP.get(extension.lower(),"unknown")
@agsaru agsaru merged commit c9585bc into agsaru:main Jul 11, 2026
4 checks passed
@agsaru agsaru added the ECSoC26 label Jul 11, 2026
@agsaru

agsaru commented Jul 11, 2026

Copy link
Copy Markdown
Owner

@deepmhatre13 See the pr is labelled with ECSoC26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Language detection fails for extension-only metadata

3 participants