classification
Title: IDLE: Support syntax highlighting for .pyi stub files
Type: enhancement Stage: patch review
Components: IDLE Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: AlexWaygood, epaine, ronaldoussoren, steve.dower, terry.reedy
Priority: normal Keywords: patch

Created on 2021-10-12 15:24 by AlexWaygood, last changed 2021-10-16 19:11 by terry.reedy.

Pull Requests
URL Status Linked Edit
PR 28950 open AlexWaygood, 2021-10-14 16:10
Messages (14)
msg403738 - (view) Author: Alex Waygood (AlexWaygood) * Date: 2021-10-12 15:24
IDLE currently does not do any syntax highlighting if you use it to open a .pyi stub file, even though everything in a .pyi file is a valid Python expression. It would be great if syntax highlighting for .pyi files could be supported.
msg403741 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-12 15:51
It should be fairly easy to recognize x.pyi as a python file.  Just have to find the place.  Something like
  def f(i:int) -> int: ...
would have 'def' and 'int' highlighted.
msg403755 - (view) Author: Alex Waygood (AlexWaygood) * Date: 2021-10-12 19:36
It looks like support for .py files is hardcoded into IDLE's source code in the following places:


* browser.ModuleBrowserTreeItem.OnDoubleClick
* browser.ModuleBrowserTreeItem.IsExpandable
* browser.ModuleBrowserTreeItem.listchildren
* editor.ispythonsource  << This is the one that controls syntax highlighting
* iomenu.IOBinding.filetypes


If I were to file a PR adding support for .pyi files, would you prefer that I hardcoded ".pyi" in, or would it be better to add a SUPPORTED_FILES constant somewhere?
msg403772 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-13 00:49
Thank you for doing the research.

I have been thinking about adding a file containing idlelib 'leaf' objects, those with no idlelib dependencies, which are needed in more than one file. would be to reduce the complexity of the idlelib dependency graph, which has enough cyclic dependencies to make startup tricky.  Time to do it.  idlelib/common.py?, leaves.py?, or ???  Draft:
---
"""Idlelib objects with no external idlelib dependencies and which are needed in more than one idlelib module.  They are included here because a) they don't particularly belong elsewhere or b) because inclusion here simplifies the idlelib dependency graph.

TODO: Python versions (editor and help_about), tk version and patchlevel (pyshell, help_about, maxos?, editor?), std streams (pyshell, run), warning stuff (ditto).
"""

# python_extensions is used in editor, browser, and iomenu.
# .pyw is for Windows; .pyi is for stub files.
python_extensions = ('.py', '.pyw', '.pyi')  
extension_string = "*" + " *".join(python_extensions)
---

editor.EditorWindow.ispythonsource could be moved into the new file if a 'firstline=None' argument were added.

OnDoubleClick should use IsExpandable.  IsExpandable should use the logic in ispythonsource so Linux no-extension python source can be browsed as well as edited.

After manual testing, add a new test_common.py file in idlelib.idle_test. After imports, it could start with one test:

class ExtensionTest(TestCase):
    def test_stub(self):
        self.assertIn('.pyi', common.python_extensions)
        self.assertIn('.pyi', common.extension_string)

For IDLE, I can and will backport.
msg403884 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-14 03:27
I am going to call the file util.py and will work on a PR.
msg403889 - (view) Author: Alex Waygood (AlexWaygood) * Date: 2021-10-14 07:36
Oh — I've already nearly finished a PR. Do you mind if I submit mine? Happy to make any changes you like, obviously.
msg403915 - (view) Author: Alex Waygood (AlexWaygood) * Date: 2021-10-14 16:25
Have posted my PR. I've tested it manually and run the IDLE test suite on it with the `-u all` setting; it seems fine. The new tests I've written are a bit thin on the ground right now, though. I may need some guidance/help if more extensive tests are needed -- I'm a little new to unittest.

One other thing I'm not sure on: the docstring at the top of `browser.py` includes the line "- finish removing limitation to x.py files (ModuleBrowserTreeItem)" in the TODO items. Can this be removed now?
msg403998 - (view) Author: E. Paine (epaine) * Date: 2021-10-15 09:58
Would it make sense, since this issue pretty much makes IDLE officially support .pyi files, to add the "Edit with IDLE" menu when right-clicking on a .pyi file in Windows explorer?
msg404021 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-15 14:15
Yes.  After this issue is done, we should open a new issue and nosy Steve Dower, who is in charge of that for Windows, and Ned Deily, in case anything should be done on mac.
msg404023 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-10-15 14:23
The most straightforward change for macOS is to add “pyi” to the list of extensions in “Mac/IDLE/IDLE.app/Contents/Info.plist”.  That way IDLE will be seen as a valid editor for .pyi files.
msg404030 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-15 15:29
Steve, how do we make it so that "Edit with IDLE" appears when right-clicking on a .pyi file in Windows explorer?  As easy as on mac (see Ronald's answer above)?
msg404100 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-10-16 16:34
Bit more complicated. It'll need changes to Tools/msi/launcher/launcher_reg.wxs, Tools/msi/tcltk/tcltk_reg.wxs, and PC/layout/support/appxmanifest.py, but I don't think it's that obvious where (we don't want these files to open with py.exe by default, so they'll need a whole new definition).
msg404102 - (view) Author: Alex Waygood (AlexWaygood) * Date: 2021-10-16 16:54
Shall I add the change to the mac file to my PR, or would it be better to leave it for a separate PR? (I'm not confident enough to make the changes to the Windows files.)
msg404110 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-16 19:11
We can add the mac change once it is manually tested on some system.  I may try on my macbook.  Windows will be another PR or even issue.
History
Date User Action Args
2021-10-16 19:11:57terry.reedysetmessages: + msg404110
2021-10-16 16:54:34AlexWaygoodsetmessages: + msg404102
2021-10-16 16:34:21steve.dowersetmessages: + msg404100
2021-10-15 15:29:47terry.reedysetnosy: + steve.dower
messages: + msg404030
2021-10-15 14:23:10ronaldoussorensetnosy: + ronaldoussoren
messages: + msg404023
2021-10-15 14:15:21terry.reedysetmessages: + msg404021
2021-10-15 09:58:47epainesetmessages: + msg403998
2021-10-14 16:25:24AlexWaygoodsetmessages: + msg403915
2021-10-14 16:10:50AlexWaygoodsetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request27239
2021-10-14 11:12:26epainesetnosy: + epaine
2021-10-14 07:36:51AlexWaygoodsetmessages: + msg403889
2021-10-14 03:27:13terry.reedysetmessages: + msg403884
2021-10-13 00:49:08terry.reedysetmessages: + msg403772
stage: test needed
2021-10-12 19:36:00AlexWaygoodsetmessages: + msg403755
2021-10-12 15:51:22terry.reedysetmessages: + msg403741
2021-10-12 15:24:54AlexWaygoodsettitle: Support syntax highlighting for .pyi stub files -> IDLE: Support syntax highlighting for .pyi stub files
2021-10-12 15:24:36AlexWaygoodcreate