This is a Python programming language detection library.
It detects the programming language of source code with an ensemble of classifiers.
Use this when a quick first approximation is good enough.
Accuracy depends heavily on the available hints; checked-in reports live in docs/accuracy.md.
I created this because I wanted
- a Python programming language detector
- no machine learning dependencies
Tested on python 3.10 through 3.14.
from whats_that_code.election import guess_language_all_methods
code = "def yo():\n print('hello')"
result = guess_language_all_methods(code, file_name="yo.py")
assert result == "python" # returns a single language name (str), or None if unknownPass an Options to avoid guessing obscure languages unless there is strong
evidence (a matching file extension, shebang, or tag). The default is unchanged —
no suppression — so existing callers are unaffected.
from whats_that_code.election import guess_language_all_methods
from whats_that_code.options import Options
# "common" keeps only mainstream languages; "uncommon" also keeps established ones.
guess_language_all_methods(code, options=Options(min_tier="common"))
# A .zig file is still detected as Zig even though Zig is below "common":
guess_language_all_methods(code, file_name="x.zig", options=Options(min_tier="common")) # -> "zig"Regex marker matching (the hottest path) runs on Google's re2 — a normal
dependency, installed automatically — which is linear-time and ~6× faster on large
inputs, with identical results. If a platform has no re2 wheel it transparently
falls back to stdlib re. Nothing to configure.
With Options(use_parsers=True) the code is actually parsed; a clean parse is
strong evidence for that language. This uses stdlib parsers (Python/JSON/XML/TOML)
plus ~26 tree-sitter grammars that are installed with the main package.
On the evaluation corpus this lifted code-only accuracy from ~13% to ~21% when added. It is off by default, so existing callers are unaffected.
guess_language_all_methods(go_source, options=Options(use_parsers=True)) # -> "go"- Inspects file extension if available.
- Inspects shebang
- Looks for keywords
- Counts regexes for common patterns
- Attempts to parse Python, JSON, XML, and TOML
- Inspects tags if available.
Each is imperfect and can error. The classifier then combines the results of each using a voting algorithm
This works best if you only use it for fallback, e.g. classifying code that can't already be classified by extension or tag, or when tag is ambiguous.
It was a tool that outgrew being a part of so_pip a StackOverflow code extraction tool I wrote.