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
{{ message }}
Repository navigation
(🐞) No error when overload implementation has incompatible return type #12401
While the strictest of solutions would be to enforce the return type be an intersection of the overload return types, that would result in being overly strict to the point of being unusable. I suggest enforcing the return type to be a union of the overloads, this is not typesafe, but is much safer than not checking it at all.
Not sure why this should be an error. object is a supertype of int, and it's generally not possible for mypy to check that overload implementations are correct.
I think this is intentional, as there is no way to correctly annotate an overload like the following due to overload signatures being intersections in the return type.
The following:
fromtypingimportoverload@overloaddeffoo(a: int) ->int: ...
@overloaddeffoo(a: str) ->str: ...
deffoo(
a: int|str# correct
) ->int|str: ... # incorrect, should be intersection of int and str
It is at least, much more close to being correct, and I would expect mypy to enforce this tighter(unionized) type.
That being said, the true nature of overloads is far more complicated than just unionizing the inputs and intersecting the outputs, their final form is a complex conditional type with no currently existing method of describing or checking them.
i believe this is related to microsoft/TypeScript#47669. specifically, the issue is that the function implementation is not checked to be assignable to its type (the overloads).
in typescript, function interfaces/intersections can be used to create overloads on arrow functions as they do not have their own dedicated overload syntax, however this results in so many false positives that it's pretty much unusable.
take this example:
// Type '(value: string | number) => string | number' is not assignable to type '{ (value: number): number; (value: string): string; }'.// Type 'string | number' is not assignable to type 'number'.// Type 'string' is not assignable to type 'number'constfoo: {(value: number): number(value: string): string}=(value)=>value
there are two workarounds for this, both of which are pretty annoying:
so i'm skeptical of making overloads work this way. while it is stricter, it makes overloads pretty much unusable in most cases (in typescript it's not as much of a problem because you can get around it with generics and conditional types for some of the more complicated overloads)
i think @KotlinIsland's idea of enforcing that the impl's return type is at least a union of the return types in the overloads is a decent compromise tho
Activity
JelleZijlstra commentedon Mar 21, 2022
Not sure why this should be an error.
objectis a supertype of int, and it's generally not possible for mypy to check that overload implementations are correct.KotlinIsland commentedon Mar 21, 2022
@JelleZijlstra It's not to do with the implementation,
objectisn't a subtype ofint, it's the same as the following scenario:In the OP only subtypes of
intwould be valid, egintorbool.KotlinIsland commentedon Mar 21, 2022
I think this is intentional, as there is no way to correctly annotate an overload like the following due to overload signatures being intersections in the return type.
The following:
It is at least, much more close to being correct, and I would expect mypy to enforce this tighter(unionized) type.
That being said, the true nature of overloads is far more complicated than just unionizing the inputs and intersecting the outputs, their final form is a complex conditional type with no currently existing method of describing or checking them.
DetachHead commentedon Mar 21, 2022
i believe this is related to microsoft/TypeScript#47669. specifically, the issue is that the function implementation is not checked to be assignable to its type (the overloads).
in typescript, function interfaces/intersections can be used to create overloads on arrow functions as they do not have their own dedicated overload syntax, however this results in so many false positives that it's pretty much unusable.
take this example:
there are two workarounds for this, both of which are pretty annoying:
or more accurately (which almost defeats the point of the overload in the first place):
so i'm skeptical of making overloads work this way. while it is stricter, it makes overloads pretty much unusable in most cases (in typescript it's not as much of a problem because you can get around it with generics and conditional types for some of the more complicated overloads)
i think @KotlinIsland's idea of enforcing that the impl's return type is at least a union of the return types in the overloads is a decent compromise tho
KotlinIsland commentedon Mar 21, 2022
This is almost identical to a constrained
TypeVar!