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 — gh-105144: abc: Suppress errors raised by unrelated other subclasses by JelleZijlstra · Pull Request #105159 · python/cpython

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented May 31, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

A more invasive solution for #105144; perhaps better kept for 3.13 only.

  • Issue: Strange interaction between typing.Protocol and unrelated isinstance() checks #105144

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JelleZijlstra
pythongh-105144: abc: Suppress errors raised by unrelated other subcl…
2008aa5
…asses

A more invasive solution for python#105144; perhaps better kept for 3.13 only.
@JelleZijlstra
JelleZijlstra requested a review from AlexWaygood May 31, 2023 16:56
@bedevere-bot bedevere-bot added the awaiting core review label May 31, 2023
@bedevere-bot bedevere-bot mentioned this pull request May 31, 2023
Strange interaction between typing.Protocol and unrelated isinstance() checks #105144
Closed
JelleZijlstra
JelleZijlstra commented May 31, 2023
View reviewed changes
Comment thread Lib/_py_abc.py
if issubclass(subclass, rcls):
cls._abc_cache.add(subclass)
return True
except Exception:

@JelleZijlstra JelleZijlstra May 31, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In the C code I actually suppress all exceptions, not just Exception. Not sure what the right thing to do is.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AlexWaygood AlexWaygood May 31, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'd say suppressing all exceptions would definitely be bad -- that would mean that we'd be suppressing e.g. KeyboardInterrupt, and SystemExit.

Honestly, I feel pretty uncomfortable about suppressing all subclasses of Exception -- could we just do TypeError? If I had a typo in a custom __subclasscheck__ method, I'm not sure I'd want e.g. the resulting AttributeError to be silenced because of the fact that ABCMeta was being used somewhere

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JelleZijlstra JelleZijlstra May 31, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You'd still see the error if you use your broken class directly. What this PR changes is that you'll no longer see the error if you use issubclass() checks on a separate class that inherits from the same ABC.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood

AlexWaygood commented Jun 1, 2023

Copy link
Copy Markdown
Member

Looks like there's still lots of test_typing failures if I apply this diff to your PR branch:

diff --git a/Lib/typing.py b/Lib/typing.py
index 8c874797d2..8caeb915d8 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1733,7 +1733,7 @@ def _allow_reckless_class_checks(depth=3):
     The abc and functools modules indiscriminately call isinstance() and
     issubclass() on the whole MRO of a user class, which may contain protocols.
     """
-    return _caller(depth) in {'abc', 'functools', None}
+    return _caller(depth) in {'functools', None}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jun 1, 2023

Copy link
Copy Markdown
Member

The trouble is that these __subclasscheck__ calls call into _ProtocolMeta.__subclasscheck__, which might raise:

cpython/Lib/_py_abc.py

Lines 105 to 106 in ec0082c

return cls.__subclasscheck__(subclass)
return any(cls.__subclasscheck__(c) for c in (subclass, subtype))

But we don't want it to raise if the __subclasscheck__ call is coming from an __instancecheck__ call.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jun 1, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Ultimately, it does sort of feel like typing is the module that's "in the wrong" here. The abc module makes the assumption that issubclass() calls will never raise, providing both arguments are instances of type. That's true for the vast majority of classes, so I think it's a reasonable assumption to make. typing is the module that's doing a surprising/unusual thing here, so it seems reasonable to say that it's typing that should have to do the awkward workaround. It's unfortunate how fragile that makes some of the code in typing.py, but I think PEP-544's semantics are too well-established now to change the behaviour.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Jun 16, 2023

Copy link
Copy Markdown
Member Author

I feel like this change isn't worth the churn.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra JelleZijlstra closed this Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood Awaiting requested review from AlexWaygood

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@JelleZijlstra @AlexWaygood @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.