Introduce a type for an unknown invariant generic param that behaves strictly

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

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Review the examples in this issue and compare them with the prior discussion in #1835. Define the desired behavior for an unknown generic parameter, including which calls are accepted or rejected; the issue is complete when the typing behavior and implementation path are agreed.

Written by the indexing model from the issue text.

Description

topic: feature

This is the same issue as #1835, which was closed -- the given reason was that object can be used instead of Unknown. However, this is not the case. Consider:

class Foo(tp.Generic[T]):
  def bar(self, x: T) -> T:
    ...

  def baz(...):
    ...

I want a type that represents "Foo with unknown T", which I can call baz on but not bar.

foo: Foo[Unknown] = ...
foo.bar(1)  # type error, int cannot be assigned to Unknown
foo.baz()  # works fine because no T is needed

This doesn't work if I use object:

foo: Foo[object] = ...
foo.bar(1)  # typechecks because int is a subtype of object
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

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.

More from python/typing

All issues in python/typing

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.