python / python/mypy

Failing to infer type upon concretization of generic (inferred <nothing>)

Open
#14,785 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I created a generic class (Baz) which except an instance with a generic interface (Foo), and manipulates it.

I made a non-generic class (Bar), subclassing the interface with a concrete type (str).

Inside the class the types work, but when I use the non-generic class with the manipulator class, I have type errors.

I expected mypy to infer the concretization of the generic type from the usage.

I have confirmation that my typing is indeed correct: https://stackoverflow.com/questions/75568405/typing-of-using-a-subclass-of-a-generic-class?noredirect=1#comment133323604_75568405

To Reproduce


from typing import Generic, TypeVar


T = TypeVar("T")


class Foo(Generic[T]):
    def foo(self, arg: T) -> None:
        pass


class Bar(Foo[str]):
    def foo(self, arg: str) -> None:
        print(arg + " boldly runs")


T1 = TypeVar("T1")


class Baz(Generic[T1]):
    def sirRobin(self, instance: Foo[T1], param: T1) -> None:
        instance.foo(param)


# no problem, no output
Baz[str]().sirRobin(Foo(), "she")
Baz[int]().sirRobin(Foo(), 1)
print("----")
# Argument 1 to "sirRobin" of "Baz" has incompatible type "Bar"; expected "Foo[<nothing>]"  [arg-type]
# Argument 2 to "sirRobin" of "Baz" has incompatible type "str"; expected < nothing > [arg-type]
# works as expected though
Baz().sirRobin(Bar(), "he")

Playground url: https://mypy-play.net/?mypy=latest&python=3.11&gist=7bdb14547c7d7ea11185c02e6267c016

Expected Behavior
Correctly infer that T=str
No errors

Actual Behavior

Inferred T = nothing

Argument 1 to "sirRobin" of "Baz" has incompatible type "Bar"; expected "Foo[]" [arg-type]
Argument 2 to "sirRobin" of "Baz" has incompatible type "str"; expected < nothing > [arg-type]

Your Environment

  • Mypy version used: latest
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
strict = True
explicit_package_bases = True
namespace_packages = True
#mypy_path = src:test
files = src,test
mypy_path = $MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/test

  • Python version used: 3.11

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

Start with the minimal Foo, Bar, and Baz generic reproduction in the issue and run it with the linked mypy-play example. Trace how an unparameterized Baz call infers T1 during sirRobin argument checking. Done means Baz().sirRobin(Bar(), "he") infers T1 as str and produces no errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.