-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Believe more __new__ return types #16020
Open
hauntsaninja
wants to merge
8
commits into
python:master
Choose a base branch
from
hauntsaninja:abide-by-new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−22
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4616fa2
Believe arbitrary __new__ return types
hauntsaninja e2d3a5e
changes
hauntsaninja 2bd91d5
more changes
hauntsaninja 393b303
hacks
hauntsaninja 7b7d76d
fix instantiate
hauntsaninja aa5df74
clarify comment
hauntsaninja 59a5aee
clarify comment
hauntsaninja cdccffa
Merge branch 'master' into abide-by-new
JelleZijlstra 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 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
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -368,8 +368,27 @@ def validate_super_call(node: FuncBase, mx: MemberContext) -> None: | |||||
def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: MemberContext) -> Type: | ||||||
# Class attribute. | ||||||
# TODO super? | ||||||
ret_type = typ.items[0].ret_type | ||||||
item = typ.items[0] | ||||||
ret_type = item.ret_type | ||||||
assert isinstance(ret_type, ProperType) | ||||||
|
||||||
# The following is a hack. mypy often represents types as CallableType, where the signature of | ||||||
# CallableType is determined by __new__ or __init__ of the type (this logic is in | ||||||
# type_object_type). Then if we ever need the TypeInfo or an instance of the type, we fish | ||||||
# around for the return type, e.g. in CallableType.type_object. Unfortunately, this is incorrect | ||||||
# if __new__ returns an unrelated type, but we can kind of salvage things here using | ||||||
# CallableType.bound_args | ||||||
self_type: Type = typ | ||||||
if len(item.bound_args) == 1 and item.bound_args[0]: | ||||||
bound_arg = get_proper_type(item.bound_args[0]) | ||||||
if isinstance(bound_arg, Instance) and isinstance(ret_type, Instance): | ||||||
# Unfortunately, generic arguments have already been determined for us. We need these, | ||||||
# see e.g. testGenericClassMethodUnboundOnClass. So just copy them over to our type. | ||||||
# This does the wrong thing with custom __new__, see testNewReturnType15, but is | ||||||
# no worse than previous behaviour. | ||||||
ret_type = bound_arg.copy_modified(args=ret_type.args) | ||||||
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. Maybe you can
Suggested change
if you change checkexpr.py's return callable.copy_modified(
ret_type=expand_type(callable.ret_type, id_to_type),
+ bound_args=[
+ expand_type(ba, id_to_type) if ba is not None else None
+ for ba in callable.bound_args
+ ],
variables=remaining_tvars,
type_guard=type_guard,
) |
||||||
self_type = TypeType(ret_type) | ||||||
|
||||||
if isinstance(ret_type, TupleType): | ||||||
ret_type = tuple_fallback(ret_type) | ||||||
if isinstance(ret_type, TypedDictType): | ||||||
|
@@ -391,7 +410,11 @@ def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: Member | |||||
# See https://github.com/python/mypy/pull/1787 for more info. | ||||||
# TODO: do not rely on same type variables being present in all constructor overloads. | ||||||
result = analyze_class_attribute_access( | ||||||
ret_type, name, mx, original_vars=typ.items[0].variables, mcs_fallback=typ.fallback | ||||||
ret_type, | ||||||
name, | ||||||
mx.copy_modified(self_type=self_type), | ||||||
original_vars=typ.items[0].variables, | ||||||
mcs_fallback=typ.fallback, | ||||||
) | ||||||
if result: | ||||||
return result | ||||||
|
This file contains 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
This file contains 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
This file contains 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 agree with other comments that using
bound_args
doesn't look like something we want to do. I think storing the real self type in a new attribute might be a better approach. All type operations should be updated to do something reasonable with the attribute.It seems like the above should also fix the issue that no error is currently reported here, and perhaps other related issues:
We'd use use the new attribute when looking up class attributes.