Skip to content

gh-144207: Syntax highlighting (theming support) for dis module - #144208

Open
AbduazizZiyodov wants to merge 29 commits into
python:mainfrom
Abduaziz-Projects:dis-theme-support
Open

gh-144207: Syntax highlighting (theming support) for dis module#144208
AbduazizZiyodov wants to merge 29 commits into
python:mainfrom
Abduaziz-Projects:dis-theme-support

Conversation

@AbduazizZiyodov

@AbduazizZiyodov AbduazizZiyodov commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

About

Implemented in similar fashion (e.g unittest, difflib etc.) by defining ThemeSection and registered theme on Theme class.

Basic highlighting shown in forum(green for all opcodes), but in this PR: one color maps to group of opcodes (e.g. blue for binary opcodes) -- gives better context and it is determined via color_by_opname method.

I have trouble with tests, now dis is defaulting to colored output and 2 tests were failing. I had to set environment variable: NO_COLOR=1... I think, there might be better ways of doing this, one is to define force_no_color flag(keyword only) to dis.dis function which I'm not certain. So, I would like to elaborate with you on that.

EDIT: Any ideas/additions on color mapping is encouraged :)

Sample screenshots

(see #144208 (comment) for the latest)

In dark mode dark_01 dark_02
In light mode light_01 light_02
No color no_color_01

Thanks.

Highlights opcode's name, arguments, exception table labels.
…ecause dis is not defaulting to syntax highligthing. Of course it needs better handling which I'm not sure now.
@bedevere-app

bedevere-app Bot commented Jan 24, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Jan 24, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jan 24, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a NEWS entry and update What's New.

Comment thread Lib/test/test_dis.py Outdated
Comment thread Lib/_colorize.py Outdated
Comment thread Lib/dis.py Outdated
Comment thread Lib/test/test_dis.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add tests for the colouration.

@bedevere-app

bedevere-app Bot commented Jan 24, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Comment thread Lib/dis.py Outdated
Comment thread Lib/_colorize.py

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add a What's News entry and tests. I personally do not like this by default since I use grep/cut on the dis output so I would prefer a CLI option to enable colors. EDIT: not a concern anymore

Comment thread Lib/_colorize.py
"GET_AWAITABLE",
"GET_AITER",
"GET_ANEXT",
"END_ASYNC_FOR",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am a bit confused with END_FOR and END_ASYNC_FOR being colored differently but I do not remember the exact effect of the latter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've read that END_FOR is equivalent(or alias I would say) to POP_TOP:

Removes the top-of-stack item. Equivalent to POP_TOP. Used to clean up at the end of loops, hence the name.

and it is specified under "General instructions" section while END_ASYNC_FOR in "Coroutine opcodes" (I generalized this into "control flow" opcodes).

Almost all opcodes are dealing with stack, but END_ASYNC_FOR is putting little more effort than END_FOR which is just stack.pop(), that's why I thought END_ASYNC_FOR is different than END_FOR.

That's my understanding.

We might elaborate our discussion on categories in your next comment too.

Comment thread Lib/_colorize.py Outdated
exception_label: str = ANSIColors.CYAN
argument_detail: str = ANSIColors.GREY

op_stack: str = ANSIColors.BOLD_YELLOW

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How were those categories determined? are they determined already like that in dis.rst?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

are they determined already like that in dis.rst?

Almost, I first grouped according to dis.rst then re-categorized them according to my understanding (how these opcodes relate, semantically) -- which might need some refinement too.

Comment thread Lib/dis.py Outdated
@bedevere-app

bedevere-app Bot commented Jan 24, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@hugovk

hugovk commented Jan 24, 2026

Copy link
Copy Markdown
Member

I personally do not like this by default since I use grep/cut on the dis output

Not to worry, it can stay on by default because colour is automatically disabled in pipes:

image

@picnixz

picnixz commented Jan 24, 2026

Copy link
Copy Markdown
Member

Oh! great then. I was worried about this. Do we plan to have colors for JSON? (or maybe we already do?) or for symtable? (the symtable cli is very rudimentary and I sometimes want a better one).

There are few modules who output formatted code (AFAIR, dis, symtable, json or even ast) so I wondered whether those would also be colorized. And if we eventually plan to add colors wherever we can (it should be a separate DPO thread/gh issue)

@hugovk

hugovk commented Jan 24, 2026

Copy link
Copy Markdown
Member

json, yes: https://docs.python.org/3/whatsnew/3.14.html#json. symtable, no[t yet].

@bedevere-app

bedevere-app Bot commented Jan 25, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Comment thread Lib/dis.py Outdated
Comment thread Lib/test/test_dis.py Outdated

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, I did not check that colorizations logic, because I am not an expert in it.

@picnixz

picnixz commented Mar 1, 2026

Copy link
Copy Markdown
Member

I emitted some reservations in https://discuss.python.org/t/syntax-highlighting-for-dis-module/105833/14 about the explosion of colors. I would prefer that we have alternate colorization in terms of blocks like godbolt

@hugovk

hugovk commented Apr 18, 2026

Copy link
Copy Markdown
Member

Where are we up to with this one? There's just over a couple of weeks before the 3.15 feature freeze.

@picnixz Is the current state a blocker for you?

@picnixz

picnixz commented Apr 19, 2026

Copy link
Copy Markdown
Member

I have commented on the DPO thread about my concerns for the overusage of colors and suggested zebra lines per section.

@hugovk

hugovk commented Apr 20, 2026

Copy link
Copy Markdown
Member

OK, thanks!

@AbduazizZiyodov Do you know what to do next? Do you have an idea of the colour scheme to use?

@AbduazizZiyodov

Copy link
Copy Markdown
Contributor Author

OK, thanks!

@AbduazizZiyodov Do you know what to do next? Do you have an idea of the colour scheme to use?

I have some idea, the screenshot I've shared on discourse forum + coloring exception tables (e.g. magenta, red). Any suggestions are welcome - current color scheme(the way it "rendered") looks bit sharp to me.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Jul 24, 2026
@AbduazizZiyodov
AbduazizZiyodov marked this pull request as draft August 17, 2026 23:36
@read-the-docs-community

read-the-docs-community Bot commented Aug 17, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34127044 | 📁 Comparing c3378fb against main (a7bb524)

  🔍 Preview build  

3 files changed
± library/asyncio-task.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 18, 2026
@StanFromIreland

Copy link
Copy Markdown
Member

I think adding an additional option for the block highlight is a little messy. Indeed it's currently not ideal, another option would be to use ESC[7m to invert, but that'll be quite a bright highlight. We could detect the user's background and adjust appropriately, but that's also messy.

Ultimately, while the background is nice, I don't consider it a deal-breaker.

@AbduazizZiyodov

Copy link
Copy Markdown
Contributor Author

I think adding an additional option for the block highlight is a little messy.

Exactly, and my intention to introduce this here was for sake of experimenting (PoC) since PR is WIP/Draft.

Indeed it's currently not ideal, another option would be to use ESC[7m to invert, but that'll be quite a bright highlight. We could detect the user's background and adjust appropriately, but that's also messy.

+1

Ultimately, while the background is nice, I don't consider it a deal-breaker.

So, we(I) can keep it as ast/tokenize-way like plain highlighting ? I'll have a look on highlighting "logic" again (i.e. what to highlight, how etc.)

@StanFromIreland

Copy link
Copy Markdown
Member

So, we(I) can keep it as ast/tokenize-way like plain highlighting ?

That seems fine to me.

@AbduazizZiyodov

Copy link
Copy Markdown
Contributor Author

Marking PR as ready now.

  • I think it is final version (for me). And now argument detail is "rendered" as an italic (-1 colors)
  • News entry was added, moved "whatsnew" section into 3.16.rst (i hope its appropriate)
  • Tests are here too

Screenshots (ast -> tokenize -> dis):

Dark color scheme:

Screenshot From 2026-08-19 01-20-51

Light color scheme:

Screenshot From 2026-08-19 01-22-23

On bigger scope:

Dark color scheme Screenshot From 2026-08-19 01-23-49 Screenshot From 2026-08-19 01-23-56
Light color scheme Screenshot From 2026-08-19 01-22-51 Screenshot From 2026-08-19 01-22-58

CC: @picnixz @StanFromIreland @hugovk @sobolevn

Thanks.

@AbduazizZiyodov
AbduazizZiyodov marked this pull request as ready for review August 18, 2026 20:39
@cdce8p

cdce8p commented Aug 19, 2026

Copy link
Copy Markdown
Contributor
Screenshot From 2026-08-19 01-23-56

Not sure how others feel about it but I'm not sure this actually improves readability. To me the colors in this example feel much more distracting than anything else. I believe it came up in the discuss thread as well that compiler explorer/godbolt is generally a reasonable / good output. While I agree that banding might be too much for the terminal, maybe the color choices can at least be adopted. Most notably that the instructions are usually just one color. Only jump targets appear to be highlighted. That would reduce the visual noice and make it easier to read IMO. For reference: https://discuss.python.org/t/syntax-highlighting-for-dis-module/105833/20

Side note for the argument details: Not sure italics are helpful here. I'd prefer unformatted text over that. If the intention just was to reduce the number of different colors, I do think the compiler explorer style for highlighting these would work as well. Especially if the number of colors for instructions is reduced.

--
Edit: Linking the screenshot seems to be broken. I'm referring to the one from your last post: #144208 (comment), the "bigger scope" example.

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.

6 participants