-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[stubtest] transparent @type_check_only types
#20352
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |
| from mypy import nodes | ||
| from mypy.config_parser import parse_config_file | ||
| from mypy.evalexpr import UNKNOWN, evaluate_expression | ||
| from mypy.maptype import map_instance_to_supertype | ||
| from mypy.options import Options | ||
| from mypy.util import FancyFormatter, bytes_to_human_readable_repr, is_dunder, plural_s | ||
|
|
||
|
|
@@ -1852,10 +1853,39 @@ def describe_runtime_callable(signature: inspect.Signature, *, is_async: bool) - | |
| return f'{"async " if is_async else ""}def {signature}' | ||
|
|
||
|
|
||
| class _TypeCheckOnlyBaseMapper(mypy.types.TypeTranslator): | ||
| """Rewrites @type_check_only instances to the nearest runtime-visible base class.""" | ||
|
|
||
| def visit_instance(self, t: mypy.types.Instance, /) -> mypy.types.Type: | ||
| instance = mypy.types.get_proper_type(super().visit_instance(t)) | ||
| assert isinstance(instance, mypy.types.Instance) | ||
|
|
||
| if instance.type.is_type_check_only: | ||
| # find the nearest non-@type_check_only base class | ||
| for base_info in instance.type.mro[1:]: | ||
| if not base_info.is_type_check_only: | ||
| return map_instance_to_supertype(instance, base_info) | ||
|
|
||
| msg = f"all base classes of {instance.type.fullname!r} are @type_check_only" | ||
| assert False, msg | ||
|
|
||
| return instance | ||
|
|
||
| def visit_type_alias_type(self, t: mypy.types.TypeAliasType, /) -> mypy.types.Type: | ||
| return t | ||
|
Comment on lines
+1874
to
+1875
Contributor
Author
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. this method might look a bit awkward, but we need it because it's marked as abstract in |
||
|
|
||
|
|
||
| _TYPE_CHECK_ONLY_BASE_MAPPER = _TypeCheckOnlyBaseMapper() | ||
|
|
||
|
|
||
| def _relax_type_check_only_type(typ: mypy.types.ProperType) -> mypy.types.ProperType: | ||
| return mypy.types.get_proper_type(typ.accept(_TYPE_CHECK_ONLY_BASE_MAPPER)) | ||
|
|
||
|
|
||
| def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool: | ||
| """Checks whether ``left`` is a subtype of ``right``.""" | ||
| left = mypy.types.get_proper_type(left) | ||
| right = mypy.types.get_proper_type(right) | ||
| left = _relax_type_check_only_type(mypy.types.get_proper_type(left)) | ||
| right = _relax_type_check_only_type(mypy.types.get_proper_type(right)) | ||
| if ( | ||
| isinstance(left, mypy.types.LiteralType) | ||
| and isinstance(left.value, int) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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 assume here that it will eventually always have encountered
builtins.objectbefore getting here.