python / python/mypy

Analysis time binding of restricted `Generic`s

Open
#11,565 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

A generic type defined with reference to a restricted type variable only makes sense when containing a value of type within the restrictions of the type variable.

It would then make sense to assume that when the user does not bind the generic type (that is, they do not provide a type argument), the implied type is the Union of the possible bound types.

To Reproduce

from typing import Any
from typing import AnyStr
from typing import Generic


class Message(Generic[AnyStr]):
    header: AnyStr
    content: AnyStr

    def __init__(self, header: AnyStr, content: AnyStr) -> None:
        self.header = header
        self.content = content

# This feels wrong to me, and I'd rather see this as Union[Message[str], Message[bytes]]
m1: Message = Message(header="foo", content="bar")
reveal_type(m1)  # Message[Any]...

# This is perhaps ok, since the user explicitly passed the `Any` parameter. But if this behavior is kept, 
# one shouldn't be forced to provide a type arg to restricted generics in strict mode, since that 
# would imply having to enumerate the restriction to get the same behavior as above.
m2: Message[Any] = Message(header="foo", content="bar")
reveal_type(m2)  # Message[Any]...

Pitch

Say I have a function that accepts any valid Message as an arg:

def read(msg: Message) -> None:
    msg.content.method_in_neither_str_nor_bytes()

If I annotate it like I just did, I get no hand-holding from the type checker. msg.content is understood as Any, so everything goes. We know, however, that this has to be one of AnyStr. If I want type checker assistance, I now have to manually list the possible Message types, like so:

def read(msg: Union[Message[str], Message[bytes]]) -> None:
    msg.content.method_in_neither_str_nor_bytes()

This is fine when the restriction consists of two types, but it should be easy to imagine examples with a more verbose restriction.

Contributor guide

Open the contributing guide

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

The issue names no implementation files, tests, or entry points. Start with the reproduction and the existing handling of unbound restricted generics; done means inferring the union of permitted bound types instead of Any, while preserving explicitly supplied Any behavior and adding coverage for the shown cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.