-
-
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
Fix crashes and fails in forward references #3952
Changes from 1 commit
45e5931
cb4caa5
1cdc980
260ef02
a58a217
950a022
411b24d
b9b8528
48d6de4
ec45441
ac32ed4
3fb3019
f9b1320
cf014b8
665236b
9a318aa
757fbd9
4502ce2
3b39d40
c8b28fe
9f92b0f
9779103
b914bdb
3568fdb
54d9331
b9ddacc
5bfe9ca
a2912e9
10c65b8
21dfbfe
f2ddbcd
03597ee
649ef32
83f8907
13c7176
79b10d6
321a809
076c909
c1a63ec
97e6f47
8f52654
6edd078
514b8bd
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 |
---|---|---|
|
@@ -660,6 +660,7 @@ def visit_instance(self, t: Instance) -> None: | |
t.invalid = True | ||
elif info.defn.type_vars: | ||
# Check type argument values. | ||
# TODO: Calling is_subtype and is_same_types in semantic analysis is a bad idea | ||
for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars): | ||
if tvar.values: | ||
if isinstance(arg, TypeVarType): | ||
|
@@ -672,12 +673,28 @@ def visit_instance(self, t: Instance) -> None: | |
else: | ||
arg_values = [arg] | ||
self.check_type_var_values(info, arg_values, tvar.name, tvar.values, i + 1, t) | ||
# TODO: These hacks will be not necessary when this will be moved to later stage. | ||
if isinstance(arg, ForwardRef): | ||
arg = arg.link | ||
if not is_subtype(arg, tvar.upper_bound): | ||
bound = tvar.upper_bound | ||
if isinstance(bound, ForwardRef): | ||
bound = bound.link | ||
if isinstance(bound, Instance) and bound.type.replaced: | ||
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. I wonder if this code is almost duplicated somewhere else. If so, it would be better to have only one implementation in a utility function/method. 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. I will minimize duplication (but I can't use exactly the code from visitor in |
||
replaced = bound.type.replaced | ||
if replaced.tuple_type: | ||
bound = replaced.tuple_type | ||
if replaced.typeddict_type: | ||
bound = replaced.typeddict_type | ||
if isinstance(arg, Instance) and arg.type.replaced: | ||
replaced = arg.type.replaced | ||
if replaced.tuple_type: | ||
arg = replaced.tuple_type | ||
if replaced.typeddict_type: | ||
arg = replaced.typeddict_type | ||
if not is_subtype(arg, bound): | ||
self.fail('Type argument "{}" of "{}" must be ' | ||
'a subtype of "{}"'.format( | ||
arg, info.name(), tvar.upper_bound), t) | ||
arg, info.name(), bound), t) | ||
for arg in t.args: | ||
arg.accept(self) | ||
if info.is_newtype: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -440,7 +440,7 @@ T = TypeVar('T', bound='M') | |
class G(Generic[T]): | ||
x: T | ||
|
||
yb: G[int] | ||
yb: G[int] # E: Type argument "builtins.int" of "G" must be a subtype of "Tuple[builtins.int, fallback=__main__.M]" | ||
yg: G[M] | ||
z: int = G[M]().x.x | ||
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. Using |
||
z = G[M]().x[0] | ||
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.
|
||
|
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.
What about
type_annotation
ofArgument
? Should we process it?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 was not able to trigger any errors with this, I will double check.