Skip to content

spec/conformance: function with TypeVar default and default argument used in context. #2213

@randolf-scholz

Description

@randolf-scholz

I want to suggest adding the following test to the conformance test suite:

class Getter[T]:
    def get[S=None](self, default: S = None) -> T | S: ...  # OK

def test(arg: Getter[str]) -> None:
    result: str = arg.get()  # type: ignore
  • Since the default argument value is assignable to the default type var type, def get is well-defined
  • arg.get() implicitly uses None of type None as the argument. Therefore, we have an implicit constraint S <: None when .get is called without explicit default.

Therefore, result: str = arg.get() should raise an assignment type error (str | None not assignalbe to str).

Currently, only pyright succeeds on this test, and type checker results diverge.

  1. pyright: no errors (true negative on def get, true positive on result) (pyright playground)
  2. mypy: false positive on def get, false negative on result.
  3. ty: false positive on def get, true positive on result. (playground)
  4. pyrefly: false positive on def get, false negative on result (playground)
  5. zuban: false postive on def get, false negative on result.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions