GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Untested code in `typing.py` · Issue #105834 · python/cpython

Untested code in typing.py #105834

New issue
Copy link
New issue
Copy link
Closed
Closed
Untested code in typing.py#105834
Copy link
Labels
testsTests in the Lib/test dirTests in the Lib/test dirtopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@AlexWaygood
AlexWaygood
opened on Jun 15, 2023
Issue body actions

Bug report

If you apply this diff to typing.py, all tests continue to pass:

diff --git a/Lib/typing.py b/Lib/typing.py
index 1dd9398344..98e19644a2 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1931,6 +1931,7 @@ def _proto_hook(other):
                     if (isinstance(annotations, collections.abc.Mapping) and
                             attr in annotations and
                             issubclass(other, Generic) and getattr(other, '_is_protocol', False)):
+                        1/0
                         break

The break on line 1934 here is unreachable; thus, this whole block of code is pointless:

cpython/Lib/typing.py

Lines 1929 to 1934 in 3af2dc7

# ...or in annotations, if it is a sub-protocol.
annotations = getattr(base, '__annotations__', {})
if (isinstance(annotations, collections.abc.Mapping) and
attr in annotations and
issubclass(other, Generic) and getattr(other, '_is_protocol', False)):
break

I think we can say for sure that the break here is unreachable. This is the __subclasshook__ method that is monkey-patched onto all subclasses of typing.Protocol that do not define their own __subclasshook__ methods. This block of code in the __subclasshook__ method is inspecting the __annotations__ dictionary of other to see if it can find any protocol members in that dictionary. But we know that there can't be any protocol members in the __annotations__ dictionary, because if there were, that would make other a protocol with at least one non-callable member. If it's a protocol that has at least one non-callable member, the __subclasshook__ method is never called at all during isinstance() or issubclass() checks, because we raise TypeError in _ProtocolMeta.__subclasscheck__, short-circuiting the call to abc.ABCMeta.__subclasscheck__ that would call Protocol.__subclasshook__:

cpython/Lib/typing.py

Lines 1828 to 1831 in 3af2dc7

if not cls.__callable_proto_members_only__:
raise TypeError(
"Protocols with non-method members don't support issubclass()"
)

I believe that this block of code can therefore be safely deleted.

Linked PRs

  • gh-105834: Add tests for calling issubclass() between two protocols #105835
  • [3.12] gh-105834: Add tests for calling issubclass() between two protocols (GH-105835) #105859
  • [3.11] gh-105834: Backport new tests for typing.Protocol (#105835) #105860

Activity

  1. AlexWaygood
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    topic-typing
    on Jun 15, 2023
  2. AlexWaygood commented on Jun 16, 2023

    @AlexWaygood
    AlexWaygood
    on Jun 16, 2023
    MemberAuthor
    More actions

    As has been pointed out in the PR discussion, this code is not in fact dead, just completely untested. (Thanks @JelleZijlstra!)

  3. AlexWaygood
    changed the title [-]Dead code in `typing.py`[/-] [+]Untested code in `typing.py`[/+] on Jun 16, 2023
  4. AlexWaygood
    added
    testsTests in the Lib/test dirTests in the Lib/test dir
    on Jun 16, 2023
  5. AlexWaygood
    added a commit that references this issue on Jun 16, 2023

    gh-105834: Add tests for calling `issubclass()` between two protocols (…

    70c075c
  6. miss-islington
    added a commit that references this issue on Jun 16, 2023

    pythongh-105834: Add tests for calling `issubclass()` between two pro…

    2aa26aa
  7. AlexWaygood
    added a commit that references this issue on Jun 16, 2023

    [3.11] pythongh-105834: Add tests for calling `issubclass()` between …

    7a3b537
  8. JelleZijlstra
    added 2 commits that reference this issue on Jun 16, 2023

    [3.12] gh-105834: Add tests for calling `issubclass()` between two pr…

    e6982c5

    [3.11] gh-105834: Backport new tests for `typing.Protocol` (#105835) (#…

    e590338
  9. AlexWaygood
    closed this as completedon Jun 16, 2023
  10. carljm
    added a commit that references this issue on Jun 16, 2023

    Merge branch 'main' into modulelocals

    b096651
  11. gvanrossum
    added a commit that references this issue on Jun 18, 2023

    pythongh-105834: Add tests for calling `issubclass()` between two pro…

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirTests in the Lib/test dirtopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.