Skip to content

Add better support for Python functions and props #4285

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Core Grammars:
- enh(json) add json5 support [Kerry Shetline][]
- fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][]
- fix(csharp) Support digit separators [te-ing][]
- enh(python) add support for methods and properties [fibbo][]

Documentation:

Expand Down Expand Up @@ -53,6 +54,7 @@ CONTRIBUTORS
[Thomas Gorissen]: https://github.com/serrynaimo
[te-ing]: https://github.com/te-ing
[Anthony Martin]: https://github.com/anthony-c-martin
[fibbo]: https://github.com/fibbo


## Version 11.11.1
Expand Down
33 changes: 33 additions & 0 deletions src/languages/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,39 @@ export default function(hljs) {
relevance: 0
},
{ match: /\bor\b/, scope: "keyword" },
// Method calls with parentheses
{
match: [
/\./,
IDENT_RE,
/(?=\s*\()/
],
scope: {
2: "title.function.method"
}
},
// Chained method calls
{
match: [
/\./,
IDENT_RE,
/(?=\s*\.\s*\w)/
],
scope: {
2: "title.function.method"
}
},
{
match: [
/(?<!(?:import|from)\s+[\w.]*)/, // Negative lookbehind for import context
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test case for this? I'd like to see an example or two of what exactly we're avoiding here.

Copy link
Author

@fibbo fibbo Jun 21, 2025

Choose a reason for hiding this comment

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

Imagine the following line:

from base.derive import test

I wouldn't want derive to be highlighted as a property. I added a test case for this.

/\./, // Literal dot
IDENT_RE, // Property name
/(?!\s*[\(\[])/ // Not followed by ( or [
],
scope: {
3: "property"
}
},
STRING,
COMMENT_TYPE,
hljs.HASH_COMMENT_MODE,
Expand Down
4 changes: 2 additions & 2 deletions test/markup/python-repl/sample.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
&quot;foo = 42&quot;
<span class="hljs-meta prompt_">&gt;&gt;&gt;</span> <span class="language-python"><span class="hljs-built_in">print</span>(v)</span>
foo = 42
<span class="hljs-meta prompt_">&gt;&gt;&gt;</span> <span class="language-python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">repr</span>(v).rstrip(<span class="hljs-string">&#x27;&quot;&#x27;</span>))</span>
<span class="hljs-meta prompt_">&gt;&gt;&gt;</span> <span class="language-python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">repr</span>(v).<span class="hljs-title function_ method__">rstrip</span>(<span class="hljs-string">&#x27;&quot;&#x27;</span>))</span>
&quot;foo = 42
<span class="hljs-meta prompt_">&gt;&gt;&gt;</span> <span class="language-python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">repr</span>(v).lstrip(<span class="hljs-string">&#x27;&quot;&#x27;</span>))</span>
<span class="hljs-meta prompt_">&gt;&gt;&gt;</span> <span class="language-python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">repr</span>(v).<span class="hljs-title function_ method__">lstrip</span>(<span class="hljs-string">&#x27;&quot;&#x27;</span>))</span>
foo = 42&quot;

<span class="hljs-meta prompt_">&gt;&gt;&gt;</span> <span class="language-python"><span class="hljs-string">&quot;&quot;&quot;</span></span>
Expand Down
2 changes: 1 addition & 1 deletion test/markup/python/class_self.expect.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span class="hljs-keyword">class</span> <span class="hljs-title class_">SelfTest</span>:
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>):
<span class="hljs-variable language_">self</span>.text = <span class="hljs-literal">True</span>
<span class="hljs-variable language_">self</span>.<span class="hljs-property">text</span> = <span class="hljs-literal">True</span>

<span class="hljs-keyword">def</span> <span class="hljs-title function_">method</span>(<span class="hljs-params">self</span>):
<span class="hljs-keyword">pass</span>
2 changes: 1 addition & 1 deletion test/markup/python/function-header-comments.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span class="hljs-keyword">pass</span>


<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span>(collections.namedtuple(<span class="hljs-string">&#x27;Test&#x27;</span>), (
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span>(collections.<span class="hljs-title function_ method__">namedtuple</span>(<span class="hljs-string">&#x27;Test&#x27;</span>), (
<span class="hljs-string">&#x27;name&#x27;</span>, <span class="hljs-comment"># comment</span>
)):
<span class="hljs-keyword">pass</span>
6 changes: 5 additions & 1 deletion test/markup/python/keywords.expect.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<span class="hljs-keyword">from</span> base.derive <span class="hljs-keyword">import</span> test
<span class="hljs-keyword">import</span> base.test

<span class="hljs-keyword">class</span> <span class="hljs-title class_">Shorty</span>(<span class="hljs-title class_ inherited__">dict</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">len</span>(<span class="hljs-params">self</span>):
<span class="hljs-keyword">return</span> <span class="hljs-literal">NotImplemented</span>

x = Shorty()
x.<span class="hljs-title function_ method__">len</span>()
<span class="hljs-built_in">len</span>(x)

<span class="hljs-keyword">if</span> <span class="hljs-literal">__debug__</span>:
sys = <span class="hljs-built_in">__import__</span>(<span class="hljs-string">&#x27;sys&#x27;</span>)

<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> sys.path:
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> sys.<span class="hljs-property">path</span>:
<span class="hljs-built_in">print</span>(_)

<span class="hljs-built_in">exec</span>(<span class="hljs-number">123</span>)
Expand Down
4 changes: 4 additions & 0 deletions test/markup/python/keywords.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from base.derive import test
import base.test

class Shorty(dict):
def len(self):
return NotImplemented

x = Shorty()
x.len()
len(x)

if __debug__:
Expand Down
2 changes: 1 addition & 1 deletion test/markup/python/matrix-multiplication.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

<span class="hljs-meta"> @decorator</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(<span class="hljs-params">self, H, V, beta, r</span>):
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
S = (H @ beta - r).<span class="hljs-property">T</span> @ inv(H @ V @ H.<span class="hljs-property">T</span>) @ (H @ beta - r)
<span class="hljs-keyword">return</span> S
4 changes: 2 additions & 2 deletions test/markup/python/numbers.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


<span class="hljs-comment"># expressions containing numeric literals</span>
<span class="hljs-number">0.</span>.__str__, <span class="hljs-number">1e1</span>.__str__, fn(<span class="hljs-number">.5</span>)
<span class="hljs-number">0.</span>.<span class="hljs-property">__str__</span>, <span class="hljs-number">1e1</span>.<span class="hljs-property">__str__</span>, fn(<span class="hljs-number">.5</span>)
<span class="hljs-number">0</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>, <span class="hljs-number">0l</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
<span class="hljs-number">0_0_0</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>, <span class="hljs-number">0_0_0l</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
<span class="hljs-number">0b0</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>, <span class="hljs-number">0b0l</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
Expand All @@ -50,7 +50,7 @@
<span class="hljs-number">0_0_0j</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>, <span class="hljs-number">0_0_9j</span><span class="hljs-keyword">is</span> <span class="hljs-number">0</span>

<span class="hljs-comment"># expressions not containing numeric literals</span>
x0.j
x0.<span class="hljs-property">j</span>

<span class="hljs-comment"># invalid pseudo-numeric expressions</span>
1__0
Expand Down