Skip to content

Make IDLE recognize .pyi stub files as source for open, save, and edit #89610

Closed

Description

@AlexWaygood
BPO 45447
Nosy @terryjreedy, @ronaldoussoren, @zooba, @miss-islington, @E-Paine, @AlexWaygood
PRs
  • gh-89610: Add syntax highlighting for .pyi files in IDLE #28950
  • [3.10] bpo-45447: Add syntax highlighting for .pyi files in IDLE (GH-28950) #31303
  • bpo-45447: Add entry to What's new 3.10 #31304
  • bpo-45447: Add entry to What's new 3.9 #31305
  • [3.9] bpo-45447: Add syntax highlighting for .pyi files in IDLE (GH-28950) #31306
  • bpo-45447: Fix entry in What's New 3.11 #31307
  • [3.10] bpo-45447: Add entry to What's new 3.10 (GH-31304) #31308
  • [3.10] bpo-45447: Add entry to What's new 3.9 (GH-31305) #31309
  • [3.9] bpo-45447: Add entry to What's new 3.9 (GH-31305) #31310
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/terryjreedy'
    closed_at = None
    created_at = <Date 2021-10-12.15:24:36.540>
    labels = ['expert-IDLE', 'type-feature', '3.11']
    title = 'Make IDLE recognize .pyi stub files as source for open, save, and edit'
    updated_at = <Date 2022-02-14.03:35:10.254>
    user = 'https://github.com/AlexWaygood'

    bugs.python.org fields:

    activity = <Date 2022-02-14.03:35:10.254>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = False
    closed_date = None
    closer = None
    components = ['IDLE']
    creation = <Date 2021-10-12.15:24:36.540>
    creator = 'AlexWaygood'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45447
    keywords = ['patch']
    message_count = 28.0
    messages = ['403738', '403741', '403755', '403772', '403884', '403889', '403915', '403998', '404021', '404023', '404030', '404100', '404102', '404110', '410637', '410669', '412561', '412617', '413147', '413149', '413150', '413156', '413157', '413158', '413168', '413169', '413170', '413211']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'ronaldoussoren', 'steve.dower', 'miss-islington', 'epaine', 'AlexWaygood']
    pr_nums = ['28950', '31303', '31304', '31305', '31306', '31307', '31308', '31309', '31310']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue45447'
    versions = ['Python 3.11']

    Linked PRs

    Activity

    1. AlexWaygood commented on Oct 12, 2021

      @AlexWaygood
      MemberAuthor

      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.

    2. changed the title [-]Support syntax highlighting for .pyi stub files[/-] [+]IDLE: Support syntax highlighting for .pyi stub files[/+] on Oct 12, 2021
    3. changed the title [-]Support syntax highlighting for .pyi stub files[/-] [+]IDLE: Support syntax highlighting for .pyi stub files[/+] on Oct 12, 2021
    4. terryjreedy commented on Oct 12, 2021

      @terryjreedy
      Member

      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.

    5. AlexWaygood commented on Oct 12, 2021

      @AlexWaygood
      MemberAuthor

      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?

    6. terryjreedy commented on Oct 13, 2021

      @terryjreedy
      Member

      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.

    7. terryjreedy commented on Oct 14, 2021

      @terryjreedy
      Member

      I am going to call the file util.py and will work on a PR.

    8. AlexWaygood commented on Oct 14, 2021

      @AlexWaygood
      MemberAuthor

      Oh — I've already nearly finished a PR. Do you mind if I submit mine? Happy to make any changes you like, obviously.

    9. 34 remaining items

    10. Repository owner moved this from In Progress to Done in IDLE Issueson Jul 28, 2022
    11. ronaldoussoren commented on Jul 28, 2022

      @ronaldoussoren
      Contributor

      See also my comment about adding pyi to the list of CFBundleTypeExtensions in IDLE's Info.plist. That will allow opening stub files by double clicking on them in the Finder, and is not necessary for the File/open menu.

      Otherwise syntax coloring for stub files appears to work fine in the current Python 3.11 beta.

    12. added a commit that references this issue on Jul 28, 2022
    13. erlend-aasland commented on Jul 28, 2022

      @erlend-aasland
      Contributor

      See also my comment about adding pyi to the list of CFBundleTypeExtensions in IDLE's Info.plist.

      👉🏻 #95393

    14. added a commit that references this issue on Jul 28, 2022
    15. erlend-aasland commented on Jul 28, 2022

      @erlend-aasland
      Contributor

      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

      👉🏻 #95397

      • iomenu.IOBinding.filetypes

      Seems irrelevant for this issue to me; I might be wrong, I only took a quick glance at the code.

    16. terryjreedy commented on Jul 28, 2022

      @terryjreedy
      Member

      I listed iomenu filetypes before we added the util.py_extensions tuple, which is used in several places. Before, the same information was duplicated in multiple places. That item is now obsolete.

    17. added 2 commits that reference this issue on Jul 28, 2022
    18. added a commit that references this issue on Jul 28, 2022
    19. added 2 commits that reference this issue on Jul 28, 2022
    20. terryjreedy commented on Jul 29, 2022

      @terryjreedy
      Member

      I open task issue #95410 with two of the items completed by this issue. I opened a browser issue #95411 and moved the browser PR there.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions