Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[spec] overload subtyping rules are too strict. #2021

Open
Labels
topic: otherOther topics not covered
@randolf-scholz

Description

@randolf-scholz

Thetyping spec states that, whenA andB are potentially overloaded methods

If a callableB is overloaded with two or more signatures, it isassignable to callableA if at least one of the overloaded signatures inB is assignable toA

If a callableA is overloaded with two or more signatures, callableB is assignable toA ifB is assignable to all of the signatures inA

However, this definition seems to be too strict. Consider the following example:

fromtypingimportProtocol,Self,overloadclassIntScalarOurs(Protocol):def__add__(self,other:int|Self,/)->Self: ...classIntScalarTheirs(Protocol):@overloaddef__add__(self,other:int,/)->Self: ...@overloaddef__add__(self,other:Self,/)->Self: ...

These two protocols specify the exact same runtime behavior. Yet, following the rules of the spec,IntScalarOurs is assignable toIntScalarTheirs, butIntScalarTheirs is not assignable toIntScalarOurs.

Case 1:A=IntScalarOurs,B=IntScalarTheirs.

If a callable B is overloaded with two or more signatures, it is assignable to callable A if at least one of the overloaded signatures in B is assignable to A

  • Is(self, int) -> Self assignable to(self, int | Self) -> Self? No, becauseint is not a supertype ofint | Self (contravariance)
  • Is(self, Self) -> Self assignable to(self, int | Self) -> Self? No, becauseSelf is not a supertype ofint | Self (contravariance)

Thus,IntScalarTheirs is not assignable toIntScalarOurs.

Case 2:A=IntScalarTheirs,B=IntScalarOurs.

If a callable A is overloaded with two or more signatures, callable B is assignable to A if B is assignable to all of the signatures in A

  • Is(self, int | Self) -> Self assignable to(self, int) -> Self? Yes.
  • Is(self, int | Self) -> Self assignable to(self, Self) -> Self? Yes.

Thus,IntScalarOurs is assignable toIntScalarTheirs.

From a pure set-theoretic POV, it seems that subtyping functions should follow a very simply rule based onthe domain.

$f <: g$ if and only if$\text{dom}(f) ⊇ \text{dom}(g)$ and$f(x) <: g(x)$ for all$x∈\text{dom}(g)$

What is currently specced appears to be a lemma for a sufficient condition, but not a necessary one.
Individual type-checkers can of course use such lemmas if the general case is too difficult to check/implement (and communicate that to their users), but shouldn't the spec be biased towards the theoretically principled point of view, when applicable?

Related Discussions

#1782

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp