Skip to content

gh-89610: Add syntax highlighting for .pyi files in IDLE #28950

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

Merged
merged 34 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bd2cdae
bpo-45447: Add syntax highlighting for `.pyi` files in IDLE
AlexWaygood Oct 14, 2021
b12399d
Improve news entry.
AlexWaygood Oct 15, 2021
96d5a88
Improve docstring at top of util.py
AlexWaygood Oct 15, 2021
eee661e
Improve tests
AlexWaygood Oct 15, 2021
b9ef62b
Use keyword arguments in test_util.py
AlexWaygood Oct 15, 2021
44fb0f1
Correct alphabetical order in README.txt...
AlexWaygood Oct 15, 2021
2d8e0e8
Update coverage statistics in test files
AlexWaygood Oct 15, 2021
4bba476
Add __main__ block for util.py, tweak added test for iomenu.py, tweak…
AlexWaygood Oct 16, 2021
be0043e
Tweak test_util.py
AlexWaygood Oct 18, 2021
f8906a5
Merge branch 'python:main' into idle-pyi-syntax-highlighting
AlexWaygood Oct 28, 2021
5dc03fa
Update Lib/idlelib/util.py
AlexWaygood Jan 16, 2022
4b2c36c
Add example `.pyi` file
AlexWaygood Jan 16, 2022
e9dfeb1
Revert changes to coverage statistics
AlexWaygood Jan 24, 2022
a197c93
Rename `example.pyi` to `example_stub.pyi`
AlexWaygood Jan 24, 2022
eb754bd
Have stub files combined with other Python files in `iomenu.py`
AlexWaygood Jan 24, 2022
e32bfca
Merge remote-tracking branch 'origin/main' into idle-pyi-syntax-highl…
AlexWaygood Jan 24, 2022
c6bd524
Fix broken test, improve other tests
AlexWaygood Jan 24, 2022
5a74935
Add `example_noext` file
AlexWaygood Jan 24, 2022
84b9251
Add entry to 'What's new in 3.11'
AlexWaygood Jan 24, 2022
f790a82
Merge remote-tracking branch 'upstream/main' into pr_28950
terryjreedy Feb 6, 2022
b5e781e
Merge remote-tracking branch 'upstream/main' into pr_28950
terryjreedy Feb 11, 2022
58fef8e
Revert changes in browser.py
terryjreedy Feb 11, 2022
00a51b2
Revert changes to editor.EditorWindow.ispythonsource.
terryjreedy Feb 11, 2022
a589099
Revise util, removing new functions.
terryjreedy Feb 11, 2022
647ae1a
Revise test_util, deleting most tests.
terryjreedy Feb 11, 2022
a090db7
Use tuple instead of frozen set.
terryjreedy Feb 11, 2022
fc9e587
Update 2021-10-14-16-55-03.bpo-45447.FhiH5P.rst
terryjreedy Feb 11, 2022
70f06f1
Create 3.11.rst
terryjreedy Feb 11, 2022
d4d3906
Update 3.11.rst
terryjreedy Feb 11, 2022
aafd8e2
Revert unwanted changed to what's new
terryjreedy Feb 11, 2022
e4b289a
Remove commented-out line
AlexWaygood Feb 11, 2022
91651a7
Merge remote-tracking branch 'upstream/main' into pr_28950
terryjreedy Feb 12, 2022
4931b3f
Final tweaks.
terryjreedy Feb 12, 2022
38e5fc0
Merge branch 'idle-pyi-syntax-highlighting' of https://github.com/Ale…
terryjreedy Feb 12, 2022
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
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ fractions
(Contributed by Mark Dickinson in :issue:`44547`.)


IDLE and idlelib
----------------

* IDLE now applies syntax highlighting to `.pyi` files. (Contributed by Alex
Waygood and Terry Jan Reedy in :issue:`45447`.)

inspect
-------
* Add :func:`inspect.getmembers_static`: return all members without
Expand Down
3 changes: 3 additions & 0 deletions Lib/idlelib/NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Released on 2022-10-03
=========================


bpo-28950: Apply IDLE syntax highlighting to `.pyi` files. Add util.py
for common components. Patch by Alex Waygood and Terry Jan Reedy.

