mypy icon indicating copy to clipboard operation
mypy copied to clipboard

defining the choice of overload when more than one applies

Open ashleyh opened this issue 13 years ago • 5 comments

I don't think it's completely clear what the following example should print:

class A: pass
class B(A): pass

def f(A a): print('A')
def f(B b): print('B')

f(B())

One might intuitively expect the 'best match' to be chosen, which would print 'B', but the current implementation actually chooses the 'first match', which would print 'A'. This isn't wrong per se but I think it's at least worth documenting.

ashleyh avatar Dec 09 '12 18:12 ashleyh

Yes, the desired behaviour is to pick the first matching overload variant. It should be described in the documentation.

JukkaL avatar Dec 11 '12 11:12 JukkaL

Ah, and the type checker should probably give a warning in cases like the above example, since the second overload can never be called.

JukkaL avatar Dec 11 '12 14:12 JukkaL

By the way, the overview page says:

The most specific signature is selected at runtime at each call.

ashleyh avatar Dec 13 '12 20:12 ashleyh

Oops, that's how I initially designed it, but it turned out to be a bad idea (often there is no single most specific signature). No it should be fixed in the overview.

JukkaL avatar Dec 13 '12 20:12 JukkaL

Yeah, I agree that first match is a better idea, just wanted to point out the discrepancy in the docs.

ashleyh avatar Dec 13 '12 20:12 ashleyh