Repository navigation
[Python] Do not fail type checks on Protocol type hints #40757
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -368,6 +368,16 @@ def is_typing_generic(type_param): | |
| return isinstance(type_param, typing._GenericAlias) | ||
|
|
||
|
|
||
| def _is_unchecked_protocol(type_param): | ||
| """Determines if an object is a typing.Protocol class that is not | ||
| runtime_checkable, which isinstance() and issubclass() reject. | ||
| """ | ||
| return ( | ||
| isinstance(type_param, type) and | ||
| getattr(type_param, '_is_protocol', False) and | ||
| not getattr(type_param, '_is_runtime_protocol', False)) | ||
|
Comment on lines
+371
to
+378
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth considering while we're here: even with Keeping |
||
|
|
||
|
|
||
| def validate_composite_type_param(type_param, error_msg_prefix): | ||
| """Determines if an object is a valid type parameter to a | ||
| :class:`CompositeTypeHint`. | ||
|
|
@@ -429,6 +439,8 @@ def check_constraint(type_constraint, object_instance): | |
| pass | ||
| elif not isinstance(type_constraint, type): | ||
| raise RuntimeError("bad type: %s" % (type_constraint, )) | ||
| elif _is_unchecked_protocol(type_constraint): | ||
| pass | ||
| elif not isinstance(object_instance, type_constraint): | ||
| raise SimpleTypeHintError | ||
|
|
||
|
|
@@ -1536,6 +1548,9 @@ def is_consistent_with( | |
| # Cannot check unsupported parameterized generic which will cause issubclass | ||
| # to fail with an exception. | ||
| return False | ||
| elif _is_unchecked_protocol(base): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium: Because Moving the |
||
| # Protocols are matched structurally, which issubclass cannot check. | ||
| return True | ||
|
Comment on lines
+1551
to
+1553
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
| if use_beartype: | ||
| try: | ||
| return is_subhint(sub, base) | ||
|
|
||
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.
Nit: Could we also add a corresponding test in
PerformanceRuntimeTypeCheckTestright below to make sure the--performance_runtime_type_checkpath stays covered too?