You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix inference when class and instance match protocol (python#18587)
Fixespython#14688
The bug resulted from (accidentally) inferring against `Iterable` for
both instance and class object. While working on this I noticed there
are also couple flaws in direction handling in constrain inference,
namely:
* A protocol can never ever be a subtype of class object or a `Type[X]`
* When matching against callback protocol, subtype check direction must
match inference direction
I also (conservatively) fix some unrelated issues uncovered by the fix
(to avoid fallout):
* Callable subtyping with trivial suffixes was broken for
positional-only args
* Join of `Parameters` could lead to meaningless results in case of
incompatible arg kinds
* Protocol inference was inconsistent with protocol subtyping w.r.t.
metaclasses.
Copy file name to clipboardExpand all lines: test-data/unit/check-functions.test
+26-4Lines changed: 26 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -105,16 +105,38 @@ if int():
105
105
h = h
106
106
107
107
[case testSubtypingFunctionsDoubleCorrespondence]
108
+
def l(x) -> None: ...
109
+
def r(__x, *, x) -> None: ...
110
+
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, NamedArg(Any, 'x')], None]")
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, NamedArg(Any, 'x')], None]")
114
+
def r(__x, *, x = 1) -> None: ...
115
+
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, DefaultNamedArg(Any, 'x')], None]")
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Any, DefaultNamedArg(Any, 'x')], None]")
r = l # E: Incompatible types in assignment (expression has type "Callable[[Any], None]", variable has type "Callable[[Arg(Any, 'x'), VarArg(Any), KwArg(Any)], None]")
r = l # E: Incompatible types in assignment (expression has type "Callable[[DefaultArg(Any)], None]", variable has type "Callable[[DefaultArg(Any, 'x'), VarArg(Any), KwArg(Any)], None]")
0 commit comments