python / python/typing

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

Open
#2,213 3 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: typing spec
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the typing conformance test suite and its existing tests for TypeVar defaults and default arguments. Add the proposed Getter example with the expected definition and assignment diagnostics, then compare the results across the listed type checkers so the conformance expectation is explicit.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.