bpo-46630: Make query dialogs on Windows start with a cursor in the
entry box.

Expand Down
1 change: 1 addition & 0 deletions Lib/idlelib/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ tabbedpages.py # Define tabbed pages widget (nim).
textview.py # Define read-only text widget (nim).
tree.py # Define tree widget, used in browsers (nim).
undo.py # Manage undo stack.
util.py # Define objects imported elsewhere with no dependencies (nim)
windows.py # Manage window list and define listed top level.
zoomheight.py # Zoom window to full height of screen.

Expand Down
3 changes: 2 additions & 1 deletion Lib/idlelib/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from idlelib import replace
from idlelib import search
from idlelib.tree import wheel_event
from idlelib.util import py_extensions
from idlelib import window

# The default tab setting for a Text widget, in average-width characters.
Expand Down Expand Up @@ -757,7 +758,7 @@ def ispythonsource(self, filename):
if not filename or os.path.isdir(filename):
return True
base, ext = os.path.splitext(os.path.basename(filename))
if os.path.normcase(ext) in (".py", ".pyw"):
if os.path.normcase(ext) in py_extensions:
return True
line = self.text.get('1.0', '1.0 lineend')
return line.startswith('#!') and 'python' in line
Expand Down
4 changes: 4 additions & 0 deletions Lib/idlelib/idle_test/example_noext
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!usr/bin/env python

def example_function(some_argument):
pass
2 changes: 2 additions & 0 deletions Lib/idlelib/idle_test/example_stub.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Example:
def method(self, argument1: str, argument2: list[int]) -> None: ...
24 changes: 23 additions & 1 deletion Lib/idlelib/idle_test/test_iomenu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Test , coverage 17%."

from idlelib import iomenu
from idlelib import iomenu, util
import unittest
from test.support import requires
from tkinter import Tk
Expand Down Expand Up @@ -45,5 +45,27 @@ def test_fixnewlines_end(self):
eq(fix(), 'a'+io.eol_convention)


def _extension_in_filetypes(extension):
return any(
f'*{extension}' in filetype_tuple[1]
for filetype_tuple in iomenu.IOBinding.filetypes
)


class FiletypesTest(unittest.TestCase):
def test_python_source_files(self):
for extension in util.py_extensions:
with self.subTest(extension=extension):
self.assertTrue(
_extension_in_filetypes(extension)
)

def test_text_files(self):
self.assertTrue(_extension_in_filetypes('.txt'))

def test_all_files(self):
self.assertTrue(_extension_in_filetypes(''))


if __name__ == '__main__':
unittest.main(verbosity=2)
14 changes: 14 additions & 0 deletions Lib/idlelib/idle_test/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Test util, coverage 100%"""

import unittest
from idlelib import util


class UtilTest(unittest.TestCase):
def test_extensions(self):
for extension in {'.pyi', '.py', '.pyw'}:
self.assertIn(extension, util.py_extensions)


if __name__ == '__main__':
unittest.main(verbosity=2)
5 changes: 4 additions & 1 deletion Lib/idlelib/iomenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import idlelib
from idlelib.config import idleConf
from idlelib.util import py_extensions

py_extensions = ' '.join("*"+ext for ext in py_extensions)

encoding = 'utf-8'
if sys.platform == 'win32':
Expand Down Expand Up @@ -348,7 +351,7 @@ def print_window(self, event):
savedialog = None

filetypes = (
("Python files", "*.py *.pyw", "TEXT"),
("Python files", py_extensions, "TEXT"),
("Text files", "*.txt", "TEXT"),
("All files", "*"),
)
Expand Down
22 changes: 22 additions & 0 deletions Lib/idlelib/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Idlelib objects with no external idlelib dependencies
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 (pyshell, run).
"""
from os import path

# .pyw is for Windows; .pyi is for stub files.
py_extensions = ('.py', '.pyw', '.pyi') # Order needed for open/save dialogs.

if __name__ == '__main__':
from unittest import main
main('idlelib.idle_test.test_util', verbosity=2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Apply IDLE syntax highlighting to `.pyi` files. Patch by Alex Waygood
and Terry Jan Reedy.